I have the following code which attempts to get the DUT VID from the invoked command line:
parser = argparse.ArgumentParser(description=\'A Test\',
argparse
is looking to create a callable type conversion from the 'type'
value:
def _get_value(self, action, arg_string):
type_func = self._registry_get('type', action.type, action.type)
if not _callable(type_func):
msg = _('%r is not callable')
raise ArgumentError(action, msg % type_func)
# convert the value to the appropriate type
try:
result = type_func(arg_string)
# ArgumentTypeErrors indicate errors
except ArgumentTypeError:
name = getattr(action.type, '__name__', repr(action.type))
msg = str(_sys.exc_info()[1])
raise ArgumentError(action, msg)
# TypeErrors or ValueErrors also indicate errors
except (TypeError, ValueError):
name = getattr(action.type, '__name__', repr(action.type))
msg = _('invalid %s value: %r')
raise ArgumentError(action, msg % (name, arg_string))
# return the converted value
return result
By default int()
is set to base 10. In order to accommodate base 16 and base 10 parameters we can enable auto base detection:
def auto_int(x):
return int(x, 0)
...
group.add_argument('--vid',
type=auto_int,
help='vid of DUT')
Note the type is update to be 'auto_int'
.
Not enough reputation to comment on the other answer.
If you don't want to define the auto_int
function, I found that this works very cleanly with a lambda.
group.add_argument('--vid',
type=lambda x: int(x,0),
help='vid of DUT')