properly handle true/false cli args

This commit is contained in:
Alex Grintsvayg 2017-03-23 17:44:06 -04:00
parent 41257c07e6
commit 8fa2524e54
2 changed files with 29 additions and 14 deletions

View file

@ -93,18 +93,6 @@ def main():
def print_help_response(help_response):
print help_response['help'] if 'help' in help_response else help_response
def guess_type(x):
if '.' in x:
try:
return float(x)
except ValueError:
# not a float
pass
try:
return int(x)
except ValueError:
return x
def parse_params(params):
if len(params) > 1:
@ -134,6 +122,23 @@ def get_params_from_kwargs(params):
return params_for_return
def guess_type(x):
if x in ('true', 'True', 'TRUE'):
return True
if x in ('false', 'False', 'FALSE'):
return False
if '.' in x:
try:
return float(x)
except ValueError:
# not a float
pass
try:
return int(x)
except ValueError:
return x
def print_help_suggestion():
print "See `{} help` for more information.".format(os.path.basename(sys.argv[0]))

View file

@ -9,6 +9,12 @@ class DaemonCLITests(unittest.TestCase):
self.assertEqual(3, DaemonCLI.guess_type('3'))
self.assertEqual('VdNmakxFORPSyfCprAD/eDDPk5TY9QYtSA==', DaemonCLI.guess_type('VdNmakxFORPSyfCprAD/eDDPk5TY9QYtSA=='))
self.assertEqual(0.3, DaemonCLI.guess_type('0.3'))
self.assertEqual(True, DaemonCLI.guess_type('TRUE'))
self.assertEqual(True, DaemonCLI.guess_type('true'))
self.assertEqual(True, DaemonCLI.guess_type('True'))
self.assertEqual(False, DaemonCLI.guess_type('FALSE'))
self.assertEqual(False, DaemonCLI.guess_type('false'))
self.assertEqual(False, DaemonCLI.guess_type('False'))
def test_get_params(self):
test_params = [
@ -16,13 +22,17 @@ class DaemonCLITests(unittest.TestCase):
'name=test',
'amount=5.3',
'n=5',
'address=bY13xeAjLrsjP4KGETwStK2a9UgKgXVTXu'
'address=bY13xeAjLrsjP4KGETwStK2a9UgKgXVTXu',
't=true',
'f=False',
]
test_r = {
'b64address': 'VdNmakxFORPSyfCprAD/eDDPk5TY9QYtSA==',
'name': 'test',
'amount': 5.3,
'n': 5,
'address': 'bY13xeAjLrsjP4KGETwStK2a9UgKgXVTXu'
'address': 'bY13xeAjLrsjP4KGETwStK2a9UgKgXVTXu',
't': True,
'f': False,
}
self.assertDictEqual(test_r, DaemonCLI.get_params_from_kwargs(test_params))