mercurial/cmdutil.py
changeset 4733 0ecfc3b3f072
parent 4732 b0520e3903fe
child 4734 9ac493f12901
equal deleted inserted replaced
4732:b0520e3903fe 4733:0ecfc3b3f072
   246         except (IndexError, ValueError):
   246         except (IndexError, ValueError):
   247             raise util.Abort(_('malformed --config option: %s') % cfg)
   247             raise util.Abort(_('malformed --config option: %s') % cfg)
   248     return parsed
   248     return parsed
   249 
   249 
   250 def earlygetopt(aliases, args):
   250 def earlygetopt(aliases, args):
   251     """Return list of values for a option (with aliases) in given order
   251     """Return list of values for an option (or aliases).
   252 
   252 
   253     Short option aliases have to occur before long aliases, e.g.:
   253     The values are listed in the order they appear in args.
   254     earlygetopt(["-R", "--repository", "--repo"], args)
   254     The options and values are removed from args.
   255     (this is not checked!)
       
   256     """
   255     """
   257     try:
   256     try:
   258         argcount = args.index("--")
   257         argcount = args.index("--")
   259     except ValueError:
   258     except ValueError:
   260         argcount = len(args)
   259         argcount = len(args)
       
   260     shortopts = [opt for opt in aliases if len(opt) == 2]
   261     values = []
   261     values = []
   262     pos = 0
   262     pos = 0
   263     while pos < argcount:
   263     while pos < argcount:
   264         valuepos = argcount
   264         if args[pos] in aliases:
   265         for opt in aliases:
   265             if pos + 1 >= argcount:
   266             # short option can have no following space, e.g. hg log -Rfoo:
       
   267             if len(opt) == 2:
       
   268                 i = argcount
       
   269                 while i > 0:
       
   270                     i -= 1
       
   271                     arg = args[i]
       
   272                     if len(arg) > 2 and arg.startswith(opt):
       
   273                         # split -Rfoo -> -R foo
       
   274                         args[i:i+1] = [opt, arg[2:]]
       
   275                         argcount += 1
       
   276             # find next occurance of current alias
       
   277             try:
       
   278                 candidate = args.index(opt, pos, argcount) + 1
       
   279                 # ignore and let getopt report an error if there is no value
   266                 # ignore and let getopt report an error if there is no value
   280                 if candidate < valuepos:
   267                 break
   281                     valuepos = candidate
   268             del args[pos]
   282             except ValueError:
   269             values.append(args.pop(pos))
   283                 pass
   270             argcount -= 2
   284         if valuepos < argcount:
   271         elif args[pos][:2] in shortopts:
   285             values.append(args[valuepos])
   272             # short option can have no following space, e.g. hg log -Rfoo
   286         pos = valuepos
   273             values.append(args.pop(pos)[2:])
       
   274             argcount -= 1
       
   275         else:
       
   276             pos += 1
   287     return values
   277     return values
   288 
   278 
   289 def dispatch(ui, args, argv0=None):
   279 def dispatch(ui, args, argv0=None):
   290     # remember how to call 'hg' before changing the working dir
   280     # remember how to call 'hg' before changing the working dir
   291     util.set_hgexecutable(argv0)
   281     util.set_hgexecutable(argv0)