Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I process command line arguments in Python?

What would be an easy expression to process command line arguments if I'm expecting anything like 001 or 999 (let's limit expectations to 001...999 range for this time), and few other arguments passed, and would like to ignore any unexpected?

I understand if for example I need to find out if "debug" was passed among parameters it'll be something like that:

if 'debug' in argv[1:]:   print 'Will be running in debug mode.' 

How to find out if 009 or 575 was passed?

All those are expected calls:

python script.py python script.py 011 python script.py 256 debug python script.py 391 xls python script.py 999 debug pdf 

At this point I don't care about calls like that:

python script.py 001 002 245 568 python script.py some unexpected argument python script.py 0001 python script.py 02 

...first one - because of more than one "numeric" argument; second - because of... well, unexpected arguments; third and fourth - because of non-3-digits arguments.

like image 418
user63503 Avatar asked Feb 20 '09 00:02

user63503


People also ask

How do you process 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.

How do you use command line in Python?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!


1 Answers

As others answered, optparse is the best option, but if you just want quick code try something like this:

import sys, re  first_re = re.compile(r'^\d{3}$')  if len(sys.argv) > 1:      if first_re.match(sys.argv[1]):         print "Primary argument is : ", sys.argv[1]     else:         raise ValueError("First argument should be ...")      args = sys.argv[2:]  else:      args = ()  # ... anywhere in code ...  if 'debug' in args:     print 'debug flag'  if 'xls' in args:     print 'xls flag' 

EDIT: Here's an optparse example because so many people are answering optparse without really explaining why, or explaining what you have to change to make it work.

The primary reason to use optparse is it gives you more flexibility for expansion later, and gives you more flexibility on the command line. In other words, your options can appear in any order and usage messages are generated automatically. However to make it work with optparse you need to change your specifications to put '-' or '--' in front of the optional arguments and you need to allow all the arguments to be in any order.

So here's an example using optparse:

import sys, re, optparse  first_re = re.compile(r'^\d{3}$')  parser = optparse.OptionParser() parser.set_defaults(debug=False,xls=False) parser.add_option('--debug', action='store_true', dest='debug') parser.add_option('--xls', action='store_true', dest='xls') (options, args) = parser.parse_args()  if len(args) == 1:     if first_re.match(args[0]):         print "Primary argument is : ", args[0]     else:         raise ValueError("First argument should be ...") elif len(args) > 1:     raise ValueError("Too many command line arguments")  if options.debug:     print 'debug flag'  if options.xls:     print 'xls flag' 

The differences here with optparse and your spec is that now you can have command lines like:

python script.py --debug --xls 001 

and you can easily add new options by calling parser.add_option()

like image 173
Van Gale Avatar answered Sep 21 '22 23:09

Van Gale