Skip to content

Commit

Permalink
properly handle true/false cli args
Browse files Browse the repository at this point in the history
  • Loading branch information
lyoshenka committed Mar 23, 2017
1 parent 41257c0 commit 8fa2524
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
29 changes: 17 additions & 12 deletions lbrynet/lbrynet_daemon/DaemonCLI.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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]))

Expand Down
14 changes: 12 additions & 2 deletions tests/unit/lbrynet_daemon/test_DaemonCLI.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,30 @@ def test_guess_type(self):
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 = [
'b64address=VdNmakxFORPSyfCprAD/eDDPk5TY9QYtSA==',
'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))

0 comments on commit 8fa2524

Please sign in to comment.