Python Command Line Option Parsing

时间:2019-04-08 12:58:04

标签: python python-3.x getopt

I've a Python script and run it from command line with possible arguments. After some arguments should add value.

For example:

[ab:c]

I am using getopt.getopt() Python function to get arguments and values. I know if after an argument is colon means need to add value to the related argument. In this case: 'b'

How can I handle if user add arg. '-b' but missed to add value for -b but add the 3rd oprtion to command:

python3 mypythoncmd -a -b -c

If I am using getopt.getopt() function result is:

[('-a',''),('-b','-c')]

...but this wrong because '-c' is an another argument.

Is possible to handle it somehow? Missing value from arg throw and error?

Thanks

0 个答案:

没有答案