I want to send arguments to script by their name (something like kwargs). I tried something like this but it's not doing what I want: (let's say it's written in script.py)
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("name")
args = parser.parse_args()
and then writing in commant line: script.py name = david
another thing, let's say I have few named argument in argparse If I will send them not in the order they are declared will it still work well?
Using the nargs parameter in add_argument() , you can specify the number (or arbitrary number) of inputs the argument should expect. In this example named sum.py , the --value argument takes in 3 integers and will print the sum.
The store_true option automatically creates a default value of False. Likewise, store_false will default to True when the command-line argument is not present. The source for this behavior is succinct and clear: http://hg.python.org/cpython/file/2.7/Lib/argparse.py#l861.
parse_args() returns two values: options, an object containing values for all of your options— e.g. if "--file" takes a single string argument, then options. file will be the filename supplied by the user, or None if the user did not supply that option.
Metavar: It provides a different name for optional argument in help messages. Provide a value for the metavar keyword argument within add_argument() .
In argparse
, and earlier command line processers of the same style, there's a distinction between 'optionals' or flagged arguments and positionals.
'optionals' are signaled by a flag string, something like -f
or --foo
. These are similar to the keyword arguments of Python functions, but not identical. The order does not matter. With in limits the flags can be joined to the values, e.g. -f1
, --foo=astring
.
'positionals' are identified by order, without any identifying name. These are similar to the args
of Python functions. In functions, all positional args have to occur before keyword ones. With argparse
'optionals' can mixed with 'positionals' - with some limits. It's common to supply all positionals after the optionals, as indicated in the argparse
usage message.
Look at the examples in the argparse
documentation.
Periodically we get questions from people who want to circumvent these conventions, for example expecting to use flag without the prefix characters, or to input dictionary like pairs, foo=test
or foo:test
. Some of this is possible, but it takes more work. And usually for little gain in clarity and usefulness.
I'd suggest passing script.py name = david
to a script that just displays the sys.argv
list. These are the values that argparse
has to work with. I expect you will see:
['script.py', 'name', '=', 'david']
Your shell has split that commandline into separate strings. It is probably easier to do your own parsing of that list than it is to twist argparse
into a form that will it.
Argparse can easily handle inputs like
script.py --name david
script.py --name=david
script.py david
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With