"""Handle command line arguments.
This module contains functions to parse and access the arguments given
to the program on the command line.
# Symbolic constants for the indexes into an argument specifier tuple.
('help', 0, Bool
, 'print help and exit'),
def parseArgs(title
, argv
, argSpecs
, filesOK
):
"""Parse and check command line arguments.
Scan the command line arguments in *argv* according to the argument
specifier *argSpecs*. Return **None** if there are no errors in
the arguments, otherwise return an error string describing the error.
This function must be called to initialise this module.
title -- The name of the program. This is used when returning
error messages or help text.
argv -- A sequence containing the arguments given to the program.
argSpecs -- A sequence of argument specifiers. Each specifier describes
a valid command line argument and consists of 4 or 5 items:
- The argument name (without a leading minus sign **-**).
- A boolean value, true if the argument is mandatory.
- This should be **Args.Bool** if the argument has no option.
Otherwise it should be a string describing the option
required for this argument. This is used when printing help.
- A short string describing the argument.
- The default value of the argument. This should only be used
for non-mandatory arguments expecting an option.
('foreground', 0, 'colour', 'colour of text', 'black'),
('geometry', 0, 'spec', 'geometry of initial window'),
('server', 1, 'ompserver', 'ompserver to connect to'),
('silent', 0, Args.Bool, 'do not sound bell'),
errMsg
= title
+ ' command line error: '
argSpecs
= helpSpec
+ argSpecs
if len(spec
) >= SPEC_LENGTH
:
knownKeys
= argSpecDic
.keys()
return errMsg
+ 'missing argument to \'' + arg
+ '\' option.'
if len(spec
) >= SPEC_LENGTH
:
if type(spec
[DEFAULT
]) == types
.IntType
:
value
= string
.atoi(value
)
elif type(spec
[DEFAULT
]) == types
.FloatType
:
value
= string
.atof(value
)
sys
.exc_traceback
= None # Clean up object references
return errMsg
+ 'cannot convert string \'' + value
+ \
'\' to ' + typeStr
+ ' for option \'-' + key
+ '\'.'
return _helpString(title
, argSpecs
)
if not filesOK
and len(_fileList
) > 0:
return errMsg
+ 'unknown option \'' + str(_fileList
[0]) + '\'.'
return errMsg
+ 'unknown options ' + str(_fileList
) + '.'
if spec
[MANDATORY
] and get(spec
[NAME
]) is None:
_missing
.append(spec
[NAME
])
return errMsg
+ 'required argument \'-' + \
str(_missing
[0]) + '\' is missing.'
return errMsg
+ 'required arguments ' + \
str(map(lambda s
: '-' + s
, _missing
)) + ' are missing.'
def _helpString(title
, argSpecs
):
width
= len(spec
[NAME
]) + 1
width
= len(spec
[NAME
]) + 4 + len(spec
[TYPE
])
rtn
= title
+ ' command line arguments:'
format
= '\n %-' + str(max) + 's %s'
if mandatory
and spec
[MANDATORY
] or not mandatory
and not spec
[MANDATORY
]:
rtn
= rtn
+ '\n Mandatory arguments:'
rtn
= rtn
+ '\n Optional arguments (defaults in parentheses):'
arg
= '-%s <%s>' % (spec
[NAME
], spec
[TYPE
])
if len(spec
) >= SPEC_LENGTH
:
if type(spec
[DEFAULT
]) == types
.StringType
:
definition
= spec
[HELP
] + ' (' + spec
[DEFAULT
] + ')'
definition
= spec
[HELP
] + ' (' + str(spec
[DEFAULT
]) + ')'
rtn
= rtn
+ format
% (arg
, definition
)
return configDict
.has_key(key
)