How to make sys.argv arguments optional? (Python)

sys.argv accepts arguments on the shell command line when the program starts. How to make these arguments optional?

I know that I can use try - except . But this forces you to insert either extra arguments, or all additional arguments, unless you enclose more try - except , which makes the code less clear.

Edit

Suppose I need the following functionality, how to implement this?

 $ python program.py add Peter 'Peter' was added to the list of names. 

This add argument (not --add ) is optional, so

 $ python program.py 

runs the program normally.

+8
python command-line-arguments sys
source share
5 answers

plac is an alternative to the standard library modules mentioned in other answers. It allows you to define command line arguments using annotations . From the documentation, Example 8 demonstrates an additional argument:

 example8.py def main(command: ("SQL query", 'option', 'q'), dsn): if command: print('executing %s on %s' % (command, dsn)) # ... if __name__ == '__main__': import plac; plac.call(main) 

Argparse example:

 import argparse parser = argparse.ArgumentParser() parser.add_argument("--add", help="Add prefix to string") args = parser.parse_args() 

Note that the convention is for an optional argument like "-add", while subcommands are provided as "add". Argparse has a subcommand .

+2
source share

You can use higher-level libraries: argparse , optparse , opster . They all support optional arguments.

+9
source share

EDIT to refer to your editing,

 import sys sys.argv = sys.argv[1:] names = [] while sys.argv and sys.argv[0] == 'add': #while the list is not empty and there is a name to add names.append(sys.argv[1]) print sys.argv[1], 'was added to the list of names.' sys.argv = sys.argv[2:] 

all the following work with this

 $ python program.py add Peter Peter was added to the list of names. $ python program.py add Peter add Jane Peter was added to the list of names. Jane was added to the list of names. $ python program.py 

if the advantage over the โ€œaddโ€ requirement over each name is that if there are any other arguments that you want to find after adding the names, you can. If you want to pass multiple names by saying python program.py add Peter Jane , this can be done with a fairly simple change

 import sys names = [] if len(sys.argv) > 2 and sys.argv[1] == 'add': names = sys.argv[2:] for n in names: print n, 'was added to the list of names.' 

ORIGINAL

You seem to be better off with something like optparse. However, since sys.argv is a list, you can check its length.

 arg1 = sys.argv[1] if len(sys.argv) > 1 else 0 # replace 0 with whatever default you want arg2 = sys.argv[2] if len(sys.argv) > 2 else 0 

and then use arg1 and arg2 as the "optional" command line arguments. this will allow you to pass command line arguments 1, 2 or 0 (in fact, you can pass more than 2 and they will be ignored). this also assumes the arguments are in a known order, if you want to use flags like -a followed by a value, look at optparse http://docs.python.org/library/optparse.html?highlight=optparse#optparse

+4
source share

You should use a command line parser such as getopt or argparse . They allow you to define options that are optional and have default values.

+2
source share

You can find the answer using argparse here here . Solutions using getopt or opster will be considered the most liked answer instead.

0
source share

All Articles