[Python-3000] Move argv[0]? (Re: Unicode and OS strings) (original) (raw)

Ron Adam rrr at ronadam.com
Mon Sep 17 17:51:18 CEST 2007


Greg Ewing wrote:

Thomas Wouters wrote:

If you want to put more meaning in the argv list, use an option parser. I want to put less meaning in it, not more. :-) And using an argument parser is often overkill for simple programs.

Would it be possible to split out the (pre) parsing from optparse so that instead of returning a list, it returns a dictionary of attributes and values?

This would only contain what was given in the command line as a first "lighter weight" step to parsing the command line.

opts = opt_parser(argv)
command_name = opts['argv0']   # better name for argv0?

Or...

opts = opt_parser(argv)
if "-h" in opts or "--h" in opts:
   print("Help on {argv0}: ...".format(opts))

If the dictionary was pre defined with defaults it might be more like..

 opts = {'-h':False, '--h':False}
 opts.update(opt_parser(argv)

 if opts['-h'] or opts['--h']:
    print("Help on {argv0}: ...".format(opts))

This avoids the loop for the simplest cases.

A second dispatcher/validator object could then use this as input.

Regards, Ron

The actual meaning of each element depends entirely on the program that's started. For Python-the-language, there isn't any difference between them. So in your Python programs, you're quite happy to write for arg in sys.argv: process(arg) and not care about what this does with argv[0]? I hardly see how one can claim that there's "no difference" between argv[0] and the rest for practical purposes.



More information about the Python-3000 mailing list