Mercurial > hg > mercurial-crew-with-dirclash
annotate 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 |
rev | line source |
---|---|
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
1 import os, re, traceback, sys |
209 | 2 from mercurial import fancyopts, ui, hg |
3 | |
4 class UnknownCommand(Exception): pass | |
5 | |
6 def relpath(repo, args): | |
7 if os.getcwd() != repo.root: | |
8 p = os.getcwd()[len(repo.root) + 1: ] | |
9 return [ os.path.join(p, x) for x in args ] | |
10 return args | |
11 | |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
12 def help(ui, cmd=None): |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
13 '''show help''' |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
14 if cmd: |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
15 try: |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
16 i = find(cmd) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
17 ui.write("%s\n\n" % i[2]) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
18 ui.write(i[0].__doc__, "\n") |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
19 except UnknownCommand: |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
20 ui.warn("unknown command %s", cmd) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
21 sys.exit(0) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
22 |
209 | 23 ui.status("""\ |
24 hg commands: | |
25 | |
26 add [files...] add the given files in the next commit | |
27 addremove add all new files, delete all missing files | |
28 annotate [files...] show changeset number per file line | |
29 branch <path> create a branch of <path> in this directory | |
30 checkout [changeset] checkout the latest or given changeset | |
31 commit commit all changes to the repository | |
32 diff [files...] diff working directory (or selected files) | |
33 dump <file> [rev] dump the latest or given revision of a file | |
34 dumpmanifest [rev] dump the latest or given revision of the manifest | |
35 export <rev> dump the changeset header and diffs for a revision | |
36 history show changeset history | |
37 init create a new repository in this directory | |
38 log <file> show revision history of a single file | |
39 merge <path> merge changes from <path> into local repository | |
40 recover rollback an interrupted transaction | |
41 remove [files...] remove the given files in the next commit | |
42 serve export the repository via HTTP | |
43 status show new, missing, and changed files in working dir | |
44 tags show current changeset tags | |
45 undo undo the last transaction | |
46 """) | |
47 | |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
48 def init(ui): |
209 | 49 """create a repository""" |
50 hg.repository(ui, ".", create=1) | |
51 | |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
52 def checkout(u, repo, changeset=None): |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
53 '''checkout a given changeset or the current tip''' |
209 | 54 node = repo.changelog.tip() |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
55 if changeset: |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
56 node = repo.lookup(changeset) |
209 | 57 repo.checkout(node) |
58 | |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
59 def annotate(u, repo, *args, **ops): |
209 | 60 def getnode(rev): |
61 return hg.short(repo.changelog.node(rev)) | |
62 | |
63 def getname(rev): | |
64 try: | |
65 return bcache[rev] | |
66 except KeyError: | |
67 cl = repo.changelog.read(repo.changelog.node(rev)) | |
68 name = cl[1] | |
69 f = name.find('@') | |
70 if f >= 0: | |
71 name = name[:f] | |
72 bcache[rev] = name | |
73 return name | |
74 | |
75 bcache = {} | |
76 opmap = [['user', getname], ['number', str], ['changeset', getnode]] | |
77 if not ops['user'] and not ops['changeset']: | |
78 ops['number'] = 1 | |
79 | |
80 args = relpath(repo, args) | |
81 node = repo.current | |
82 if ops['revision']: | |
83 node = repo.changelog.lookup(ops['revision']) | |
84 change = repo.changelog.read(node) | |
85 mmap = repo.manifest.read(change[0]) | |
86 maxuserlen = 0 | |
87 maxchangelen = 0 | |
88 for f in args: | |
89 lines = repo.file(f).annotate(mmap[f]) | |
90 pieces = [] | |
91 | |
92 for o, f in opmap: | |
93 if ops[o]: | |
94 l = [ f(n) for n,t in lines ] | |
95 m = max(map(len, l)) | |
96 pieces.append([ "%*s" % (m, x) for x in l]) | |
97 | |
98 for p,l in zip(zip(*pieces), lines): | |
99 u.write(" ".join(p) + ": " + l[1]) | |
100 | |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
101 def undo(ui, repo): |
210 | 102 repo.undo() |
103 | |
209 | 104 table = { |
105 "init": (init, [], 'hg init'), | |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
106 "help": (help, [], 'hg help [command]'), |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
107 "checkout|co": (checkout, [], 'hg checkout [changeset]'), |
209 | 108 "ann|annotate": (annotate, |
109 [('r', 'revision', '', 'revision'), | |
110 ('u', 'user', None, 'show user'), | |
111 ('n', 'number', None, 'show revision number'), | |
112 ('c', 'changeset', None, 'show changeset')], | |
113 'hg annotate [-u] [-c] [-n] [-r id] [files]'), | |
210 | 114 "undo": (undo, [], 'hg undo'), |
209 | 115 } |
116 | |
117 norepo = "init branch help" | |
118 | |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
119 def find(cmd): |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
120 i = None |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
121 for e in table.keys(): |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
122 if re.match(e + "$", cmd): |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
123 return table[e] |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
124 |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
125 raise UnknownCommand(cmd) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
126 |
209 | 127 def dispatch(args): |
128 options = {} | |
129 opts = [('v', 'verbose', None, 'verbose'), | |
130 ('d', 'debug', None, 'debug'), | |
131 ('q', 'quiet', None, 'quiet'), | |
132 ('y', 'noninteractive', None, 'run non-interactively'), | |
133 ] | |
134 | |
135 args = fancyopts.fancyopts(args, opts, options, | |
136 'hg [options] <command> [options] [files]') | |
137 | |
138 if not args: | |
139 cmd = "help" | |
140 else: | |
141 cmd, args = args[0], args[1:] | |
142 | |
143 u = ui.ui(options["verbose"], options["debug"], options["quiet"], | |
144 not options["noninteractive"]) | |
145 | |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
146 # deal with unfound commands later |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
147 i = find(cmd) |
209 | 148 |
149 cmdoptions = {} | |
150 args = fancyopts.fancyopts(args, i[1], cmdoptions, i[2]) | |
151 | |
152 if cmd not in norepo.split(): | |
153 repo = hg.repository(ui = u) | |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
154 d = lambda: i[0](u, repo, *args, **cmdoptions) |
209 | 155 else: |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
156 d = lambda: i[0](u, *args, **cmdoptions) |
209 | 157 |
158 try: | |
159 d() | |
160 except KeyboardInterrupt: | |
161 u.warn("interrupted!\n") | |
212
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
162 except TypeError, inst: |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
163 # was this an argument error? |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
164 tb = traceback.extract_tb(sys.exc_info()[2]) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
165 if len(tb) > 2: # no |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
166 raise |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
167 u.warn("%s: invalid arguments\n" % i[0].__name__) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
168 u.warn("syntax: %s\n" % i[2]) |
48398a5353e3
commands: better argument processing, per-command help
mpm@selenic.com
parents:
211
diff
changeset
|
169 sys.exit(-1) |