process command line parameter

Cameron Simpson cs at
Sat Jan 17 22:40:07 CET 2009

On 17Jan2009 13:28, asit <lipun4u at> wrote:
| Recently I was coding a link extractor. It's a command line stuff and
| takes parameter as argument.
| I found that the in operator is not always helpful.
| eg. if "--all" in sys.argv:
|            print "all links will be printed"

Indeed. While I can't speak for your particular app, usually command
line parameters are context sensitive. Therefore one normally processes
them in order (especially if you want to support the fairly normal "--"
option which means "no more options after this", useful if you need to
supply a filename called "--app").

If you dont want to use the getopt standard module and thus its
conventionaly argument syntax, then one alternative is like this:

| its not helpful when some attribute value is sent in command line
| parameter.
| hence I want to process some data like find=""

  for arg in sys.argv[1:]:
    if arg == "--":
    elif arg =="--all":
    elif arg.startswith("find="):
      # unhandled argument: complain or drop out of loop

Cameron Simpson <cs at> DoD#743

More information about the Python-list mailing list