473,545 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

optparse multiple arguments

Hi,

I'm having some minor problems with optparse. I'm just worried that
someone shouldn't say that multiple argument feature isn't implemented
in optpartse.

How to tackle multiple arguments to an option ?
As far as I dug, I've found that >1 arguments are being ignored.

parser.add_opti on("", "--my-option", dest="my_option ", action="store",
type="string")

Now if someone uses it as:
../foo --my-option a b c

I want somehow to store all the three arguments but my_option stores
only "a" while ignoring b and c.

Any help?

Ritesh

Jun 30 '06 #1
8 9929

Ritesh Raj Sarraf wrote:
Hi,

I'm having some minor problems with optparse. I'm just worried that
someone shouldn't say that multiple argument feature isn't implemented
in optpartse.

How to tackle multiple arguments to an option ?
As far as I dug, I've found that >1 arguments are being ignored.

parser.add_opti on("", "--my-option", dest="my_option ", action="store",
type="string")

Now if someone uses it as:
./foo --my-option a b c

I want somehow to store all the three arguments but my_option stores
only "a" while ignoring b and c.


I just noticed that the args variable is holding values b and c.
the args variables comes from:
(options, args) = parser.parse_ar gs()

I guess I only need to figure out now is why args isn't storing
argument "a" also...

Ritesh

Jun 30 '06 #2

Ritesh Raj Sarraf wrote:
I just noticed that the args variable is holding values b and c.
the args variables comes from:
(options, args) = parser.parse_ar gs()

I guess I only need to figure out now is why args isn't storing
argument "a" also...

Ritesh


I fixed it, I guess.

parser.add_opti on("", "--my-option", dest="my_option ",
action="store_t rue")

sets my_option to True and the arguments are all stored in the list
"args". :-)

Ritesh

Jun 30 '06 #3
Ritesh Raj Sarraf wrote:
I guess I only need to figure out now is why args isn't storing
argument "a" also...


I fixed it, I guess.

parser.add_opti on("", "--my-option", dest="my_option ",
action="store_t rue")

sets my_option to True and the arguments are all stored in the list
"args". :-)


note that whatever action you're using, args holds the *remaining* arguments, after
option processing.

</F>

Jun 30 '06 #4

Ritesh Raj Sarraf wrote:
Ritesh Raj Sarraf wrote:
I just noticed that the args variable is holding values b and c.
the args variables comes from:
(options, args) = parser.parse_ar gs()

I guess I only need to figure out now is why args isn't storing
argument "a" also...

Ritesh


I fixed it, I guess.

parser.add_opti on("", "--my-option", dest="my_option ",
action="store_t rue")

sets my_option to True and the arguments are all stored in the list
"args". :-)

Ritesh


It might do you good to read the documentation instead of blindly
experimenting.

Anyway,

parser.add_opti on("", "--my-option", nargs=3)

http://docs.python.org/lib/optparse-...n-actions.html

Jun 30 '06 #5

Simon Percivall wrote:

It might do you good to read the documentation instead of blindly
experimenting.

Anyway,

parser.add_opti on("", "--my-option", nargs=3)

http://docs.python.org/lib/optparse-...n-actions.html


That won't help because by design of my program, I can't limit the
number of arguments a user can pass to it.

Ritesh

Jun 30 '06 #6
Ritesh Raj Sarraf wrote:
http://docs.python.org/lib/optparse-...n-actions.html


That won't help because by design of my program, I can't limit the
number of arguments a user can pass to it.


do you want options, arguments, or are you just somewhat confused ?

</F>

Jun 30 '06 #7

Fredrik Lundh wrote:
Ritesh Raj Sarraf wrote:
http://docs.python.org/lib/optparse-...n-actions.html


That won't help because by design of my program, I can't limit the
number of arguments a user can pass to it.


do you want options, arguments, or are you just somewhat confused ?

</F>


I'm not sure about that.

My program will accept user input. The user can input multiple
arguments. maybe 1 or maybe 100, that's not definite. I do need option
because this particular (multiple) argument criteria is for one single
option.

Thanks,
Ritesh

Jun 30 '06 #8
Ritesh Raj Sarraf wrote:
Now if someone uses it as:
./foo --my-option a b c

I want somehow to store all the three arguments


Currently optparse doesn't support options with an arbitrary number of
arguments. I'm currently working on a optparse-inspired replacement
which does support this along the lines of:

parser.add_opti on('--my-option', nargs='*')

but it's not quite ready for prime-time yet. I'm hoping to put an
announcement out in the next couple of weeks.

STeVe
Jul 1 '06 #9

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

Similar topics

8
2297
by: Hans-Joachim Widmaier | last post by:
I was really pleased when the Optik module found its way into the standard Python battery compartment, as it matched all my option parsing requirements. But, as always, there's really nothing that does all you ever want, especially when it comes to option parsing - there's just too many schemes to handle them all comfortably. With this...
7
2778
by: Henry Ludemann | last post by:
I've been writing an optparse alternative (using getopt) that is at a stage where I'd be interested in people's opinions. It allows you to easily creating command line interfaces to existing functions, using flags (which are optional) and arguments. It will automatically print a nicely formatted usage (eg: -h or --help), and easily &...
3
2455
by: Tomi Silander | last post by:
Hi, this must have been asked 1000 times (or nobody is as stupid as me), but since I could not find the answer, here is the question. My program mitvit.py: -------------- import optparse optparse.OptionParser().parse_args() -------------- gives me
3
2794
by: Bob | last post by:
I'd like to setup command line switches that are dependent on other switches, similar to what rpm does listed below. From the grammar below we see that the "query-options" are dependent on the query switch, {-q|--query}. Can "optparse" do this or do I have to code my own "thing"? Thanks. QUERYING AND VERIFYING PACKAGES: rpm {-q|--query} ...
0
1131
by: Steven Bethard | last post by:
I feel like I must be reinventing the wheel here, so I figured I'd post to see what other people have been doing for this. In general, I love the optparse interface, but it doesn't do any checks on the arguments. I've coded something along the following lines a number of times: class OptionArgParser(optparse.OptionParser): def...
4
431
by: rick | last post by:
Consider the following piece of code: parser = optparse.OptionParser(usage="usage: %prog <input filename> <output filename", add_help_option=False) parser.add_option("-d", type="string", action="store", dest="DELIM", default="|", help="single character delimiter in quotes (default: |)") (options, args) = parser.parse_args() Is there...
4
1638
by: Mathias Waack | last post by:
We've integrated python into a legacy application. Everything works fine (of course because its python;). There's only one small problem: the application reads the commandline and consumes all arguments prefixed with a '-' sign. Thus its not possible to call a python module from the commandline with a parameter list containing options prefixed...
2
2899
by: braver | last post by:
Posted to the Optik list, but it seems defunct. Optik is now Python's optparse. I wonder how do you implement optional arguments to Optik. I.e., you can have an option -P -- the filename is optional, with a default "data,pikl". It works as follows:
10
1406
by: James | last post by:
Hi, I would like to know your thoughts on a proposed change to optparse that I have planned. It is possible to add default values to multiple options using the set_defaults. However, when adding descriptions to options the developer has to specify it in each add_option() call. This results in unreadable code such as: ...
0
7410
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7437
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7773
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5984
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4960
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.