Commit a6859b4d authored by ∞'s avatar 💻

Merge branch '4-cast-arguments-to-int-float-None' into 'master'

Resolve "Attempt to cast arguments to int/float/None"

Closes #4

See merge request !7
parents 945a8b21 09287389
Pipeline #3667 passed with stages
in 44 seconds
......@@ -47,6 +47,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
......@@ -59,6 +72,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 and (not option.immediate or self.immediate):
self.options[option.name] = value
......@@ -68,6 +82,7 @@ class Parser:
else:
self.immediate = False
key, value = arg.split('=', 1)
value = self.cast_val(value)
self.funckwargs[key] = value
else:
......@@ -81,4 +96,4 @@ class Parser:
self.dashargs.append(stripped)
elif not filled:
self.immediate = False
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