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

Home Posts Topics Members FAQ

getopt, i don't get it

I tried to use getopt and copied the example from:
http://www.python.org/doc/current/li...le-getopt.html

but nothing is working... getopt.GetoptEr ror 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
import getopt, sys

def usage():
print "Use it like this: bla bla bla"

def main():
try:
opts, args = getopt.getopt(s ys.argv[1:], "ho:v", ["help",
"output="])
except: # getopt.GetoptEr ror:
# print help information and exit:
usage()
sys.exit(2)
output = None
verbose = False
for o, a in opts:
if o == "-v":
verbose = True
if o in ("-h", "--help"):
usage()
sys.exit()
if o in ("-o", "--output"):
output = a
# ...

if __name__ == "__main__":
main()
Jul 18 '05 #1
3 2319
Dominik Kaspar wrote:

I tried to use getopt and copied the example from:
http://www.python.org/doc/current/li...le-getopt.html

but nothing is working... getopt.GetoptEr ror 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?!


It seems likely you have another file called getopt.py (or perhaps
a leftover getopt.pyc from a previous time) in your Python path
(check all directories in sys.path, starting with current directory).

You can't safely reuse the names of standard library modules most
of the time...

-Peter
Jul 18 '05 #2
do******@studen t.ethz.ch (Dominik Kaspar) writes:
I tried to use getopt and copied the example from:
http://www.python.org/doc/current/li...le-getopt.html


If you haven't used getopt before, don't start now. Check out optparse
instead.

--
Ville Vainio http://www.students.tut.fi/~vainio24
Jul 18 '05 #3
> If you haven't used getopt before, don't start now. Check out optparse
instead.

Thanks for the tip, I have already wrote something with getopt, but I found
that the "add_option " of the optparse is cleaner and easier to read than the
list of options that you use with getopt. Other thing I liked is that you
don't
have to write a method to print the help, you just pass a descriptive text
to the parser and it will do the rest.

Really nice :-). I will try it.

Regards,
Josef
Jul 18 '05 #4

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

Similar topics

6
2789
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 --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...
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
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)
1
1559
by: Shaun Jackman | last post by:
I'd like to call getopt with one set of arguments, and once that's completely done call it again with an entirely different set of arguments. I've found though that at least with the getopt implementation common to both glibc and newlib, getopt has to be "cleaned up" in between. The glibc source code shows that to clean it up either optind must be set to zero or __getopt_initialized must be set to non-zero. The latter is clearly...
18
3239
by: k_over_hbarc | last post by:
What is the correct format of getopt() and how does it work? I looked at the man page; it doesn't really clarify things. What's a program that uses it, maybe I could figure it out from that. Note: This is on-topic. getopt is part of the standard library. Andrew Usher
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
20
5828
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 negative numbers into a separate sequence and ignore the (now empty) args list returned by getopt, but it would seem this is such a common requirement that there would be an option to treat a negative value as an argument. Note that this is only a...
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
10163
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9835
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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
5276
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
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
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.