在Python中,我习惯使用
argparse
,在我看来它非常完美(也许是针对我的需求)。不幸的是,OptionParser
不允许像argparse
那样具有灵活性和功能。我现在特别关注以下约束:
How do I make mutually exclusive option lists? For e.g. a very small option list for a program called
test
.usage: test [-h] [-a | -b | -c] [-d] [filename]
I can write some code like:
# implement a ---------------------------------------------- opts.on( "-a", "--alpha", "implement alpha") do #... end
and so on. But then, I've no way to make
a
,b
andc
mutually exclusive unless I code a permutation of those and do some error handling. For e.g.test -ab #should through an error
In Python, I could do this in a very easy way:
# create an command line argument parser object cmd_line_parser = argparse.ArgumentParser() # create a mutually exclusive group cmd_line_group = cmd_line_parser.add_mutually_exclusive_group()
Secondly, I've no way of pairing
-d
with-a
unless I specifically write code for this permutation. This is insane.I've to write the
[OPTION]
list myself; I've no way to know if I am wrong or right unless and until I do a blackbox testing for all possible input permutations and map them to the blackbox list.Moreover, compulsory arguments again need to be handled using special code.
optparse
或其他库来处理这些约束条件的简单方法?