passing arguments like -JOB

Duncan Booth duncan.booth at invalid.invalid
Thu Feb 10 08:46:18 EST 2005


John Leslie wrote:

> I am porting a script from Korn Shell to python and want to pass named
> parameters like -JOB 123456 -DIR mydir
> 
> I can get it to work passing --JOB and --DIR but not -JOB and -DIR
> 
> Any ideas?
> 
Unfortunately (for you), I think you will find most or all of the existing 
ways to parse command line options in Python follow the POSIX convention 
for arguments, i.e. single letter options are introduced with a single - 
and multi-letter options are introduced with --.

My guess is that if you can't change whatever generates the command lines 
to conform to this convention you will have to write your own code to do 
the processing.

Alternatively you might get by by hacking the command line in your Python 
code:

   for i, opt in enumerate(sys.argv):
       if opt in ('-JOB','-DIR', '-ERR', '-GRP', '-TST', '-JNM', '-DAT'):
          sys.argv[i] = '-'+opt

   ... and then use getopt or optparse here ...

It isn't pretty, but so long as those values don't turn up elsewhere in the 
command line it ought to work.



More information about the Python-list mailing list