Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a help for python script

Tags:

python

I am trying to make my python script very user friendly, so I like to write some sort of help for it. What is your advise for this? I could just put in some logic that if the user passed help as a paramater to the script, they get help. Is there a best practise or convention for this?

like image 260
dublintech Avatar asked Jan 27 '12 17:01

dublintech


People also ask

How do you write a help function in Python?

You have to use Ctrl + I in front of an object's name to show their help in our Help pane.

How do I get help for a Python module?

Enter the name of any module, keyword, or topic to get help on writing Python programs and using Python modules. To quit this help utility and return to the interpreter, just type "quit". To get a list of available modules, keywords, or topics, type "modules", "keywords", or "topics".

How do I start a Python script?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!


1 Answers

Use argparse.

For example, with test.py:

import argparse  parser=argparse.ArgumentParser(     description='''My Description. And what a lovely description it is. ''',     epilog="""All is well that ends well.""") parser.add_argument('--foo', type=int, default=42, help='FOO!') parser.add_argument('bar', nargs='*', default=[1, 2, 3], help='BAR!') args=parser.parse_args() 

Running

% test.py -h 

yields

usage: test.py [-h] [--foo FOO] [bar [bar ...]]  My Description. And what a lovely description it is.      positional arguments:   bar         BAR!      optional arguments:   -h, --help  show this help message and exit   --foo FOO   FOO!      All is well that ends well. 
like image 189
unutbu Avatar answered Oct 18 '22 20:10

unutbu