[PATCH]: Optional explanation for options

Magnus Therning magnus at therning.org
Thu Sep 15 17:54:00 BST 2005


Does this come close to what you were discussing?

/M


*** added file 'bzrlib/option.py'
--- /dev/null 
+++ bzrlib/option.py 
@@ -0,0 +1,118 @@
+import bzrlib.commands
+
+def _parse_revision_str(revstr):
+    """This handles a revision string -> revno.
+
+    This always returns a list.  The list will have one element for 
+
+    It supports integers directly, but everything else it
+    defers for passing to Branch.get_revision_info()
+
+    >>> _parse_revision_str('234')
+    [234]
+    >>> _parse_revision_str('234..567')
+    [234, 567]
+    >>> _parse_revision_str('..')
+    [None, None]
+    >>> _parse_revision_str('..234')
+    [None, 234]
+    >>> _parse_revision_str('234..')
+    [234, None]
+    >>> _parse_revision_str('234..456..789') # Maybe this should be an error
+    [234, 456, 789]
+    >>> _parse_revision_str('234....789') # Error?
+    [234, None, 789]
+    >>> _parse_revision_str('revid:test at other.com-234234')
+    ['revid:test at other.com-234234']
+    >>> _parse_revision_str('revid:test at other.com-234234..revid:test at other.com-234235')
+    ['revid:test at other.com-234234', 'revid:test at other.com-234235']
+    >>> _parse_revision_str('revid:test at other.com-234234..23')
+    ['revid:test at other.com-234234', 23]
+    >>> _parse_revision_str('date:2005-04-12')
+    ['date:2005-04-12']
+    >>> _parse_revision_str('date:2005-04-12 12:24:33')
+    ['date:2005-04-12 12:24:33']
+    >>> _parse_revision_str('date:2005-04-12T12:24:33')
+    ['date:2005-04-12T12:24:33']
+    >>> _parse_revision_str('date:2005-04-12,12:24:33')
+    ['date:2005-04-12,12:24:33']
+    >>> _parse_revision_str('-5..23')
+    [-5, 23]
+    >>> _parse_revision_str('-5')
+    [-5]
+    >>> _parse_revision_str('123a')
+    ['123a']
+    >>> _parse_revision_str('abc')
+    ['abc']
+    """
+    import re
+    old_format_re = re.compile('\d*:\d*')
+    m = old_format_re.match(revstr)
+    if m:
+        warning('Colon separator for revision numbers is deprecated.'
+                ' Use .. instead')
+        revs = []
+        for rev in revstr.split(':'):
+            if rev:
+                revs.append(int(rev))
+            else:
+                revs.append(None)
+        return revs
+    revs = []
+    for x in revstr.split('..'):
+        if not x:
+            revs.append(None)
+        else:
+            try:
+                revs.append(int(x))
+            except ValueError:
+                revs.append(x)
+    return revs
+
+
+def _parse_merge_type(typestring):
+    import bzrlib.commands
+    return bzrlib.commands.get_merge_type(typestring)
+
+
+class Option(object):
+    OPTIONS = {}
+    SHORT_OPTIONS = {}
+
+    def __init__(self, name, help='help string', type=None):
+        # need to add some description of the parameters here
+        self.name = name
+        self.help = help
+        self.type = type
+
+Option.OPTIONS['all'] = Option('all', type=None)
+Option.OPTIONS['diff-options'] = Option('diff', type=str)
+Option.OPTIONS['help'] = Option('help', type=None)
+Option.OPTIONS['file'] = Option('file', type=unicode)
+Option.OPTIONS['force'] = Option('force', type=None)
+Option.OPTIONS['format'] = Option('format', type=unicode)
+Option.OPTIONS['forward'] = Option('forward', type=None)
+Option.OPTIONS['message'] = Option('message', type=unicode)
+Option.OPTIONS['no-recurse'] = Option('no', type=None)
+Option.OPTIONS['profile'] = Option('profile', type=None)
+Option.OPTIONS['revision'] = Option('revision', type=_parse_revision_str)
+Option.OPTIONS['short'] = Option('short', type=None)
+Option.OPTIONS['show-ids'] = Option('show', type=None)
+Option.OPTIONS['timezone'] = Option('timezone', type=str)
+Option.OPTIONS['verbose'] = Option('verbose', type=None)
+Option.OPTIONS['version'] = Option('version', type=None)
+Option.OPTIONS['email'] = Option('email', type=None)
+Option.OPTIONS['unchanged'] = Option('unchanged', type=None)
+Option.OPTIONS['update'] = Option('update', type=None)
+Option.OPTIONS['long'] = Option('long', type=None)
+Option.OPTIONS['root'] = Option('root', type=str)
+Option.OPTIONS['no-backup'] = Option('no', type=None)
+Option.OPTIONS['merge-type'] = Option('merge', type=_parse_merge_type)
+Option.OPTIONS['pattern'] = Option('pattern', type=str)
+
+Option.SHORT_OPTIONS['F'] = Option.OPTIONS['file']
+Option.SHORT_OPTIONS['h'] = Option.OPTIONS['help']
+Option.SHORT_OPTIONS['m'] = Option.OPTIONS['message']
+Option.SHORT_OPTIONS['r'] = Option.OPTIONS['revision']
+Option.SHORT_OPTIONS['v'] = Option.OPTIONS['verbose']
+Option.SHORT_OPTIONS['l'] = Option.OPTIONS['long']

