parameters: (self, scriptname, cmd, argv, test=False)
return_statement: return vargs, kwargs
docstring: Parse arguments from argv.
  
          :param scriptname: The script filename.
          :param cmd: The command which is being called.
          :param argv: The argument list.
          :param test: If True prints to stdout.
docstring_summary: Parse arguments from argv.
docstring_tokens: Parse arguments from argv .
function: def parse_args(self, scriptname, cmd, argv, test=False):
          """
          Parse arguments from argv.
  
          :param scriptname: The script filename.
          :param cmd: The command which is being called.
          :param argv: The argument list.
          :param test: If True prints to stdout.
          """
          keywords = cmd.keywords
          shortopts = cmd.shortopts
  
          def type_error(name, value, t):
              if not test:
                  msg = "%s value %r must be %s" % (name, value, t)
                  raise CommandError(msg, scriptname, cmd)
  
          # shortopts maps long option names to characters. To look up short
          # options, we need to create a reverse mapping.
          shortchars = dict((v, k) for k, v in shortopts.items())
  
          # The *args list and **kwargs dict to build up from the command line
          # arguments
          vargs = []
          kwargs = {}
  
          double_dash = 0
          single_dash = 0
          while argv:
              # Take the next argument
              arg = argv.pop(0)
  
              if arg == "--":
                  double_dash += 1
                  if double_dash != 1:
                      raise CommandError("You cannot specify -- more than once")
                  # All arguments following a double hyphen are treated as
                  # positional arguments
                  vargs.extend(argv)
                  break
  
              elif arg == "-":
                  # sys.stdin
                  single_dash += 1
                  if single_dash != 1:
                      raise CommandError("You cannot specify - more than once")
                  vargs.append("-")
  
              elif arg.startswith("--"):
                  # Process long option
  
                  value = None
                  if "=" in arg:
                      # The argument was specified like --keyword=value
                      name, value = arg[2:].split("=", 1)
                      # strip quotes if value is quoted like
                      # --keyword='multiple words'
                      value = value.strip('\'"')
  
                      default = keywords.get(name)
                      try:
                          value = totype(value, default)
                      except (TypeError, ValueError):
                          type_error(name, value, type(default))
                  else:
                      # The argument was not specified with an equals sign...
                      name = arg[2:]
                      default = keywords.get(name)
  
                      if type(default) is bool:
                          # If this option is a boolean, it doesn't need a value;
                          # specifying it on the command line means "do the
                          # opposite of the default".
                          value = not default
                      else:
                          # The next item in the argument list is the value, i.e.
                          # --keyword value
                          if not argv or argv[0].startswith("-"):
                              # Oops, there isn't a value available... just use
                              # True, assuming this is a flag.
                              value = True
                          else:
                              value = argv.pop(0)
  
                          try:
                              value = totype(value, default)
                          except (TypeError, ValueError):
                              type_error(name, value, type(default))
  
                  # Store this option
                  kwargs[name] = value
  
              elif arg.startswith("-") and (cmd.shortopts or cmd.has_kwargs):
                  # Process short option(s)
  
                  # For each character after the '-'...
                  for i in range(1, len(arg)):
                      char = arg[i]
                      if cmd.has_kwargs:
                          name = char
                          default = keywords.get(name)
                      elif char not in shortchars:
                          continue
                      else:
                          # Get the long option name corresponding to this char
                          name = shortchars[char]
                          default = keywords[name]
  
                      if isinstance(default, bool):
                          # If this option is a boolean, it doesn't need a value;
                          # specifying it on the command line means "do the
                          # opposite of the default".
                          kwargs[name] = not default
                      else:
                          # This option requires a value...
                          if i == len(arg) - 1:
                              # This is the last character in the list, so the
                              # next argument on the command line is the value.
                              value = argv.pop(0)
                          else:
                              # There are other characters after this one, so
                              # the rest of the characters must represent the
                              # value (i.e. old-style UNIX option like -Nname)
                              value = arg[i + 1:]
  
                          # Remove leading equals sign if it's present. That
                          # means the option/value were specified as opt=value
                          # Then remove quotes.
                          value = value.lstrip("=").strip("'\"")
  
                          try:
                              kwargs[name] = totype(value, default)
                          except (TypeError, ValueError):
                              type_error(name, value, type(default))
                          break
              else:
                  # This doesn't start with "-", so just add it to the list of
                  # positional arguments.
                  vargs.append(arg)
  
          return vargs, kwargs
