How can I process command line arguments in Python?

Posted by photographer on Stack Overflow See other posts from Stack Overflow or by photographer
Published on 2009-02-20T00:41:02Z Indexed on 2010/04/24 17:33 UTC
Read the original article Hit count: 366

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.

© Stack Overflow or respective owner

Related posts about python

Related posts about command-line