~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/commands.py

  • Committer: Martin Pool
  • Date: 2005-05-05 02:55:45 UTC
  • Revision ID: mbp@sourcefrog.net-20050505025545-7c45e4c5255c5fd5
- refactor command aliases into command classes
- fix edge bugs in aliases and help command
- test cases for this

Show diffs side-by-side

added added

removed removed

Lines of Context:
77
77
     format_date
78
78
 
79
79
 
80
 
CMD_ALIASES = {
81
 
    '?':         'help',
82
 
    'ci':        'commit',
83
 
    'checkin':   'commit',
84
 
    'di':        'diff',
85
 
    'st':        'status',
86
 
    'stat':      'status',
87
 
    }
88
 
 
89
 
 
90
 
def get_cmd_class(cmd):
91
 
    cmd = str(cmd)
92
 
    
93
 
    cmd = CMD_ALIASES.get(cmd, cmd)
94
 
    
 
80
def _squish_command_name(cmd):
 
81
    return 'cmd_' + cmd.replace('-', '_')
 
82
 
 
83
 
 
84
def _unsquish_command_name(cmd):
 
85
    assert cmd.startswith("cmd_")
 
86
    return cmd[4:].replace('_','-')
 
87
 
 
88
def _get_all_cmds():
 
89
    """Return canonical name and class for all registered commands."""
 
90
    for k, v in globals().iteritems():
 
91
        if k.startswith("cmd_"):
 
92
            yield _unsquish_command_name(k), v
 
93
 
 
94
def _get_cmd_class(cmd):
 
95
    """Return the canonical name and command class for a command.
 
96
    """
 
97
    cmd = str(cmd)                      # not unicode
 
98
 
 
99
    # first look up this command under the specified name
95
100
    try:
96
 
        cmd_class = globals()['cmd_' + cmd.replace('-', '_')]
 
101
        return cmd, globals()[_squish_command_name(cmd)]
97
102
    except KeyError:
 
103
        pass
 
104
 
 
105
    # look for any command which claims this as an alias
 
106
    for cmdname, cmdclass in _get_all_cmds():
 
107
        if cmd in cmdclass.aliases:
 
108
            return cmdname, cmdclass
 
109
    else:
98
110
        raise BzrCommandError("unknown command %r" % cmd)
99
111
 
100
 
    return cmd, cmd_class
101
 
 
102
 
 
103
112
 
104
113
class Command:
105
114
    """Base class for commands.
159
168
    missing, in which case the old name is shown.
160
169
    """
161
170
    takes_options = ['all']
 
171
    aliases = ['st', 'stat']
162
172
    
163
173
    def run(self, all=False):
164
174
        #import bzrlib.status
409
419
    
410
420
    takes_args = ['file*']
411
421
    takes_options = ['revision']
 
422
    aliases = ['di']
412
423
 
413
424
    def run(self, revision=None, file_list=None):
414
425
        from bzrlib.diff import show_diff
594
605
    TODO: Strict commit that fails if there are unknown or deleted files.
595
606
    """
596
607
    takes_options = ['message', 'verbose']
597
 
    
 
608
    aliases = ['ci', 'checkin']
 
609
 
598
610
    def run(self, message=None, verbose=False):
599
611
        if not message:
600
612
            raise BzrCommandError("please specify a commit message")
687
699
 
688
700
    For a list of all available commands, say 'bzr help commands'."""
689
701
    takes_args = ['topic?']
 
702
    aliases = ['?']
690
703
    
691
704
    def run(self, topic=None):
692
705
        help(topic)
705
718
    cmdname = str(cmdname)
706
719
 
707
720
    from inspect import getdoc
708
 
    topic, cmdclass = get_cmd_class(cmdname)
 
721
    topic, cmdclass = _get_cmd_class(cmdname)
709
722
 
710
723
    doc = getdoc(cmdclass)
711
724
    if doc == None:
755
768
    import inspect
756
769
    
757
770
    accu = []
758
 
    for k, v in globals().items():
759
 
        if k.startswith('cmd_'):
760
 
            accu.append((k[4:].replace('_','-'), v))
 
771
    for cmdname, cmdclass in _get_all_cmds():
 
772
        accu.append((cmdname, cmdclass))
761
773
    accu.sort()
762
774
    for cmdname, cmdclass in accu:
763
775
        if cmdclass.hidden:
932
944
        log_error('  try "bzr help"')
933
945
        return 1
934
946
 
935
 
    canonical_cmd, cmd_class = get_cmd_class(cmd)
 
947
    canonical_cmd, cmd_class = _get_cmd_class(cmd)
936
948
 
937
949
    # global option
938
950
    if 'profile' in opts: