Commit aa7c11d3 authored by John Kirkwood's avatar John Kirkwood

Cast CLI args to int/float/None (best guess).

parent 0ef1ea9b
Pipeline #2801 passed with stages
in 36 seconds
......@@ -45,6 +45,19 @@ class Parser:
return option
return False
@staticmethod
def cast_val(value):
"""Attempt to cast CLI argument to int or float."""
if value == 'None':
return None
try:
return int(value)
except ValueError:
try:
return float(value)
except ValueError:
return value
def append(self, arg):
spec = inspect.getfullargspec(self.command.target)
filled = False
......@@ -57,6 +70,7 @@ class Parser:
if arg.count('=') == 1:
if arg.startswith('-'):
key, value = arg.lstrip('-').split('=')
value = self.cast_val(value)
option = self.get_option(key)
if option:
self.options[option.name] = value
......@@ -64,6 +78,7 @@ class Parser:
self.dashkwargs[key] = value
else:
key, value = arg.split('=', 1)
value = self.cast_val(value)
self.funckwargs[key] = value
else:
......@@ -75,4 +90,4 @@ class Parser:
else:
self.dashargs.append(stripped)
elif not filled:
self.funcargs.append(arg)
self.funcargs.append(self.cast_val(arg))
......@@ -17,6 +17,10 @@ import cli2
('run_module_missing', 'missinggggggg.foo'),
('run_module_nodoc', 'test_cli2.test_cli2'),
('run_module_builtin', 'datetime.datetime.now'),
('run_module_args_int', 'datetime.datetime 2019 2 1'),
('run_module_kwds_int', 'datetime.datetime year=2019 month=2 day=1'),
('run_module_args_None', 'datetime.datetime.now None'),
('run_module_args_float', 'builtins.round 1.888 1'),
('help_module', 'help cli2'),
('help_module_attr_notfound', 'help cli2.skipppp'),
('help_module_no_callables', 'help datetime'),
......
command: cli2 datetime.datetime.now None
retcode: 1
stdout:
FakeDatetime(2010, 2, 1, 0, 0)
command: cli2 builtins.round 1.888 1
retcode: 1
stdout:
1.9
command: cli2 datetime.datetime 2019 2 1
retcode: 1
stdout:
FakeDatetime(2019, 2, 1, 0, 0)
command: cli2 datetime.datetime year=2019 month=2 day=1
retcode: 1
stdout:
FakeDatetime(2019, 2, 1, 0, 0)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment