473,395 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

getopt: Make argument mandatory


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

Jul 18 '05 #1
6 3678
> 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.

--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
In <ma**************************************@python.o rg>, Frans Englich
wrote:

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
Jul 18 '05 #3
On Wednesday 15 December 2004 20:12, Marc 'BlackJack' Rintsch wrote:
In <ma**************************************@python.o rg>, Frans Englich

wrote:
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.


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

Jul 18 '05 #4
In <ma**************************************@python.o rg>, Frans Englich
wrote:
> 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.


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/op...tions-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
Jul 18 '05 #5
On Wednesday 15 December 2004 14:07, Diez B. Roggisch wrote:
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.


I'm slow here, do you mean in of the callback examples in 6.21.4?

http://docs.python.org/lib/module-optparse.html
Cheers,

Frans

Jul 18 '05 #6
Frans Englich wrote:
On Wednesday 15 December 2004 14:07, Diez B. Roggisch wrote:
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.

I'm slow here, do you mean in of the callback examples in 6.21.4?

http://docs.python.org/lib/module-optparse.html


No, try here - this section seems to have been deleted from the Python 2.4 docs!
http://www.python.org/doc/2.3.4/lib/...-examples.html

Kent
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: David Bear | last post by:
I'm stumped. Trying to follow the docs and .. failure. here's the args >>> args '-Middwb@mainex1.asu.edu -AKHAAM@prlinux+898 -CA --D2003-08-20-09:28:13.417 -Ff -Hprlinux...
4
by: Dan Rawson | last post by:
Is there any way to force getopt to process one option first?? I'd like to be able to pass in the name of a configuration file for my application, then have the remaining command-line parameters...
3
by: Don Low | last post by:
Hi, I'm going over a script that demonstrates the getopt function. I include the script here: #! /usr/bin/python import sys, getopt, string
1
by: M.N.A.Smadi | last post by:
hi; I have a perl script that I need to port to python. The script takes input from the command line. Is there a standard way of processing command line arguments based on the -flag preceeding...
14
by: José de Paula | last post by:
Is getopt() and its companions, commonly found in GNU libc and other Unices libc, part of the C standard? Another doubt: I have a switch inside a while loop; is there a way to break out of the...
2
by: Praveen | last post by:
Hi, Any one ever used getopt function more than once. Here is my example. I have to run a process as myProcess -x -y -c "-a -f myFile -o myOutput -d debugFile" First I have to check for -c...
4
by: pinkfloydhomer | last post by:
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. But as far as I can see, getopt can only...
1
by: Daniel Mark | last post by:
Hello all: I am using getopt package in Python and found a problem that I can not figure out an easy to do . // Correct input D:\>python AddArrowOnImage.py --imageDir=c:/ // Incorrect...
20
by: Casey | last post by:
Is there an easy way to use getopt and still allow negative numbers as args? I can easily write a workaround (pre-process the tail end of the arguments, stripping off any non-options including...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.