command line arguments (newbie...)

M

Matteo Risoldi

Hello,

I could not find the answer to the following through a search, so I thought
I could ask here. Thanks to everyone who would try and give a suggestion.
In this program I did, I am parsgin command line arguments through the
getopt.getopt function in the following way (note: try/except and other
non-relevant stuff has been omitted for clarity). Note that thr "argv"
parameter of main is actually sys.argv[1:].

def main(argv):
opts, args=getopt.getopt(argv, "f:q:d:vro:t:h",
["file=","query=","database=","verbose","reformat","outfile=","tabulate=","h
elp"])
....
....
for opt, arg in opts:
if opt in ("-t","--tabulate"):
separator=arg


This lets me assign the parameter of the "-t" argument to the "separator"
variable.
Still, I would like to be able to use the "-t" argument with OR without a
parameter, i.e. if I specify something after -t, it should go into
"separator", but if I don't specify anything, "separator" should keep its
default value (which is set elsewhere).
Now, the problem is that with getopt.getopt, if I put "t:"in the option
list, then I HAVE to put something after it; if I put only a "-t" on the
command line, I get a getopt.GetoptError exception. I have tried putting
both "t:" and "t" in the option list, but this won't work. At first I had
thought about testing "if arg" and only then assigning a value to separator,
but as I said the "-t" argument without parameters would not even be
accepted.

Any suggestion on how to achieve this?

Thank you very much.

Cheers.
Matteo
 
V

Ville Vainio

Matteo Risoldi said:
In this program I did, I am parsgin command line arguments through the
getopt.getopt function in the following way (note: try/except and other

I can say this w/o reading the rest of your mail: don't do it, use
optparse instead.
 
M

Matteo Risoldi

Ville Vainio said:
I can say this w/o reading the rest of your mail: don't do it, use
optparse instead.

I knew optparse, and that would be nice, but I forgot to say that I have to
stick with 2.2 (it's the version installed everywhere here and in the
centers which will use this script, and I can't expect hundreds of people to
upgrade their python to 2.3).

Any other try?

Thanks
M.
 
P

P

Matteo said:
Hello,

Still, I would like to be able to use the "-t" argument with OR without a
parameter, i.e. if I specify something after -t, it should go into
"separator", but if I don't specify anything, "separator" should keep its
default value (which is set elsewhere).
Now, the problem is that with getopt.getopt, if I put "t:"in the option
list, then I HAVE to put something after it; if I put only a "-t" on the
command line, I get a getopt.GetoptError exception. I have tried putting
both "t:" and "t" in the option list, but this won't work. At first I had
thought about testing "if arg" and only then assigning a value to separator,
but as I said the "-t" argument without parameters would not even be
accepted.

Any suggestion on how to achieve this?

man 3 getopt suggests t:: should work.
If it doesn't then it's another broken python reimplementation of a
standard library:
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&[email protected]

Pádraig.
 
V

Ville Vainio

Matteo Risoldi said:
I knew optparse, and that would be nice, but I forgot to say that I have to
stick with 2.2 (it's the version installed everywhere here and in the
centers which will use this script, and I can't expect hundreds of people to
upgrade their python to 2.3).

How about downloading Optik, which is "optparse for 2.2"?
 
M

Matteo Risoldi

Ville Vainio said:
How about downloading Optik, which is "optparse for 2.2"?

Yes, I just realized the optparse was pre-existing as Optik - I didn't know
it, I am quite new to Python! :)

Well, I believe in the end I'll just do without the default value for the -t
option, it's not important enough to justify users having to upgrade Python
or download a module. Anyway, optparse is a great addition to 2.3, I can see
lots of good reasons for having it inside.

Thank you.

Bye
M.
 
F

Fredrik Lundh

man 3 getopt suggests t:: should work.
If it doesn't then it's another broken python reimplementation of a
standard library:

if you're going to refer to standards, maybe you should read the
standard:

http://www.opengroup.org/onlinepubs/007908799/xsh/getopt.html

"The argument optstring is a string of recognised option characters;
if a character is followed by a colon, the option takes an argument.
All option characters allowed by Utility Syntax Guideline 3 are allowed
in optstring. The implementation may accept other characters as an
extension."

where Utility Syntax Guideline 3 says:

"Each option name should be a single alphanumeric character (the
alnum character classification) from the portable character set. Multi-
digit options are not allowed."

and Utility Syntax Guideline 7 says:

"Option-arguments should not be optional."

</F>
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top