hgkw/keyword.py
branch0.9.2compat
changeset 312 b92767fb8fb5
parent 311 6160401f94f2
child 322 b1ac738404f8
equal deleted inserted replaced
311:6160401f94f2 312:b92767fb8fb5
   114         return f
   114         return f
   115 
   115 
   116 # commands.parse/cmdutil.parse returned nothing for
   116 # commands.parse/cmdutil.parse returned nothing for
   117 # "hg diff --rev" before 88803a69b24a due to bug in fancyopts
   117 # "hg diff --rev" before 88803a69b24a due to bug in fancyopts
   118 def fancyopts(args, options, state):
   118 def fancyopts(args, options, state):
   119     '''Fixed fancyopts from 88803a69b24a.'''
   119     '''Fixed fancyopts from a9b7e425674f.'''
   120     long = []
   120     namelist = []
   121     short = ''
   121     shortlist = ''
   122     map = {}
   122     argmap = {}
   123     dt = {}
   123     defmap = {}
   124     for s, l, d, c in options:
   124 
   125         pl = l.replace('-', '_')
   125     for short, name, default, comment in options:
   126         map['-'+s] = map['--'+l] = pl
   126         # convert opts to getopt format
   127         if isinstance(d, list):
   127         oname = name
   128             state[pl] = d[:]
   128         name = name.replace('-', '_')
       
   129 
       
   130         argmap['-' + short] = argmap['--' + oname] = name
       
   131         defmap[name] = default
       
   132 
       
   133         # copy defaults to state
       
   134         if isinstance(default, list):
       
   135             state[name] = default[:]
       
   136         elif callable(default):
       
   137             print "whoa", name, default
       
   138             state[name] = None
   129         else:
   139         else:
   130             state[pl] = d
   140             state[name] = default
   131         dt[pl] = type(d)
   141 
   132         if (d is not None and d is not True and d is not False and
   142         # does it take a parameter?
   133             not callable(d)):
   143         if not (default is None or default is True or default is False):
   134             if s: s += ':'
   144             if short: short += ':'
   135             if l: l += '='
   145             if oname: oname += '='
   136         if s: short = short + s
   146         if short:
   137         if l: long.append(l)
   147             shortlist += short
   138     opts, args = getopt.getopt(args, short, long)
   148         if name:
   139     for opt, arg in opts:
   149             namelist.append(oname)
   140         if dt[map[opt]] is type(fancyopts): state[map[opt]](state, map[opt], arg)
   150 
   141         elif dt[map[opt]] is type(1): state[map[opt]] = int(arg)
   151     # parse arguments
   142         elif dt[map[opt]] is type(''): state[map[opt]] = arg
   152     opts, args = getopt.getopt(args, shortlist, namelist)
   143         elif dt[map[opt]] is type([]): state[map[opt]].append(arg)
   153 
   144         elif dt[map[opt]] is type(None): state[map[opt]] = True
   154     # transfer result to state
   145         elif dt[map[opt]] is type(False): state[map[opt]] = True
   155     for opt, val in opts:
       
   156         name = argmap[opt]
       
   157         t = type(defmap[name])
       
   158         if t is type(fancyopts):
       
   159             state[name] = defmap[name](val)
       
   160         elif t is type(1):
       
   161             state[name] = int(val)
       
   162         elif t is type(''):
       
   163             state[name] = val
       
   164         elif t is type([]):
       
   165             state[name].append(val)
       
   166         elif t is type(None) or t is type(False):
       
   167             state[name] = True
       
   168 
       
   169     # return unparsed args
   146     return args
   170     return args
   147 
   171 
   148 def findcmd(ui, cmd, table):
   172 def findcmd(ui, cmd, table):
   149     '''findcmd has table argument since 18a9fbb5cd78.'''
   173     '''findcmd has table argument since 18a9fbb5cd78.'''
   150     try:
   174     try: