Access to raw command line?

Bjoern Schliessmann usenet-mail-0306.20.chr0n0ss at spamgourmet.com
Thu Apr 26 06:52:01 EDT 2007


Pieter Edelman wrote:

> ./myprog.py -t *.gpx *.jpg
> 
> This seems like a sensible option at first sight, but it's
> difficult to implement because the wildcard is expanded by the
> shell, 

(in *nix only)

> so sys.argv gets a list containing "-t", all .gpx files and 
> all .jpg files. With this list, there's no way to tell which files
> belong to the "-t" switch and which are arguments (other than
> using the extension).
> 
> One possible way to work around this is to get the raw command
> line and do the shell expansions ourselves from within Python.
> Ignoring the question of whether it is worth the trouble, does
> anybody know if it is possible to obtain the raw (unexpanded)
> command line?

Breaking the "de-facto standards" isn't a good idea, IMHO.

> Alternatively,  does anybody have suggestion of how to do this in
> a clean way?

First, you could define another wildcard character, or let users
escape the * like this: ./myprog.py -t \*.gpx \*.jpg

Let users give a list of files to process, and default to looking
for the same filename without image extension and with ".gpx". 
E. g.:

./myprog.py -t .gpx *.jpg

will expand to:

./myprog.py -t .gpx a.jpg b.jpg [...] z.jpg

What your program's "policy" would be depends on its exact function.
I'm thinking about if the program silently fails if it doesn't find
a .gpx file for one .jpg, for example, or if it complains about it.

Regards,


Björn

-- 
BOFH excuse #380:

Operators killed when huge stack of backup tapes fell over.




More information about the Python-list mailing list