473,765 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getopt issues


I'm stumped. Trying to follow the docs and .. failure.

here's the args
args '-******@mainex1. asu.edu -AKHAAM@prlinux+ 898 -CA --D2003-08-20-09:28:13.417 -Ff -Hprlinux --JTCPIP_NPF_TEST _DARS_PORTRAIT -NTCPIP_NPF_TEST _DARS_PORTRAIT --Pholdqueue -Qholdqueue -aacct -b2895 -d/var/spool/lpd/holdqueue -edfA898prlinux -fTCPIP_NPF_TEST _DARS_PORTRAIT -hprlinux -j898 -kcfA898prlinux -l66 -nKHAAM -sstatus -t2003-08-20-09:28:13.000 -w80 -x0 -y0 acct \n'
I know, they're long. sorry. they just are. and maybe thats the
bug.

here's what happens
a, b = getopt.getopt(a rgs, 'APQn')
a [] b

'-******@mainex1. asu.edu -AKHAAM@prlinux+ 898 -CA --D2003-08-20-09:28:13.417 -Ff -Hprlinux --JTCPIP_NPF_TEST _DARS_PORTRAIT -NTCPIP_NPF_TEST _DARS_PORTRAIT --Pholdqueue -Qholdqueue -aacct -b2895 -d/var/spool/lpd/holdqueue -edfA898prlinux -fTCPIP_NPF_TEST _DARS_PORTRAIT -hprlinux -j898 -kcfA898prlinux -l66 -nKHAAM -sstatus -t2003-08-20-09:28:13.000 -w80 -x0 -y0 acct \n'
not at all what the docs lead me to believe. Is getopt buggy?

I'm on python 2.2.2 on linux but will also be using python 2.1.3 on
FreeBsd.

Advice?

--
David Bear
phone: 480-965-8257
fax: 480-965-9189
College of Public Programs/ASU
Wilson Hall 232
Tempe, AZ 85287-0803
"Beware the IP portfolio, everyone will be suspect of trespassing"
Jul 18 '05 #1
6 2789
[David Bear]
I'm stumped. Trying to follow the docs and .. failure.

here's the args
args '-******@mainex1. asu.edu -AKHAAM@prlinux+ 898 -CA --D2003-08-20-09:28:13.417 -Ff
-Hprlinux --JTCPIP_NPF_TEST _DARS_PORTRAIT -NTCPIP_NPF_TEST _DARS_PORTRAIT --Phold
queue -Qholdqueue -aacct -b2895 -d/var/spool/lpd/holdqueue -edfA898prlinux -fTCP
IP_NPF_TEST_DAR S_PORTRAIT -hprlinux -j898 -kcfA898prlinux -l66 -nKHAAM -sstatus
-t2003-08-20-09:28:13.000 -w80 -x0 -y0 acct \n'

I know, they're long. sorry. they just are. and maybe thats the
bug.

here's what happens
a, b = getopt.getopt(a rgs, 'APQn')
a [] b

'-******@mainex1. asu.edu -AKHAAM@prlinux+ 898 -CA --D2003-08-20-09:28:13.417 -Ff
-Hprlinux --JTCPIP_NPF_TEST _DARS_PORTRAIT -NTCPIP_NPF_TEST _DARS_PORTRAIT --Phold
queue -Qholdqueue -aacct -b2895 -d/var/spool/lpd/holdqueue -edfA898prlinux -fTCP
IP_NPF_TEST_DAR S_PORTRAIT -hprlinux -j898 -kcfA898prlinux -l66 -nKHAAM -sstatus
-t2003-08-20-09:28:13.000 -w80 -x0 -y0 acct \n'

not at all what the docs lead me to believe. Is getopt buggy?


Normally, it is prudent to suspect your own code first rather
than the module.

In this case, getopt is expecting a list for the args argument which
normally comes from sys.argv. Try this:

args = args.split()
a, b = getopt.getopt(a rgs, 'APQn')

That will get you closer.
The next step is define the missing codes like -M.
Raymond Hettinger


Jul 18 '05 #2
Quoth David Bear:
args '-******@mainex1. asu.edu -AKHAAM@prlinux+ 898 [...]

[...]
a, b = getopt.getopt(a rgs, 'APQn')
getopt wants its arguments as a list of strings (like sys.argv),
not one big string. Here, for example,
a, b = getopt.getopt(a rgs.split(), 'APQn')

Traceback (most recent call last):
...
getopt.GetoptEr ror: option -M not recognized

as expected.

--
Steven Taschuk o- @
st******@telusp lanet.net 7O )
" (

Jul 18 '05 #3
On Tue, 26 Aug 2003, Steven Taschuk wrote:
>> args '-******@mainex1. asu.edu -AKHAAM@prlinux+ 898 [...]