function_tokens: def parse_args ( self , scriptname , cmd , argv , test = False ) : keywords = cmd . keywords shortopts = cmd . shortopts def type_error ( name , value , t ) : if not test : msg = "%s value %r must be %s" % ( name , value , t ) raise CommandError ( msg , scriptname , cmd ) # shortopts maps long option names to characters. To look up short # options, we need to create a reverse mapping. shortchars = dict ( ( v , k ) for k , v in shortopts . items ( ) ) # The *args list and **kwargs dict to build up from the command line # arguments vargs = [ ] kwargs = { } double_dash = 0 single_dash = 0 while argv : # Take the next argument arg = argv . pop ( 0 ) if arg == "--" : double_dash += 1 if double_dash != 1 : raise CommandError ( "You cannot specify -- more than once" ) # All arguments following a double hyphen are treated as # positional arguments vargs . extend ( argv ) break elif arg == "-" : # sys.stdin single_dash += 1 if single_dash != 1 : raise CommandError ( "You cannot specify - more than once" ) vargs . append ( "-" ) elif arg . startswith ( "--" ) : # Process long option value = None if "=" in arg : # The argument was specified like --keyword=value name , value = arg [ 2 : ] . split ( "=" , 1 ) # strip quotes if value is quoted like # --keyword='multiple words' value = value . strip ( '\'"' ) default = keywords . get ( name ) try : value = totype ( value , default ) except ( TypeError , ValueError ) : type_error ( name , value , type ( default ) ) else : # The argument was not specified with an equals sign... name = arg [ 2 : ] default = keywords . get ( name ) if type ( default ) is bool : # If this option is a boolean, it doesn't need a value; # specifying it on the command line means "do the # opposite of the default". value = not default else : # The next item in the argument list is the value, i.e. # --keyword value if not argv or argv [ 0 ] . startswith ( "-" ) : # Oops, there isn't a value available... just use # True, assuming this is a flag. value = True else : value = argv . pop ( 0 ) try : value = totype ( value , default ) except ( TypeError , ValueError ) : type_error ( name , value , type ( default ) ) # Store this option kwargs [ name ] = value elif arg . startswith ( "-" ) and ( cmd . shortopts or cmd . has_kwargs ) : # Process short option(s) # For each character after the '-'... for i in range ( 1 , len ( arg ) ) : char = arg [ i ] if cmd . has_kwargs : name = char default = keywords . get ( name ) elif char not in shortchars : continue else : # Get the long option name corresponding to this char name = shortchars [ char ] default = keywords [ name ] if isinstance ( default , bool ) : # If this option is a boolean, it doesn't need a value; # specifying it on the command line means "do the # opposite of the default". kwargs [ name ] = not default else : # This option requires a value... if i == len ( arg ) - 1 : # This is the last character in the list, so the # next argument on the command line is the value. value = argv . pop ( 0 ) else : # There are other characters after this one, so # the rest of the characters must represent the # value (i.e. old-style UNIX option like -Nname) value = arg [ i + 1 : ] # Remove leading equals sign if it's present. That # means the option/value were specified as opt=value # Then remove quotes. value = value . lstrip ( "=" ) . strip ( "'\"" ) try : kwargs [ name ] = totype ( value , default ) except ( TypeError , ValueError ) : type_error ( name , value , type ( default ) ) break else : # This doesn't start with "-", so just add it to the list of # positional arguments. vargs . append ( arg ) return vargs , kwargs