mercurial/fancyopts.py
author mpm@selenic.com
Fri, 03 Jun 2005 12:55:56 -0800
changeset 230 00ea3613f82c
parent 209 63af1db35611
child 293 11d64332a1cb
permissions -rw-r--r--
make diffdir default to dirstate.parents() -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 make diffdir default to dirstate.parents() update various diffdir users to use default manifest hash: aeca2b9da1aca278dd5e3f27cc2906667803577d -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (GNU/Linux) iD8DBQFCoMPcywK+sNU5EO8RAkY8AJ90UHQXnJnkG9PJKG7IsgPeOZ2WZACgiarS HhS2zX3TRM9WdZHo5nLvZGw= =7YyP -----END PGP SIGNATURE-----

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