*** modified file 'bzrlib/commands.py'
--- bzrlib/commands.py 
+++ bzrlib/commands.py 
@@ -37,6 +37,7 @@
 from bzrlib.errors import BzrError, BzrCheckError, BzrCommandError
 from bzrlib.branch import find_branch
 from bzrlib import BZRDIR
+from bzrlib.option import Option
 
 plugin_cmds = {}
 
@@ -64,76 +65,6 @@
 def _unsquish_command_name(cmd):
     assert cmd.startswith("cmd_")
     return cmd[4:].replace('_','-')
-
-
-def _parse_revision_str(revstr):
-    """This handles a revision string -> revno.
-
-    This always returns a list.  The list will have one element for 
-
-    It supports integers directly, but everything else it
-    defers for passing to Branch.get_revision_info()
-
-    >>> _parse_revision_str('234')
-    [234]
-    >>> _parse_revision_str('234..567')
-    [234, 567]
-    >>> _parse_revision_str('..')
-    [None, None]
-    >>> _parse_revision_str('..234')
-    [None, 234]
-    >>> _parse_revision_str('234..')
-    [234, None]
-    >>> _parse_revision_str('234..456..789') # Maybe this should be an error
-    [234, 456, 789]
-    >>> _parse_revision_str('234....789') # Error?
-    [234, None, 789]
-    >>> _parse_revision_str('revid:test at other.com-234234')
-    ['revid:test at other.com-234234']
-    >>> _parse_revision_str('revid:test at other.com-234234..revid:test at other.com-234235')
-    ['revid:test at other.com-234234', 'revid:test at other.com-234235']
-    >>> _parse_revision_str('revid:test at other.com-234234..23')
-    ['revid:test at other.com-234234', 23]
-    >>> _parse_revision_str('date:2005-04-12')
-    ['date:2005-04-12']
-    >>> _parse_revision_str('date:2005-04-12 12:24:33')
-    ['date:2005-04-12 12:24:33']
-    >>> _parse_revision_str('date:2005-04-12T12:24:33')
-    ['date:2005-04-12T12:24:33']
-    >>> _parse_revision_str('date:2005-04-12,12:24:33')
-    ['date:2005-04-12,12:24:33']
-    >>> _parse_revision_str('-5..23')
-    [-5, 23]
-    >>> _parse_revision_str('-5')
-    [-5]
-    >>> _parse_revision_str('123a')
-    ['123a']
-    >>> _parse_revision_str('abc')
-    ['abc']
-    """
-    import re
-    old_format_re = re.compile('\d*:\d*')
-    m = old_format_re.match(revstr)
-    if m:
-        warning('Colon separator for revision numbers is deprecated.'
-                ' Use .. instead')
-        revs = []
-        for rev in revstr.split(':'):
-            if rev:
-                revs.append(int(rev))
-            else:
-                revs.append(None)
-        return revs
-    revs = []
-    for x in revstr.split('..'):
-        if not x:
-            revs.append(None)
-        else:
-            try:
-                revs.append(int(x))
-            except ValueError:
-                revs.append(x)
-    return revs
 
 
 def get_merge_type(typestring):
