optparse

R

rick

Consider the following piece of code:

parser = optparse.OptionParser(usage="usage: %prog <input filename>
<output filename> [options]", add_help_option=False)
parser.add_option("-d", type="string", action="store", dest="DELIM",
default="|", help="single character delimiter in quotes (default: |)")
(options, args) = parser.parse_args()


Is there any way I can add help for the arguments (<input filename> and
<output filename>) in the same table that optparse generates above the
options section. Ideally, I would like the output as:

======================================================
usage: DelimTOFixedWidth.py <input filename> <output filename> [options]

required:
input filename name of the delimited input file
output filename name of fixed-width output file

options:
-d DELIM single character delimiter in quotes (default: |)
======================================================

Is that possible using optparse?

Thanks.
 
B

Ben Finney

rick said:
usage: DelimTOFixedWidth.py <input filename> <output filename> [options]

That's not the command-line argument style that optparse models. It
should be:

command_name [options] <arg1> <arg2>

In other words, the options (optionally) appear before the non-option
arguments. This is the convention followed by most command-line tools,
and it's what users expect.

There are other command-line parser modules (e.g. argparse), perhaps
you can look to them for different functionality.
 
R

rick

OK, using that convention, how would I create help for <arg1>, <arg2>, etc.

Thanks.

rick said:
usage: DelimTOFixedWidth.py <input filename> <output filename> [options]

That's not the command-line argument style that optparse models. It
should be:

command_name [options] <arg1> <arg2>

In other words, the options (optionally) appear before the non-option
arguments. This is the convention followed by most command-line tools,
and it's what users expect.

There are other command-line parser modules (e.g. argparse), perhaps
you can look to them for different functionality.
 
S

Steven Bethard

rick said:
Consider the following piece of code:

parser = optparse.OptionParser(usage="usage: %prog <input filename>
<output filename> [options]", add_help_option=False)
parser.add_option("-d", type="string", action="store", dest="DELIM",
default="|", help="single character delimiter in quotes (default: |)")
(options, args) = parser.parse_args()


Is there any way I can add help for the arguments (<input filename> and
<output filename>) in the same table that optparse generates above the
options section. Ideally, I would like the output as:

======================================================
usage: DelimTOFixedWidth.py <input filename> <output filename> [options]

required:
input filename name of the delimited input file
output filename name of fixed-width output file

options:
-d DELIM single character delimiter in quotes (default: |)
======================================================

Is that possible using optparse?

Not easily, but that's exactly what the argparse_ module is for::
usage: tasks.py [-d DELIM] input_filename output_filename

positional arguments:
input_filename name of the delimited input file
output_filename name of fixed-width output file

optional arguments:
-d DELIM character delimiter in quotes (default: |)

Note that argparse_ also figures out the correct usage message for you.

... _argparse: http://argparse.python-hosting.com/

STeVe
 
T

Thorsten Kampe

Don't top-post or other people will laugh at you and call you names...

* rick (Wed, 27 Sep 2006 11:40:40 -0400)
OK, using that convention, how would I create help for <arg1>, <arg2>, etc.

Not possible. Use argparse.

Thorsten
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,734
Latest member
manin

Latest Threads

Top