You are not logged in.

#1 2009-09-26 08:49:54

SpeedVin
Member
From: Poland
Registered: 2009-04-29
Posts: 955

[Solved] Write a --help for Python program.

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

#2 2009-09-26 09:06:58

SpeedVin
Member
From: Poland
Registered: 2009-04-29
Posts: 955

Re: [Solved] Write a --help for Python program.

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

#3 2009-09-26 09:10:47

genisis300
Member
From: Uk
Registered: 2008-01-15
Posts: 284

Re: [Solved] Write a --help for Python program.

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

#4 2009-09-26 09:39:28

SpeedVin
Member
From: Poland
Registered: 2009-04-29
Posts: 955

Re: [Solved] Write a --help for Python program.

genisis300 wrote:

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

#5 2009-09-26 09:52:17

Barrucadu
Member
From: York, England
Registered: 2008-03-30
Posts: 1,158
Website

Re: [Solved] Write a --help for Python program.

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

#6 2009-09-26 09:54:46

SpeedVin
Member
From: Poland
Registered: 2009-04-29
Posts: 955

Re: [Solved] Write a --help for Python program.

Barrucadu wrote:

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. wink


Shell Scripter | C/C++/Python/Java Coder | ZSH

Offline

#7 2009-09-26 10:08:56

g0ju
Member
From: Chemnitz, Germany
Registered: 2009-08-08
Posts: 23

Re: [Solved] Write a --help for Python program.

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

#8 2009-09-26 10:16:40

kumyco
Member
From: somewhere
Registered: 2008-06-23
Posts: 153
Website

Re: [Solved] Write a --help for Python program.

so anyway...
The syntax error is actually on line 5, missing `)`

Offline

#9 2009-09-26 12:26:48

SpeedVin
Member
From: Poland
Registered: 2009-04-29
Posts: 955

Re: [Solved] Write a --help for Python program.

g0ju wrote:

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 wink

kumyco wrote:

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

#10 2009-09-26 17:10:05

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: [Solved] Write a --help for Python program.

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

#11 2009-09-27 09:08:32

SpeedVin
Member
From: Poland
Registered: 2009-04-29
Posts: 955

Re: [Solved] Write a --help for Python program.

Dusty wrote:

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 wink


Shell Scripter | C/C++/Python/Java Coder | ZSH

Offline

Board footer

Powered by FluxBB