comparison mercurial/commands.py @ 212:48398a5353e3

commands: better argument processing, per-command help -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 commands: better argument processing, per-command help This passes command line arguments as positional arguments rather than as a list and attempt to catch exceptions if the arguments don't match up. It also adds 'hg help [cmd]' which prints the syntax and docstring for the given command. manifest hash: cef3ac7076f99fce4265a5dc7acb57fb03d93270 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (GNU/Linux) iD8DBQFCnghXywK+sNU5EO8RAv+5AJ9MfCCRxE+pNu3i44v5I4IyQoRaiQCeNqEK wBzAiP+Q6F/9PL2xOrrZGHw= =EYdk -----END PGP SIGNATURE-----
author mpm@selenic.com
date Wed, 01 Jun 2005 11:11:19 -0800
parents 426d3c3ae363
children d2172916ef6c
comparison
equal deleted inserted replaced
211:426d3c3ae363 212:48398a5353e3
1 import os, re 1 import os, re, traceback, sys
2 from mercurial import fancyopts, ui, hg 2 from mercurial import fancyopts, ui, hg
3 3
4 class UnknownCommand(Exception): pass 4 class UnknownCommand(Exception): pass
5 5
6 def relpath(repo, args): 6 def relpath(repo, args):
7 if os.getcwd() != repo.root: 7 if os.getcwd() != repo.root:
8 p = os.getcwd()[len(repo.root) + 1: ] 8 p = os.getcwd()[len(repo.root) + 1: ]
9 return [ os.path.join(p, x) for x in args ] 9 return [ os.path.join(p, x) for x in args ]
10 return args 10 return args
11 11
12 def help(ui, args): 12 def help(ui, cmd=None):
13 '''show help'''
14 if cmd:
15 try:
16 i = find(cmd)
17 ui.write("%s\n\n" % i[2])
18 ui.write(i[0].__doc__, "\n")
19 except UnknownCommand:
20 ui.warn("unknown command %s", cmd)
21 sys.exit(0)
22
13 ui.status("""\ 23 ui.status("""\
14 hg commands: 24 hg commands:
15 25
16 add [files...] add the given files in the next commit 26 add [files...] add the given files in the next commit
17 addremove add all new files, delete all missing files 27 addremove add all new files, delete all missing files
33 status show new, missing, and changed files in working dir 43 status show new, missing, and changed files in working dir
34 tags show current changeset tags 44 tags show current changeset tags
35 undo undo the last transaction 45 undo undo the last transaction
36 """) 46 """)
37 47
38 def init(ui, args): 48 def init(ui):
39 """create a repository""" 49 """create a repository"""
40 hg.repository(ui, ".", create=1) 50 hg.repository(ui, ".", create=1)
41 51
42 def checkout(u, repo, args): 52 def checkout(u, repo, changeset=None):
53 '''checkout a given changeset or the current tip'''
43 node = repo.changelog.tip() 54 node = repo.changelog.tip()
44 if args: 55 if changeset:
45 node = repo.lookup(args[0]) 56 node = repo.lookup(changeset)
46 repo.checkout(node) 57 repo.checkout(node)
47 58
48 def annotate(u, repo, args, **ops): 59 def annotate(u, repo, *args, **ops):
49 if not args:
50 return
51
52 def getnode(rev): 60 def getnode(rev):
53 return hg.short(repo.changelog.node(rev)) 61 return hg.short(repo.changelog.node(rev))
54 62
55 def getname(rev): 63 def getname(rev):
56 try: 64 try:
88 pieces.append([ "%*s" % (m, x) for x in l]) 96 pieces.append([ "%*s" % (m, x) for x in l])
89 97
90 for p,l in zip(zip(*pieces), lines): 98 for p,l in zip(zip(*pieces), lines):
91 u.write(" ".join(p) + ": " + l[1]) 99 u.write(" ".join(p) + ": " + l[1])
92 100
93 def undo(ui, repo, args): 101 def undo(ui, repo):
94 repo.undo() 102 repo.undo()
95 103
96 table = { 104 table = {
97 "init": (init, [], 'hg init'), 105 "init": (init, [], 'hg init'),
98 "help": (help, [], 'hg help'), 106 "help": (help, [], 'hg help [command]'),
99 "checkout|co": (checkout, [], 'hg checkout'), 107 "checkout|co": (checkout, [], 'hg checkout [changeset]'),
100 "ann|annotate": (annotate, 108 "ann|annotate": (annotate,
101 [('r', 'revision', '', 'revision'), 109 [('r', 'revision', '', 'revision'),
102 ('u', 'user', None, 'show user'), 110 ('u', 'user', None, 'show user'),
103 ('n', 'number', None, 'show revision number'), 111 ('n', 'number', None, 'show revision number'),
104 ('c', 'changeset', None, 'show changeset')], 112 ('c', 'changeset', None, 'show changeset')],
105 'hg annotate [-u] [-c] [-n] [-r id] [files]'), 113 'hg annotate [-u] [-c] [-n] [-r id] [files]'),
106 "undo": (undo, [], 'hg undo'), 114 "undo": (undo, [], 'hg undo'),
107 } 115 }
108 116
109 norepo = "init branch help" 117 norepo = "init branch help"
118
119 def find(cmd):
120 i = None
121 for e in table.keys():
122 if re.match(e + "$", cmd):
123 return table[e]
124
125 raise UnknownCommand(cmd)
110 126
111 def dispatch(args): 127 def dispatch(args):
112 options = {} 128 options = {}
113 opts = [('v', 'verbose', None, 'verbose'), 129 opts = [('v', 'verbose', None, 'verbose'),
114 ('d', 'debug', None, 'debug'), 130 ('d', 'debug', None, 'debug'),
125 cmd, args = args[0], args[1:] 141 cmd, args = args[0], args[1:]
126 142
127 u = ui.ui(options["verbose"], options["debug"], options["quiet"], 143 u = ui.ui(options["verbose"], options["debug"], options["quiet"],
128 not options["noninteractive"]) 144 not options["noninteractive"])
129 145
130 i = None 146 # deal with unfound commands later
131 for e in table.keys(): 147 i = find(cmd)
132 if re.match(e + "$", cmd):
133 i = table[e]
134
135 # deal with this internally later
136 if not i: raise UnknownCommand(cmd)
137 148
138 cmdoptions = {} 149 cmdoptions = {}
139 args = fancyopts.fancyopts(args, i[1], cmdoptions, i[2]) 150 args = fancyopts.fancyopts(args, i[1], cmdoptions, i[2])
140 151
141 if cmd not in norepo.split(): 152 if cmd not in norepo.split():
142 repo = hg.repository(ui = u) 153 repo = hg.repository(ui = u)
143 d = lambda: i[0](u, repo, args, **cmdoptions) 154 d = lambda: i[0](u, repo, *args, **cmdoptions)
144 else: 155 else:
145 d = lambda: i[0](u, args, **cmdoptions) 156 d = lambda: i[0](u, *args, **cmdoptions)
146 157
147 try: 158 try:
148 d() 159 d()
149 except KeyboardInterrupt: 160 except KeyboardInterrupt:
150 u.warn("interrupted!\n") 161 u.warn("interrupted!\n")
162 except TypeError, inst:
163 # was this an argument error?
164 tb = traceback.extract_tb(sys.exc_info()[2])
165 if len(tb) > 2: # no
166 raise
167 u.warn("%s: invalid arguments\n" % i[0].__name__)
168 u.warn("syntax: %s\n" % i[2])
169 sys.exit(-1)