[...]
>> a, b = getopt.getopt(a rgs, 'APQn')
getopt wants its arguments as a list of strings (like sys.argv),
not one big string. Here, for example,


many thanks. I didn't think the 'list' of args that getopt wanted was a
python list.

now that I've read a little more of the getopt documentation, it says that
getopt stops processing when a 'nonoption' is encounted. Is there a way
to have it process the whole argument list, then only return the options
specified in options, rather than have it through an exception and return
nothing?
>>> a, b = getopt.getopt(a rgs.split(),

'APQn') > Traceback (most recent call last): ...
getopt.GetoptEr ror: option -M not recognized

as expected.


Jul 18 '05 #4
Quoth David Bear:
[...]
now that I've read a little more of the getopt documentation, it says that
getopt stops processing when a 'nonoption' is encounted. Is there a way
to have it process the whole argument list, then only return the options
specified in options, rather than have it through an exception and return
nothing?


I don't think so. This is an unusual request -- normally it is
desirable for a program to insist on correct usage, rather than
trying to guess what the user meant. (Imagine, for example, that
a user types 'rm -I *', intending 'rm -i *', and rm just ignores
the unknown option '-I'. This would be bad.)

It might, however, be possible to subclass optparse.Option Parser
to get the behaviour you want. (The optparse module is new in the
2.3 stdlib.)

--
Steven Taschuk st******@telusp lanet.net
"Telekinesi s would be worth patenting." -- James Gleick

Jul 18 '05 #5
David Bear:
Still, I think getopt is rather weak. It would be better if I could build
a dictionary of options that I wanted, then when parsing the args, just
fill in my dictionary values from args that I want.


See the optparse modules in the new 2.3 distribution, at
http://www.python.org/doc/2.3/lib/module-optparse.html

from optparse import OptionParser

parser = OptionParser()
parser.add_opti on("-f", "--file", dest="filename" ,
help="write report to FILE", metavar="FILE")
parser.add_opti on("-q", "--quiet",
action="store_f alse", dest="verbose", default=True,
help="don't print status messages to stdout")

(options, args) = parser.parse_ar gs()
Andrew
da***@dalkescie ntific.com
Jul 18 '05 #6
David Bear <id***@asu.ed u> wrote in message news:<Pi******* *************** *************** *@moroni.pp.asu .edu>...
good points. yet, my purpose is to write a filter. the problem being
that with different lpr implementations you don't know exactly what
parameters will be passed since lpr is not really standardize (at least
thats my understanding). I'm just interested in two parameters out of
possibly many.
Can't you specify all the possible parameters and collect only those you
want, or are you dealing with conflicting specifications of certain options?
Still, I think getopt is rather weak. It would be better if I could build
a dictionary of options that I wanted, then when parsing the args, just
fill in my dictionary values from args that I want.


As an aside, since it doesn't deal with cases where the option is
concatenated with the following parameter, you might find cmdsyntax
interesting since it aims to populate dictionaries with appropriately
named entries for the command line input:

http://www.boddie.org.uk/david/Proje...tax/index.html

It will also try and parse the arguments and return a list of failed
matches if requested.

However, it may well be overkill for your purposes.

David
Jul 18 '05 #7

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

Similar topics

4
2296
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 over-ride the configuration file if they are present. For the moment, I'm restricted to 2.2 versions. TIA . . . . Dan
3
2319
by: Dominik Kaspar | last post by:
I tried to use getopt and copied the example from: http://www.python.org/doc/current/lib/module-getopt.html but nothing is working... getopt.GetoptError doesn't seem to exist and when i run the program commenting this out, none of "-v", "-h" and "-o" wants to be recognized... what's the matter?! Dominik
3
7029
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
6
3695
by: Frans Englich | last post by:
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):
14
3872
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 loop from the switch without using goto? I mean: start: while(chr = fgetc(inputfile)) { switch(chr)
4
5676
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 get one argument with each option. In the above case, there isn't even an option string before the *, but even if there was, I don't know how to get getopt to give me all the expanded filenames in an option.
1
2311
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 input D:\>python AddArrowOnImage.py --imageDi
2
6257
by: auditory | last post by:
I have sources written on linux quite long ago. They are compiled good on current linux machine. but not in VS2005. The cause of problem is #include<getopt.h>. Is there any correspoinding files which can be used instead of getopt.h?? I tried xgetopt.h from codeproject http://www.codeproject.com/cpp/xgetopt.asp
0
1231
by: nispe | last post by:
Hi, This is probably a really easy one but its got me; Im using getopt() and having some issues geting the specified options into a form where I can use them. Here a sample of the code Im using which compiles fine: int main (int argc, char *argv) { char *number_games = NULL; int NUMBER_GAMES = 0;
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9399
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7379
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
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
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.