sys.argv is munging my command line options

Arnau Sanchez arnau at ehas.org
Wed Aug 29 15:59:30 EDT 2007


Chris Allen escribió:

> action key=value key=value...
> 
> Where action is a required string (ie. 'backup', 'init', 'restore',
> etc) and the program can accept one or more key value pairs.  I know
> this syntax isn't standard, but I think it works great for my program
> as each key can override an identically named field in a configuration
> file, that way the user doesn't have to have two different syntaxes to
> do the same thing.  I could do this with --key value

It's ok for a personal script, but if you expect other people to use it, 
you should consider the "Rule of Least Surprise" and follow the standard.

Anyway, using getopt or optparse, it would be "--key=value", or perhaps 
"-o key=value / --option=key=value"

> But I'm running into a problem with this which is that sys.argv splits
> my key=value options.  I need to know the option associations, and
> there's no way to know this by inspecting sys.argv.  Can I get access
> to the command line string as python saw it before it split it into
> sys.argv or is there another way?

I can't see the problem, sys.argv[1:] gives you the list of ordered 
key/value pairs... give an example of what you want and what you get.



More information about the Python-list mailing list