473,382 Members | 1,380 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,382 software developers and data experts.

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.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
import getopt, sys

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

def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help",
"output="])
except: # getopt.GetoptError:
# 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 2295
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.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?!


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******@student.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
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...
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
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...
1
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...
18
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. ...
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...
2
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...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.