Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix cli arguments type casting #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

e-tinkers
Copy link

Fix CLI arguments type casting

The JsonServer is designed to allow change the tcpport and disable autoaddmgr and autodeletemgr settings via command line arguments --tcpport 8081, --autoaddmgr False and --autodeltemgr False.

description of the bug

The current code does not works as per design intention as the python argparse library consider the default user input as ‘string’,

User input:

python JsonServer.py --tcpport 8081

will parsed by the argparse as a string instead of an integer for 8081:

{'serialport': None, 'autodeletemgr': False, 'configfilename': 'JsonServer.config', 'autoaddmgr': True, 'tcpport': '8081'}

Same issue for --autoaddmgr(and --autodeletemgr), where the user input argument is parsed as a string instead of bool.

python JsonServer.py --autoaddmgr False

result:

{'serialport': None, 'autodeletemgr': False, 'configfilename': 'JsonServer.config', 'autoaddmgr': 'False', 'tcpport': 8080}

proposed changes for fixing the issue

The solution for correctly cast the input argument for --tcpport can be solved by add type=int into the code:

parser.add_argument('--tcpport', type=int, default=8080)

Adding type=bool however doesn’t work for argparse library, instead it requires of using action as suggested by argparse documentation (see reference link below):

parse.add_argument(‘--autoaddmgr’, action=“store_false’, help='to disable autoaddmgr thread', default=True)

With action='store-false, simply add --autoaddmgr as command line argument without a value will disable the autoaddmgr thread. This is a little bit counter-intuitive, therefore a helper text would be helpful to explain the usage.

python JsonServer.py --autoaddmgr

Reference: https://docs.python.org/2.7/library/argparse.html#action

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant