getopt and options with multiple arguments
Steven D'Aprano
steve at REMOVETHIScyber.com.au
Mon Dec 19 07:07:15 EST 2005
On Mon, 19 Dec 2005 02:29:41 -0800, pinkfloydhomer at gmail.com wrote:
> I want to be able to do something like:
>
> myscript.py * -o outputfile
>
> and then have the shell expand the * as usual, perhaps to hundreds of
> filenames.
If you are calling this from most Linux and Unix shells, the shell will
already have expanded the * and myscript.py will never see the asterisk.
> But as far as I can see, getopt can only get one argument
> with each option.
The getopt module has two main functions you probably want to call.
getopt.getopt expects options like -o arg followed by any remaining args.
getopt.gnu_getopt allows mixed args and options.
But frankly, the easiest way of dealing with your problem is just to
change your user-interface. What is wrong with using
myscript.py -o outputfile *
instead?
> In the above case, there isn't even an option string
> before the *, but even if there was, I don't know how to get getopt to
> give me all the expanded filenames in an option.
If you use a shell that doesn't expand the filenames, you can use the glob
module to expand it yourself.
--
Steven.
More information about the Python-list
mailing list