Merge pull request #1830 from alphapapa/parse-args

Add: parse-args function
This commit is contained in:
Kodi Arfer 2019-11-14 10:12:33 -05:00 committed by GitHub
commit be1eb4bf2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 1 deletions

View File

@ -94,3 +94,4 @@
* Tristan de Cacqueray <tdecacqu@redhat.com> * Tristan de Cacqueray <tdecacqu@redhat.com>
* Sören Tempel <soeren@soeren-tempel.net> * Sören Tempel <soeren@soeren-tempel.net>
* Noah Snelson <noah.snelson@protonmail.com> * Noah Snelson <noah.snelson@protonmail.com>
* Adam Porter <adam@alphapapa.net>

View File

@ -18,6 +18,7 @@ New Features
* All augmented assignment operators (except `%=` and `^=`) now allow * All augmented assignment operators (except `%=` and `^=`) now allow
more than two arguments. more than two arguments.
* PEP 3107 and PEP 526 function and variable annotations are now supported. * PEP 3107 and PEP 526 function and variable annotations are now supported.
* Added function ``parse-args`` which parses arguments with ``argparse``.
Other Breaking Changes Other Breaking Changes
------------------------------ ------------------------------

View File

@ -786,6 +786,29 @@ Returns ``True`` if *x* is odd. Raises ``TypeError`` if
=> (odd? 0) => (odd? 0)
False False
.. _parse-args:
parse-args
----------
Usage: ``(parse-args spec &optional args &kwargs parser-args)``
Return arguments namespace parsed from *args* or ``sys.argv`` with
:py:meth:`argparse.ArgumentParser.parse_args` according to *spec*.
*spec* should be a list of arguments which will be passed to repeated
calls to :py:meth:`argparse.ArgumentParser.add_argument`. *parser-args*
may be a list of keyword arguments to pass to the
:py:class:`argparse.ArgumentParser` constructor.
.. code-block:: hy
=> (parse-args [["strings" :nargs "+" :help "Strings"]
["-n" "--numbers" :action "append" :type 'int :help "Numbers"]]
["a" "b" "-n" "1" "-n" "2"]
:description "Parse strings and numbers from args")
Namespace(numbers=[1, 2], strings=['a', 'b'])
.. _partition-fn: .. _partition-fn:
partition partition

View File

@ -391,6 +391,19 @@ Even objects with the __name__ magic will work."
False False
(or a b))) (or a b)))
(defn parse-args [spec &optional args &kwargs parser-args]
"Return arguments namespace parsed from `args` or `sys.argv` with `argparse.ArgumentParser.parse-args` according to `spec`.
`spec` should be a list of arguments to pass to repeated calls to
`argparse.ArgumentParser.add-argument`. `parser-args` may be a list
of keyword arguments to pass to the `argparse.ArgumentParser`
constructor."
(import argparse)
(setv parser (argparse.ArgumentParser #** parser-args))
(for [arg spec]
(eval `(.add-argument parser ~@arg)))
(.parse-args parser args))
(setv EXPORTS (setv EXPORTS
'[*map accumulate butlast calling-module calling-module-name chain coll? '[*map accumulate butlast calling-module calling-module-name chain coll?
combinations comp complement compress constantly count cycle dec distinct combinations comp complement compress constantly count cycle dec distinct
@ -399,6 +412,6 @@ Even objects with the __name__ magic will work."
integer? integer-char? interleave interpose islice iterable? integer? integer-char? interleave interpose islice iterable?
iterate iterator? juxt keyword keyword? last list? macroexpand iterate iterator? juxt keyword keyword? last list? macroexpand
macroexpand-1 mangle merge-with multicombinations name neg? none? nth macroexpand-1 mangle merge-with multicombinations name neg? none? nth
numeric? odd? partition permutations pos? product read read-str numeric? odd? parse-args partition permutations pos? product read read-str
remove repeat repeatedly rest reduce second some string? symbol? remove repeat repeatedly rest reduce second some string? symbol?
take take-nth take-while tuple? unmangle xor tee zero? zip-longest]) take take-nth take-while tuple? unmangle xor tee zero? zip-longest])

View File

@ -478,6 +478,15 @@ result['y in globals'] = 'y' in globals()")
(assert-false (odd? 0)) (assert-false (odd? 0))
(assert-requires-num odd?)) (assert-requires-num odd?))
(defn test-parse-args []
"NATIVE: testing the parse-args function"
(setv parsed-args (parse-args [["strings" :nargs "+" :help "Strings"]
["-n" "--numbers" :action "append" :type 'int :help "Numbers"]]
["a" "b" "-n" "1" "-n" "2"]
:description "Parse strings and numbers from args"))
(assert-equal parsed-args.strings ["a" "b"])
(assert-equal parsed-args.numbers [1 2]))
(defn test-partition [] (defn test-partition []
"NATIVE: testing the partition function" "NATIVE: testing the partition function"
(setv ten (range 10)) (setv ten (range 10))