Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading named command arguments

Can I use argparse to read named command line arguments that do not need to be in a specific order? I browsed through the documentation but most of it focused on displaying content based on the arguments provided (such as --h).

Right now, my script reads ordered, unnamed arguments:

myscript.py foo-val bar-val

using sys.argv:

foo = sys.argv[1] bar = sys.argv[2] 

But I would like to change the input so that it is order agnostic by naming arguments:

myscript.py --bar=bar-val --foo=foo-val

like image 680
amphibient Avatar asked Oct 12 '16 14:10

amphibient


People also ask

How do I grab command line arguments?

To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments. The value of argc should be non negative. argv(ARGument Vector) is array of character pointers listing all the arguments.

How do you read an argument in shell?

The special character $# stores the total number of arguments. We also have $@ and $* as wildcard characters which are used to denote all the arguments. We use $$ to find the process ID of the current shell script, while $? can be used to print the exit code for our script.


2 Answers

You can use the Optional Arguments like so:

import argparse, sys  parser=argparse.ArgumentParser()  parser.add_argument('--bar', help='Do the bar option') parser.add_argument('--foo', help='Foo the program')  args=parser.parse_args()  print args print sys 

Then if you call it with ./prog --bar=bar-val --foo foo-val it prints:

Namespace(bar='bar-val', foo='foo-val') ['Untitled 14.py', '--bar=bar-val', '--foo', 'foo-val'] 

Or, if the user wants help argparse builds that too:

 $ ./prog -h usage: Untitled 14.py [-h] [--bar BAR] [--foo FOO]  optional arguments:   -h, --help  show this help message and exit   --bar BAR   Do the bar option   --foo FOO   Foo the program 
like image 98
dawg Avatar answered Sep 24 '22 15:09

dawg


The answer is yes. A quick look at the argparse documentation would have answered as well.

Here is a very simple example, argparse is able to handle far more specific needs.

import argparse  parser = argparse.ArgumentParser() parser.add_argument('--foo', '-f', help="a random options", type= str) parser.add_argument('--bar', '-b', help="a more random option", type= int, default= 0)  print(parser.format_help()) # usage: test_args_4.py [-h] [--foo FOO] [--bar BAR] #  # optional arguments: #   -h, --help         show this help message and exit #   --foo FOO, -f FOO  a random options #   --bar BAR, -b BAR  a more random option  args = parser.parse_args("--foo pouet".split()) print(args)  # Namespace(bar=0, foo='pouet') print(args.foo) # pouet print(args.bar) # 0 

Off course, in a real script, you won't hard-code the command-line options and will call parser.parse_args() (without argument) instead. It will make argparse take the sys.args list as command-line arguments.

You will be able to call this script this way:

test_args_4.py -h  # prints the help message test_args_4.py -f pouet  # foo="pouet", bar=0 (default value) test_args_4.py -b 42  # foo=None, bar=42 test_args_4.py -b 77 -f knock  # foo="knock", bar=77 

You will discover a lot of other features by reading the doc ;)

like image 26
Tryph Avatar answered Sep 21 '22 15:09

Tryph