passing arguments like -JOB

J

John Leslie

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?

Current code :

try:
options, xarguments = getopt.getopt(sys.argv[1:], '', ['JOB=',
'DIR=', 'ERR=', 'GRP=', 'TST=', 'JNM=', 'DAT=',])
except getopt.error:
print 'Error: You tried to use an unknown option'
sys.exit(1)
JOB = ''
for o,a in options[:]:
print 'here2'
print o
print a
if o == '--JOB':
JOB = a
print JOB
 
D

Duncan Booth

John said:
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 = '-'+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.
 
J

John Leslie

Thanks...it worked perfectly.
Brilliant!!

JL


Duncan Booth said:
John said:
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 = '-'+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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,219
Messages
2,571,118
Members
47,737
Latest member
CarleyHarm

Latest Threads

Top