getopt: Make argument mandatory

F

Frans Englich

Hello,

In my use of getopt.getopt, I would like to make a certain parameter
mandatory. I know how to specify such that a parameter must have a value if
it's specified, but I also want to make the parameter itself
mandatory(combined with a mandatory value, the result is that the user must
specify a value).

Here's code illustrating my point(see the TODO):

# Parse command line flags
# --------------------------------------------
configurationFile = ""
try:

opts, args = getopt.getopt( argv, "hVc:", ["help", "version",
"configuration-file="])
for opt,arg in opts:
if opt in ("-c", "--configuration-file"):
configurationFile = arg

if opt in ("-h", "--help"):
usage()

if opt in ("-V", "--version"):
print "Version:", common.version["Runner"]
sys.exit()

# TODO Can't getopt make an argument mandatory?
if configurationFile == "":
print "You must pass an URL/path to a configuration file, see
--help."
sys.exit(common.exitCodes["parameter"])

except getopt.GetoptError:
usage()
# --------------------------------------------


Is it possible?


Cheers,

Frans
 
D

Diez B. Roggisch

In my use of getopt.getopt, I would like to make a certain parameter
mandatory. I know how to specify such that a parameter must have a value
if it's specified, but I also want to make the parameter itself
mandatory(combined with a mandatory value, the result is that the user
must specify a value).


Use optparse - in the examples section you'll find what you need.
 
M

Marc 'BlackJack' Rintsch

Frans Englich said:
Hello,

In my use of getopt.getopt, I would like to make a certain parameter
mandatory.

Isn't a *mandatory option* a contradiction? Why don't you turn it into an
argument? You already called it argument in the subject of your post.

Ciao,
Marc 'BlackJack' Rintsch
 
F

Frans Englich

Isn't a *mandatory option* a contradiction? Why don't you turn it into an
argument? You already called it argument in the subject of your post.

I probably used an inconsisten wording; option equals argument. My point is to
make an option/parameter/argument/call-it-what-you-like mandatory. When that
particular option/argument then also has a value(instead of a simple toggle),
that means a user specified value must always be specified.


Cheers,

Frans
 
M

Marc 'BlackJack' Rintsch

Frans Englich said:
I probably used an inconsisten wording; option equals argument. My point is to
make an option/parameter/argument/call-it-what-you-like mandatory. When that
particular option/argument then also has a value(instead of a simple toggle),
that means a user specified value must always be specified.

I had the following in mind:

http://www.python.org/doc/2.4/lib/optparse-what-options-for.html

Although the optparse documentation discourages `required options` there
are two examples in the source distribution how to implement them with the
optparse module.

Ciao,
Marc 'BlackJack' Rintsch
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top