eki*_*iim 5 python arguments argparse python-3.x
当前,我有一个正在构建的CLI工具,我想给它这种形式的表格。
usage: PROG SUBPARSER [-h]
(-l {optionA,optionB,optionC} | -s TERM [-a, [-b, [-c]]])
Run Code Online (Sandbox Code Playgroud)
我正在做的是有一个主cli模块,它将从我要向用户公开的所有模块中导入所有arguments_parser函数,并将其动态添加为主解析器。sub parsers
以下是python代码的功能,该功能将解析器添加到主解析器中,成为解析器,其类型ArgumentParser可能是根解析器,也可能是子解析器。(我对每个模块都这样做,因此它们的方法作为CLI公开)。
现在,我要在这种特殊情况下尝试做的事情是,让一个命令PROG具有一个第一个参数SUBPARSER,该参数具有两个(可能更多)互斥的参数序列(不创建新的子解析器),说我有两个功能,search并list因此搜索和列表可以有共同的参数(这将assing向子解析器不是组),但也有标志和参数是一款专用于使用--list或者--search,为了样订做命令
PROG SUBARSER --list optionA -a -o -b
PROG SUBARSER --list optionA -a -o
PROG SUBARSER --list optionA -a -b
PROG SUBARSER --list optionA -a
PROG SUBARSER --list optionA
PROG SUBARSER --search TERM -a -k
PROG SUBARSER --search TERM -c
PROG SUBARSER --search TERM
Run Code Online (Sandbox Code Playgroud)
I tried adding nested groups, with mutually exclusive and regular groups to the parser, but It doesn't allow me to (or at least I haven't found the way), to have mutually exclusive groups with multiple arguments, not just one flag or attribute.
This is what I have so far, that doesn't crash and actually runs usefully.
usage: PROG SUBPARSER [-h]
[-l {all,draft,staged,publish,build,not-build} | -s SEARCH]
Run Code Online (Sandbox Code Playgroud)
usage: PROG SUBPARSER [-h]
(-l {optionA,optionB,optionC} | -s TERM [-a, [-b, [-c]]])
Run Code Online (Sandbox Code Playgroud)
Please don't mind the help strings.
Any help?