Pieter Edelman wrote: > Hi, > > I'm currently writing a command-line program in Python, which takes > commands in the form of: > ./myprog.py [OPTIONS] ARGS > So pretty standard stuff. In my case, ARGS is a list of image files. > > One of the possible options is to specify a file holding information > about the photos. You'd specify it with (in this particular case) the - > t switch, and you can specify multiple files by repeating this switch: > ./myprog.py -t info1.gpx -t info2.gpx -t info3.gpx *jpg > > Now, one of the users has quite a lot of info files, and asked me if > it's possible to use a wildcard in specifying these, so he would just > have to do: > ./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, 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? > Alternatively, does anybody have suggestion of how to do this in a > clean way?
One option would be to accept a directory argument to -t and to recursively inlcude all the gpx files in that directory. Another option would be to use some sort of optional separator, like ./myprog.py -t *.gpx -j *.jpg where everything between -t and -j would be considered a -t option. I think this breaks the usual UNIX options paradigm though. Of course UNIX itself does that--dd is an example of a program that doesn't follow it. Another option would be to accept a list of gpx files from a file: find . -name '*.gpx' > gpxlist.txt ./myprog.py --gpx-file=gpxlist.txt *.jpg Or to accept more than one file per argument: ./myprog.py -t "$(echo *.gpx)" *.jpg Personally I would prefer the file list approach. If you start expanding wildcards yourself, then if someone stupidly wants to use a wildcard character, they'll have to triple-escape things, and it'll just be messy. -- Michael Hoffman -- http://mail.python.org/mailman/listinfo/python-list