@@ -342,48 +273,6 @@
     return parsed
 
 
-
-
-# list of all available options; the rhs can be either None for an
-# option that takes no argument, or a constructor function that checks
-# the type.
-OPTIONS = {
-    'all':                    None,
-    'diff-options':           str,
-    'help':                   None,
-    'file':                   unicode,
-    'force':                  None,
-    'format':                 unicode,
-    'forward':                None,
-    'message':                unicode,
-    'no-recurse':             None,
-    'profile':                None,
-    'revision':               _parse_revision_str,
-    'short':                  None,
-    'show-ids':               None,
-    'timezone':               str,
-    'verbose':                None,
-    'version':                None,
-    'email':                  None,
-    'unchanged':              None,
-    'update':                 None,
-    'long':                   None,
-    'root':                   str,
-    'no-backup':              None,
-    'merge-type':             get_merge_type,
-    'pattern':                str,
-    }
-
-SHORT_OPTIONS = {
-    'F':                      'file', 
-    'h':                      'help',
-    'm':                      'message',
-    'r':                      'revision',
-    'v':                      'verbose',
-    'l':                      'long',
-}
-
-
 def parse_args(argv):
     """Parse command line.
     
@@ -435,25 +324,25 @@
                     raise BzrError('unknown long option %r' % a)
             else:
                 shortopt = a[1:]
-                if shortopt in SHORT_OPTIONS:
+                if shortopt in Option.SHORT_OPTIONS:
                     # Multi-character options must have a space to delimit
                     # their value
-                    optname = SHORT_OPTIONS[shortopt]
+                    optname = Option.SHORT_OPTIONS[shortopt].name
                 else:
                     # Single character short options, can be chained,
                     # and have their value appended to their name
                     shortopt = a[1:2]
-                    if shortopt not in SHORT_OPTIONS:
+                    if shortopt not in Option.SHORT_OPTIONS:
                         # We didn't find the multi-character name, and we
                         # didn't find the single char name
                         raise BzrError('unknown short option %r' % a)
-                    optname = SHORT_OPTIONS[shortopt]
+                    optname = Option.SHORT_OPTIONS[shortopt].name
 
                     if a[2:]:
                         # There are extra things on this option
                         # see if it is the value, or if it is another
                         # short option
-                        optargfn = OPTIONS[optname]
+                        optargfn = Option.OPTIONS[optname].type
                         if optargfn is None:
                             # This option does not take an argument, so the
                             # next entry is another short option, pack it back
@@ -468,7 +357,7 @@
                 # XXX: Do we ever want to support this, e.g. for -r?
                 raise BzrError('repeated option %r' % a)
                 
-            optargfn = OPTIONS[optname]
+            optargfn = Option.OPTIONS[optname].type
             if optargfn:
                 if optarg == None:
                     if not argv:

*** modified file 'bzrlib/help.py'
--- bzrlib/help.py 
+++ bzrlib/help.py 
@@ -117,7 +117,7 @@
 
 
 def help_on_options(options, outfile=None):
-    from bzrlib.commands import SHORT_OPTIONS
+    from bzrlib.option import Option
     
     if not options:
         return
@@ -128,10 +128,11 @@
     outfile.write('\noptions:\n')
     for on in options:
         l = '    --' + on
-        for shortname, longname in SHORT_OPTIONS.items():
-            if longname == on:
+        for shortname, longopt in Option.SHORT_OPTIONS.items():
+            if longopt.name == on:
                 l += ', -' + shortname
                 break
+        l += (30 - len(l)) * ' ' + Option.OPTIONS[on].help
         outfile.write(l + '\n')
 
 


-- 
Magnus Therning                    (OpenPGP: 0xAB4DFBA4)
magnus at therning.org
http://therning.org/magnus

Software is not manufactured, it is something you write and publish.
Keep Europe free from software patents, we do not want censorship
by patent law on written works.

The magic words are squeamish ossifrage
      -- Plaintext of the message encoded in RSA-129, quoted in Martin
         Gardner's column about RSA.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20050915/eeb11b69/attachment.pgp 


More information about the bazaar mailing list