Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

named argument in argparse [duplicate]

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?

like image 511
davidmoshko Avatar asked Nov 10 '15 19:11

davidmoshko


People also ask

What does Nargs do in Argparse?

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.

What is action Store_true in Argparse?

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.

What does parse_args return?

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.

What is Metavar in Argparse Python?

Metavar: It provides a different name for optional argument in help messages. Provide a value for the metavar keyword argument within add_argument() .


1 Answers

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
like image 140
hpaulj Avatar answered Oct 09 '22 17:10

hpaulj