You are not logged in.
Hello.
I write programs in Python and I don't know one thing.
How to write --help command for python program (like calc.py --help).
And after this command you will see help.
Anyone know how to do something like that?
Thanks
Last edited by SpeedVin (2009-09-26 16:53:14)
Shell Scripter | C/C++/Python/Java Coder | ZSH
Offline
Ok I found def function is there any other Ideas?
Last edited by SpeedVin (2009-09-26 09:07:10)
Shell Scripter | C/C++/Python/Java Coder | ZSH
Offline
you need to use a module called option parser
http://www.alexonlinux.com/pythons-optp … man-beings
if you still need help let me know and i post an example.
Regards
Matthew
"is adult entertainment killing our children or is killing our children entertaining adults?" Marilyn Manson
Offline
you need to use a module called option parser
http://www.alexonlinux.com/pythons-optp … man-beings
if you still need help let me know and i post an example.
Regards
Matthew
Thanks that awesome!.
But I added some arguments and when I try to run program I got:
File "calculator.py", line 7
(opts, args) = parser.parse_args()
^
SyntaxError: invalid syntax
Here is my code:
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 import optparse
4 parser = optparse.OptionParser()
5 parser.add_option('-h', '--help', help='Show this help message'
6
7 (opts, args) = parser.parse_args()
8 #
9 #def help():
10 # print "+ = plus"
11 # print "- = minus"
12 # print "* = multiplication"
13 # print "/ = division"
14 # print "// = floor division"
15 # End of function.
16 print "If you need help just write --help or -h."
17 number1 = int(raw_input('Write a number: '))
18 number2 = int(raw_input('Write a number nr.2: '))
19 znak = raw_input('What to do?')
20 if znak == '+':
21 print number1 + number2
22 elif znak == '-':
23 print number1 - number2
24 elif znak == '*':
25 print number1 * number2
26 elif znak == '/':
27 print number1 / number2
28 elif znak == '**':
29 print number1 ** number2
30 elif znak == '//':
31 print number1 // number2
32 else:
33 print "I don't know what to do with " + znak
34 #if raw_input() == help:
35 # help()
Last edited by SpeedVin (2009-09-26 09:40:26)
Shell Scripter | C/C++/Python/Java Coder | ZSH
Offline
I'd just use the sys module:
import sys
if "-h" in sys.argv or "--help" in sys.argv:
print "This is help text!"
Offline
I'd just use the sys module:
import sys if "-h" in sys.argv or "--help" in sys.argv: print "This is help text!"
Thanks Barrucadu I will try sys module too.
Shell Scripter | C/C++/Python/Java Coder | ZSH
Offline
OT: SpeedVin
I noticed that you were asking some python-related questions lately... maybe you should read a good book or tutorial about python.
Why? Because you could easily figure out the answers to your questions yourself if you'd know the basics of the language and the standard library.
Offline
so anyway...
The syntax error is actually on line 5, missing `)`
Offline
OT: SpeedVin
I noticed that you were asking some python-related questions lately... maybe you should read a good book or tutorial about python.
Why? Because you could easily figure out the answers to your questions yourself if you'd know the basics of the language and the standard library.
I read Byte-Python and I like to Experiment with my code , changing examples.
Hmm do you know any other good book for Python.
Bute-Python is awesome , but it will be nice to read other books about Python.
Thanks Barrucadu your solution works nice
so anyway...
The syntax error is actually on line 5, missing `)`
Thanks
Last edited by SpeedVin (2009-09-26 12:31:20)
Shell Scripter | C/C++/Python/Java Coder | ZSH
Offline
The Python Tutorial is a terrific resource, it really progresses from the basics to advanced in a clear, sensible order:
http://docs.python.org/tutorial/
I've also heard lots of good things about dive into python, though I can't personally recommend it as I haven't read it.
The real reason I'm answering this thread is a shameless plug. I got tired of option parser syntax, so I wrote a more pythonic version:
http://github.com/buchuki/opterator
Enjoy!
Dusty
Offline
The Python Tutorial is a terrific resource, it really progresses from the basics to advanced in a clear, sensible order:
http://docs.python.org/tutorial/
I've also heard lots of good things about dive into python, though I can't personally recommend it as I haven't read it.
The real reason I'm answering this thread is a shameless plug. I got tired of option parser syntax, so I wrote a more pythonic version:
http://github.com/buchuki/opterator
Enjoy!
Dusty
Thanks Dusty for link that is what I wanted and I will try your parser
Shell Scripter | C/C++/Python/Java Coder | ZSH
Offline