Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
getopt Namespace Reference

Data Structures

class  GetoptError
 

Functions

def getopt
 
def do_longs
 
def long_has_args
 
def do_shorts
 
def short_has_arg
 

Variables

list __all__ = ["GetoptError","error","getopt"]
 
 error = GetoptError
 

Detailed Description

Parser for command line options.

This module helps scripts to parse the command line arguments in
sys.argv.  It supports the same conventions as the Unix getopt()
function (including the special meanings of arguments of the form `-'
and `--').  Long options similar to those supported by GNU software
may be used as well via an optional third argument.  This module
provides a single function and an exception:

getopt() -- Parse command line options
GetoptError -- exception (class) raised with 'opt' attribute, which is the
option involved with the exception.

Function Documentation

def getopt.do_longs (   opts,
  opt,
  longopts,
  args 
)

Definition at line 78 of file getopt.py.

References long_has_args().

78 
79 def do_longs(opts, opt, longopts, args):
80  try:
81  i = opt.index('=')
82  except ValueError:
83  optarg = None
84  else:
85  opt, optarg = opt[:i], opt[i+1:]
86 
87  has_arg, opt = long_has_args(opt, longopts)
88  if has_arg:
89  if optarg is None:
90  if not args:
91  raise GetoptError('option --%s requires argument' % opt, opt)
92  optarg, args = args[0], args[1:]
93  elif optarg:
94  raise GetoptError('option --%s must not have an argument' % opt, opt)
95  opts.append(('--' + opt, optarg or ''))
96  return opts, args
97 
98 # Return:
99 # has_arg?
# full option name
def getopt.do_shorts (   opts,
  optstring,
  shortopts,
  args 
)

Definition at line 121 of file getopt.py.

References short_has_arg().

122 def do_shorts(opts, optstring, shortopts, args):
123  while optstring != '':
124  opt, optstring = optstring[0], optstring[1:]
125  if short_has_arg(opt, shortopts):
126  if optstring == '':
127  if not args:
128  raise GetoptError('option -%s requires argument' % opt,
129  opt)
130  optstring, args = args[0], args[1:]
131  optarg, optstring = optstring, ''
132  else:
133  optarg = ''
134  opts.append(('-' + opt, optarg))
135  return opts, args
def getopt.getopt (   args,
  shortopts,
  longopts = [] 
)
getopt(args, options[, long_options]) -> opts, args

Parses command line options and parameter list.  args is the
argument list to be parsed, without the leading reference to the
running program.  Typically, this means "sys.argv[1:]".  shortopts
is the string of option letters that the script wants to
recognize, with options that require an argument followed by a
colon (i.e., the same format that Unix getopt() uses).  If
specified, longopts is a list of strings with the names of the
long options which should be supported.  The leading '--'
characters should not be included in the option name.  Options
which require an argument should be followed by an equal sign
('=').

The return value consists of two elements: the first is a list of
(option, value) pairs; the second is the list of program arguments
left after the option list was stripped (this is a trailing slice
of the first argument).  Each option-and-value pair returned has
the option as its first element, prefixed with a hyphen (e.g.,
'-x'), and the option argument as its second element, or an empty
string if the option has no argument.  The options occur in the
list in the same order in which they were found, thus allowing
multiple occurrences.  Long and short options may be mixed.

Definition at line 35 of file getopt.py.

References do_longs(), and do_shorts().

35 
36 def getopt(args, shortopts, longopts = []):
37  """getopt(args, options[, long_options]) -> opts, args
38 
39  Parses command line options and parameter list. args is the
40  argument list to be parsed, without the leading reference to the
41  running program. Typically, this means "sys.argv[1:]". shortopts
42  is the string of option letters that the script wants to
43  recognize, with options that require an argument followed by a
44  colon (i.e., the same format that Unix getopt() uses). If
45  specified, longopts is a list of strings with the names of the
46  long options which should be supported. The leading '--'
47  characters should not be included in the option name. Options
48  which require an argument should be followed by an equal sign
49  ('=').
50 
51  The return value consists of two elements: the first is a list of
52  (option, value) pairs; the second is the list of program arguments
53  left after the option list was stripped (this is a trailing slice
54  of the first argument). Each option-and-value pair returned has
55  the option as its first element, prefixed with a hyphen (e.g.,
56  '-x'), and the option argument as its second element, or an empty
57  string if the option has no argument. The options occur in the
58  list in the same order in which they were found, thus allowing
59  multiple occurrences. Long and short options may be mixed.
60 
61  """
62 
63  opts = []
64  if type(longopts) == type(""):
65  longopts = [longopts]
66  else:
67  longopts = list(longopts)
68  while args and args[0].startswith('-') and args[0] != '-':
69  if args[0] == '--':
70  args = args[1:]
71  break
72  if args[0].startswith('--'):
73  opts, args = do_longs(opts, args[0][2:], longopts, args[1:])
74  else:
75  opts, args = do_shorts(opts, args[0][1:], shortopts, args[1:])
76 
77  return opts, args
def getopt.long_has_args (   opt,
  longopts 
)

Definition at line 100 of file getopt.py.

101 def long_has_args(opt, longopts):
102  possibilities = [o for o in longopts if o.startswith(opt)]
103  if not possibilities:
104  raise GetoptError('option --%s not recognized' % opt, opt)
105  # Is there an exact match?
106  if opt in possibilities:
107  return 0, opt
108  elif opt + '=' in possibilities:
109  return 1, opt
110  # No exact match, so better be unique.
111  if len(possibilities) > 1:
112  # XXX since possibilities contains all valid continuations, might be
113  # nice to work them into the error msg
114  raise GetoptError('option --%s not a unique prefix' % opt, opt)
115  assert len(possibilities) == 1
116  unique_match = possibilities[0]
117  has_arg = unique_match.endswith('=')
118  if has_arg:
119  unique_match = unique_match[:-1]
120  return has_arg, unique_match
def getopt.short_has_arg (   opt,
  shortopts 
)

Definition at line 136 of file getopt.py.

137 def short_has_arg(opt, shortopts):
138  for i in range(len(shortopts)):
139  if opt == shortopts[i] != ':':
140  return shortopts.startswith(':', i+1)
141  raise GetoptError('option -%s not recognized' % opt, opt)

Variable Documentation

list __all__ = ["GetoptError","error","getopt"]

Definition at line 20 of file getopt.py.

error = GetoptError

Definition at line 33 of file getopt.py.