##// END OF EJS Templates
commands: better argument processing, per-command help...
mpm@selenic.com -
r212:48398a53 default
parent child Browse files
Show More
@@ -1,4 +1,4 b''
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
@@ -9,7 +9,17 b' def relpath(repo, args):'
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
@@ -35,20 +45,18 b' def help(ui, args):'
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
@@ -90,13 +98,13 b' def annotate(u, repo, args, **ops):'
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'),
@@ -108,6 +116,14 b' table = {'
108
116
109 norepo = "init branch help"
117 norepo = "init branch help"
110
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)
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'),
@@ -127,24 +143,27 b' def dispatch(args):'
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)
General Comments 0
You need to be logged in to leave comments. Login now