When handling command-line arguments, I roughly know getopt will work, but have never used it before. The documents on some books seems not very informative, if read multiple arguments from the command line, is it right to use sys.argv[1], sys.argv[2], etc to assign the arguments to respective variable in the program?
Thanks!
pythonlearning
0
Light Poster
Recommended Answers
Jump to PostYou should use optparse. There is an excellent tutorial here http://www.alexonlinux.com/pythons-optparse-for-human-beings . Also note that in recent versions of python, optparse has been replaced by a module named argparse, but I never used …
Jump to PostI just read the article but still have a couple of questions. In the article, "In this case parse_args() will place all arguments in a tuple. I.e. after we run parse_args(), opt.multi will be a tuple containing all arguments that user has passed to our program."
1. So if I …
Jump to PostThanks! I tried the following code, always got error message "invalid syntax", what is the probelm?
def main(args): import optparse parser = optparse.OptionParser() parser.add_option('-M', help='multiple arguments', dest='multi', \ action='store', nargs=3) (opts, args) = parser.parse_args() a=opt.multi[0] b=opt.multi[1] c=opt.multi[2] print "a=", a print "b=", b print "b=", b #execute upon …
Jump to PostSorry, I mean when I input the file name and some arguments in command-line mode, the error message is always "invalid syntax".
What I did was I saved the code to the python folder, input >>>commandline.py 1 2 3
then I got syntax error each time. Something …
Jump to PostOh. sorry, here is the traceback message,
>>> commandline.py -M 1 2 3
SyntaxError: invalid syntax
>>>We probably don't use the same code. Also normally the tracback shows the line where there is the syntax error when there is one. I attach a zip file with …
All 19 Replies
Gribouillis
1,391
Programming Explorer
Team Colleague
pythonlearning
0
Light Poster
pythonlearning
0
Light Poster
Gribouillis
1,391
Programming Explorer
Team Colleague
pythonlearning
0
Light Poster
Gribouillis
1,391
Programming Explorer
Team Colleague
pythonlearning
0
Light Poster
Gribouillis
1,391
Programming Explorer
Team Colleague
pythonlearning
0
Light Poster
Gribouillis
1,391
Programming Explorer
Team Colleague
pythonlearning
0
Light Poster
Gribouillis
1,391
Programming Explorer
Team Colleague
pythonlearning
0
Light Poster
Gribouillis
1,391
Programming Explorer
Team Colleague
pythonlearning
0
Light Poster
Gribouillis
1,391
Programming Explorer
Team Colleague
pythonlearning
0
Light Poster
Gribouillis
1,391
Programming Explorer
Team Colleague
pythonlearning
0
Light Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.