[RFC] use optparse for option handling

John Arbash Meinel john at arbash-meinel.com
Wed Jul 12 20:52:16 BST 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Aaron Bentley wrote:
> Aaron Bentley wrote:
>>> Here's a bundle that uses optparse as a backend.
> 
> Here's a follow-on to use optpparse for generating our option help.
> 
> Aaron

...

=== modified file 'bzrlib/commands.py'
- --- bzrlib/commands.py	2006-07-12 18:29:14 +0000
+++ bzrlib/commands.py	2006-07-12 19:18:47 +0000
@@ -367,12 +367,12 @@
             option_strings.append('-%s' % short_name)
         optargfn = option.type
         if optargfn is None:
- -            parser.add_option(action='store_true', dest=name,
+            parser.add_option(action='store_true', dest=name,
help=option.help,
                               default=OptionParser.DEFAULT_VALUE,
                               *option_strings)
         else:
             parser.add_option(action='callback', callback=type_callback,
- -                              type='string',
+                              type='string', help=option.help,
                               default=OptionParser.DEFAULT_VALUE,
                               callback_args=(name, optargfn),
*option_strings)
     return parser


As an aside, it might be nicer if we could detect a few argument types,
and pass them to the optparse parser, rather than using callback. But
that can be something in the future.



=== modified file 'bzrlib/help.py'
- --- bzrlib/help.py	2006-06-18 23:19:23 +0000
+++ bzrlib/help.py	2006-07-12 19:17:02 +0000
@@ -129,25 +129,12 @@

 def help_on_command_options(cmd, outfile=None):
     from bzrlib.option import Option
+    from commands import get_optparser
+    if outfile is None:
+        outfile = sys.stdout
     options = cmd.options()
- -    if not options:
- -        return
- -    if outfile == None:
- -        outfile = sys.stdout
- -    outfile.write('\noptions:\n')
- -    for option_name, option in sorted(options.items()):
- -        l = '    --' + option_name
- -        if option.type is not None:
- -            l += ' ' + option.argname.upper()
- -        short_name = option.short_name()
- -        if short_name:
- -            assert len(short_name) == 1
- -            l += ', -' + short_name
- -        l += (30 - len(l)) * ' ' + option.help
- -        # TODO: split help over multiple lines with correct indenting and
- -        # wrapping
- -        wrapped = textwrap.fill(l, initial_indent='',
subsequent_indent=30*' ')
- -        outfile.write(wrapped + '\n')
+    outfile.write('\n')
+    outfile.write(get_optparser(options).format_option_help())


Why are you writing a '\n' *before* the output of 'format_option_help'
it would seem to me that if anything, you need it after.
- From what I see, the code is already doing outfile.write('\n') if the
documentation doesn't end in a newline before it even calls
help_on_command_options.

Otherwise I'm happy to see us using optparse to generate the output.
Since they already deal with wrapping, and all that.

It will mean quite a change to the output, though. Since instead of
doing (log --help):
options:
    --forward                 show from oldest to newest
    --help, -h                show help message
    --line                    Use log format with one line per
                              revision. Same as --log-format line
    --log-format ARG          Use this log format
    --long, -l                Use detailed log format. Same as --log-
                              format long
    --message ARG             show revisions whose message matches
                              this regexp
    --revision ARG, -r
    --short                   Use moderately short log format. Same as
                              --log-format short
    --show-ids                show internal object ids
    --timezone ARG            display timezone as local, original, or
                              utc
    --verbose                 show files changed in each revision


The default optparse help options will generate:

options:
    --forward                 show from oldest to newest
    --help, -h                show help message
    --line                    Use log format with one line per
                              revision. Same as --log-format line
    --log-format=LOG_FORMAT   Use this log format
    --long, -l                Use detailed log format. Same as --log-
                              format long
    --message=MESSAGE         show revisions whose message matches
                              this regexp
    -r REVISION, --revision=REVISION
    --short                   Use moderately short log format. Same as
                              --log-format short
    --show-ids                show internal object ids
    --timezone=TIMEZONE       display timezone as local, original, or
                              utc
    --verbose                 show files changed in each revision

It isn't bad, but it is different.

John
=:->

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEtVLwJdeBCYSNAAMRAkrfAKCY+4SaYHGLFuM3IErNTTcvIzUYpgCeLDTN
xUrfS30J8R7R15CudACy3nQ=
=E8Xm
-----END PGP SIGNATURE-----




More information about the bazaar mailing list