sapling/mercurial/fancyopts.py
mpm@selenic.com 4cc432f9f1 Beginning of new command parsing interface
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Beginning of new command parsing interface

This adds commands.py, with a primary interface dispatch(args)

Dispatch searches a table of known commands, handles switches, sets up
a repo object if appropriate, and dispatches the command.

It also handles KeyboardInterrupt and can handle similar exceptions in
the future.

If the command is unknown, it falls through to the current command handler.

Commands currently handled by the new scheme: help, init, and annotate

manifest hash: 134cd032c880985e3f92f82efb8b629dd862ba4c
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)

iD8DBQFCnXEGywK+sNU5EO8RAuDAAJ9q7K4w7qGVWv1NWjCPFGO/UJc6VQCdEhMQ
sBBlSRzah9QPy8K94catZyg=
=wuRf
-----END PGP SIGNATURE-----
2005-06-01 00:25:42 -08:00

54 lines
1.5 KiB
Python

import sys, os, getopt
def fancyopts(args, options, state, syntax='', minlen = 0):
long=[]
short=''
map={}
dt={}
def help(state, opt, arg, options=options, syntax=syntax):
print "Usage: ", syntax
for s, l, d, c in options:
opt=' '
if s: opt = opt + '-' + s + ' '
if l: opt = opt + '--' + l + ' '
if d: opt = opt + '(' + str(d) + ')'
print opt
if c: print ' %s' % c
sys.exit(0)
if len(args) < minlen:
help(state, None, args)
options=[('h', 'help', help, 'Show usage info')] + options
for s, l, d, c in options:
map['-'+s] = map['--'+l]=l
state[l] = d
dt[l] = type(d)
if not d is None and not type(d) is type(help): s, l=s+':', l+'='
if s: short = short + s
if l: long.append(l)
if os.environ.has_key("HG_OPTS"):
args = os.environ["HG_OPTS"].split() + args
try:
opts, args = getopt.getopt(args, short, long)
except getopt.GetoptError:
help(state, None, args)
sys.exit(-1)
for opt, arg in opts:
if dt[map[opt]] is type(help): state[map[opt]](state,map[opt],arg)
elif dt[map[opt]] is type(1): state[map[opt]] = int(arg)
elif dt[map[opt]] is type(''): state[map[opt]] = arg
elif dt[map[opt]] is type([]): state[map[opt]].append(arg)
elif dt[map[opt]] is type(None): state[map[opt]] = 1
del state["help"]
return args