mercurial/fancyopts.py
changeset 43076 2372284d9457
parent 37465 39e5e346eba7
child 43077 687b865b95ad
equal deleted inserted replaced
43075:57875cf423c9 43076:2372284d9457
    24     # thing and then exit, and so aren't suitable for use in things
    24     # thing and then exit, and so aren't suitable for use in things
    25     # like aliases anyway.
    25     # like aliases anyway.
    26     'help',
    26     'help',
    27     'version',
    27     'version',
    28 }
    28 }
       
    29 
    29 
    30 
    30 def _earlyoptarg(arg, shortlist, namelist):
    31 def _earlyoptarg(arg, shortlist, namelist):
    31     """Check if the given arg is a valid unabbreviated option
    32     """Check if the given arg is a valid unabbreviated option
    32 
    33 
    33     Returns (flag_str, has_embedded_value?, embedded_value, takes_value?)
    34     Returns (flag_str, has_embedded_value?, embedded_value, takes_value?)
    86         flag, val = arg[:2], arg[2:]
    87         flag, val = arg[:2], arg[2:]
    87         i = shortlist.find(flag[1:])
    88         i = shortlist.find(flag[1:])
    88         if i >= 0:
    89         if i >= 0:
    89             return flag, bool(val), val, shortlist.startswith(':', i + 1)
    90             return flag, bool(val), val, shortlist.startswith(':', i + 1)
    90     return '', False, '', False
    91     return '', False, '', False
       
    92 
    91 
    93 
    92 def earlygetopt(args, shortlist, namelist, gnu=False, keepsep=False):
    94 def earlygetopt(args, shortlist, namelist, gnu=False, keepsep=False):
    93     """Parse options like getopt, but ignores unknown options and abbreviated
    95     """Parse options like getopt, but ignores unknown options and abbreviated
    94     forms
    96     forms
    95 
    97 
   200             pos += 2
   202             pos += 2
   201 
   203 
   202     parsedargs.extend(args[pos:])
   204     parsedargs.extend(args[pos:])
   203     return parsedopts, parsedargs
   205     return parsedopts, parsedargs
   204 
   206 
       
   207 
   205 class customopt(object):
   208 class customopt(object):
   206     """Manage defaults and mutations for any type of opt."""
   209     """Manage defaults and mutations for any type of opt."""
   207 
   210 
   208     __metaclass__ = abc.ABCMeta
   211     __metaclass__ = abc.ABCMeta
   209 
   212 
   224     def newstate(self, oldstate, newparam, abort):
   227     def newstate(self, oldstate, newparam, abort):
   225         """Adds newparam to oldstate and returns the new state.
   228         """Adds newparam to oldstate and returns the new state.
   226 
   229 
   227         On failure, abort can be called with a string error message."""
   230         On failure, abort can be called with a string error message."""
   228 
   231 
       
   232 
   229 class _simpleopt(customopt):
   233 class _simpleopt(customopt):
   230     def _isboolopt(self):
   234     def _isboolopt(self):
   231         return isinstance(self._defaultvalue, (bool, type(None)))
   235         return isinstance(self._defaultvalue, (bool, type(None)))
   232 
   236 
   233     def newstate(self, oldstate, newparam, abort):
   237     def newstate(self, oldstate, newparam, abort):
   234         return newparam
   238         return newparam
       
   239 
   235 
   240 
   236 class _callableopt(customopt):
   241 class _callableopt(customopt):
   237     def __init__(self, callablefn):
   242     def __init__(self, callablefn):
   238         self.callablefn = callablefn
   243         self.callablefn = callablefn
   239         super(_callableopt, self).__init__(None)
   244         super(_callableopt, self).__init__(None)
   240 
   245 
   241     def newstate(self, oldstate, newparam, abort):
   246     def newstate(self, oldstate, newparam, abort):
   242         return self.callablefn(newparam)
   247         return self.callablefn(newparam)
   243 
   248 
       
   249 
   244 class _listopt(customopt):
   250 class _listopt(customopt):
   245     def getdefaultvalue(self):
   251     def getdefaultvalue(self):
   246         return self._defaultvalue[:]
   252         return self._defaultvalue[:]
   247 
   253 
   248     def newstate(self, oldstate, newparam, abort):
   254     def newstate(self, oldstate, newparam, abort):
   249         oldstate.append(newparam)
   255         oldstate.append(newparam)
   250         return oldstate
   256         return oldstate
       
   257 
   251 
   258 
   252 class _intopt(customopt):
   259 class _intopt(customopt):
   253     def newstate(self, oldstate, newparam, abort):
   260     def newstate(self, oldstate, newparam, abort):
   254         try:
   261         try:
   255             return int(newparam)
   262             return int(newparam)
   256         except ValueError:
   263         except ValueError:
   257             abort(_('expected int'))
   264             abort(_('expected int'))
       
   265 
   258 
   266 
   259 def _defaultopt(default):
   267 def _defaultopt(default):
   260     """Returns a default opt implementation, given a default value."""
   268     """Returns a default opt implementation, given a default value."""
   261 
   269 
   262     if isinstance(default, customopt):
   270     if isinstance(default, customopt):
   267         return _listopt(default[:])
   275         return _listopt(default[:])
   268     elif type(default) is type(1):
   276     elif type(default) is type(1):
   269         return _intopt(default)
   277         return _intopt(default)
   270     else:
   278     else:
   271         return _simpleopt(default)
   279         return _simpleopt(default)
       
   280 
   272 
   281 
   273 def fancyopts(args, options, state, gnu=False, early=False, optaliases=None):
   282 def fancyopts(args, options, state, gnu=False, early=False, optaliases=None):
   274     """
   283     """
   275     read args, parse options, and store options in state
   284     read args, parse options, and store options in state
   276 
   285 
   367         name = argmap[opt]
   376         name = argmap[opt]
   368         obj = defmap[name]
   377         obj = defmap[name]
   369         if obj._isboolopt():
   378         if obj._isboolopt():
   370             state[name] = boolval
   379             state[name] = boolval
   371         else:
   380         else:
       
   381 
   372             def abort(s):
   382             def abort(s):
   373                 raise error.Abort(_('invalid value %r for option %s, %s')
   383                 raise error.Abort(
   374                                   % (pycompat.maybebytestr(val), opt, s))
   384                     _('invalid value %r for option %s, %s')
       
   385                     % (pycompat.maybebytestr(val), opt, s)
       
   386                 )
       
   387 
   375             state[name] = defmap[name].newstate(state[name], val, abort)
   388             state[name] = defmap[name].newstate(state[name], val, abort)
   376 
   389 
   377     # return unparsed args
   390     # return unparsed args
   378     return args
   391     return args