optparse

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rick

    #1

    optparse

    Consider the following piece of code:

    parser = optparse.Option Parser(usage="u sage: %prog <input filename>
    <output filename[options]", add_help_option =False)
    parser.add_opti on("-d", type="string", action="store", dest="DELIM",
    default="|", help="single character delimiter in quotes (default: |)")
    (options, args) = parser.parse_ar gs()


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

    =============== =============== =============== =========
    usage: DelimTOFixedWid th.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.
  • Ben Finney

    #2
    Re: optparse

    rick <rickzolun1@cha rter.netwrites:
    usage: DelimTOFixedWid th.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.

    --
    \ "If nature has made any one thing less susceptible than all |
    `\ others of exclusive property, it is the action of the thinking |
    _o__) power called an idea" -- Thomas Jefferson |
    Ben Finney

    Comment

    • rick

      #3
      Re: optparse

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

      Thanks.

      On 9/27/2006 12:44 AM, Ben Finney wrote:
      rick <rickzolun1@cha rter.netwrites:
      >
      >usage: DelimTOFixedWid th.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.
      >

      Comment

      • Steven Bethard

        #4
        Re: optparse

        rick wrote:
        Consider the following piece of code:
        >
        parser = optparse.Option Parser(usage="u sage: %prog <input filename>
        <output filename[options]", add_help_option =False)
        parser.add_opti on("-d", type="string", action="store", dest="DELIM",
        default="|", help="single character delimiter in quotes (default: |)")
        (options, args) = parser.parse_ar gs()
        >
        >
        Is there any way I can add help for the arguments (<input filenameand
        <output filename>) in the same table that optparse generates above the
        options section. Ideally, I would like the output as:
        >
        =============== =============== =============== =========
        usage: DelimTOFixedWid th.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::
        >>import argparse
        >>parser = argparse.Argume ntParser(add_he lp=False)
        >>parser.add_ar gument('-d', dest='DELIM', default='|',
        ... help='character delimiter in quotes (default: %(default)s)')
        >>parser.add_ar gument('input_f ilename',
        ... help='name of the delimited input file')
        >>parser.add_ar gument('output_ filename',
        ... help='name of fixed-width output file')
        >>parser.print_ help()
        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

        Comment

        • Thorsten Kampe

          #5
          Re: optparse

          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

          Comment

          Working...