From 8fa2524e540554662ee10935a2b2b225f656eed3 Mon Sep 17 00:00:00 2001 From: Alex Grintsvayg Date: Thu, 23 Mar 2017 17:44:06 -0400 Subject: [PATCH] properly handle true/false cli args --- lbrynet/lbrynet_daemon/DaemonCLI.py | 29 ++++++++++++--------- tests/unit/lbrynet_daemon/test_DaemonCLI.py | 14 ++++++++-- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/lbrynet/lbrynet_daemon/DaemonCLI.py b/lbrynet/lbrynet_daemon/DaemonCLI.py index fd3bd9ad4..d0063e54b 100644 --- a/lbrynet/lbrynet_daemon/DaemonCLI.py +++ b/lbrynet/lbrynet_daemon/DaemonCLI.py @@ -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])) diff --git a/tests/unit/lbrynet_daemon/test_DaemonCLI.py b/tests/unit/lbrynet_daemon/test_DaemonCLI.py index e719e0c35..ac5ae4190 100644 --- a/tests/unit/lbrynet_daemon/test_DaemonCLI.py +++ b/tests/unit/lbrynet_daemon/test_DaemonCLI.py @@ -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))