Simply put, how can I differentiate these two in test.py:
python test.py 1
python test.py '1'
Workaround is OK.
Edit:
- This workaround looks cool but too complex: argparse
- Let the invoker specify args later, in python code use
arg = input('Please enter either an integer or a string') - And other workarounds as presented in the answers of this question.
Thank you all for the replies. Every body +1.
The shell command line doesn’t support passing arguments of different types. If you want to have commands with arguments of different types you need to write your own command line or at least your own command parser.
Variant 1:
Usage:
python test.py "1 2 '3' '4'"Implementation:
Variant 2:
Usage:
Implementation:
(Of course, you’d probably want to use
raw_inputto read the command lines, andreadlinewhen it is available, that’s merely an example.)A much better solution would be to actually know what kind of arguments you’re expected to get and parse them as such, preferably by using a module like
argparse.