process command line parameter

Cameron Simpson cs at zip.com.au
Sat Jan 17 22:40:07 CET 2009


On 17Jan2009 13:28, asit <lipun4u at gmail.com> 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=".co.uk"

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

Cheers,
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/



More information about the Python-list mailing list