我有这段代码,总体上我很满意:
import argparse
servers = [ "ApaServer", "BananServer", "GulServer", "SolServer", "RymdServer",
"SkeppServer", "HavsServer", "PiratServer", "SvartServer", "NattServer", "SovServer" ]
parser = argparse.ArgumentParser(description="A program to update components on servers.")
group = parser.add_mutually_exclusive_group()
group.add_argument('-l', '--list', dest="update", action='store_false', default=False, help='list server components')
group.add_argument('-u', '--updatepom', dest="update", action='store_true', help='update server components')
parser.add_argument('-o', '--only', nargs='*', choices=servers, help='Space separated list of case sensitive server names to process')
parser.add_argument('-s', '--skip', nargs='*', choices=servers, help='Space separated list of case sensitive server names to exclude from processing')
args = parser.parse_args()
我喜欢choice=servers可以帮我验证输入的服务器名称,这样我就不必自己验证了。然而,有太多有效选项会让帮助输出看起来很糟糕:
usage: args.py [-h] [-l | -u]
[-o [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} ...]]]
[-s [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} ...]]]
A program to update components on servers.
optional arguments:
-h, --help show this help message and exit
-l, --list list server components
-u, --updatepom update server components
-o [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} ...]], --only [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} ...]]
Space separated list of case sensitive server names to
process
-s [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} ...]], --skip [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} [{ApaServer,BananServer,GulServer,SolServer,RymdServer,SkeppServer,HavsServer,PiratServer,SvartServer,NattServer,SovServer} ...]]
Space separated list of case sensitive server names to
exclude from processing
我该选择哪种方式,如果我想要:
- 漂亮的(大部分是)自动生成的帮助输出
- 验证给定到 -o 或 -s 选项的条目是否在 "servers" 中。
额外奖励:
- 是否可能对服务器名称进行不区分大小写的字符串匹配?
添加
我尝试使用 michaelfilms 的建议,在上述输出中删除 -o
-s
选项,并添加此部分:
server optional arguments:
Valid server names are: ApaServer, BananServer, GulServer, SolServer,
RymdServer, SkeppServer, HavsServer, PiratServer, SvartServer,
NattServer, SovServer
我认为它看起来相当不错,但我确实需要为-o
和-s
选项提供帮助,否则用户将不会知道它们。因此,如果使用这种方法,我还没有完全达到目标。
metavar=''
会导致错误。把它设置为除空字符串以外的任何内容(比如参数名称)就可以解决这个问题了。 - user3995702无效选项:500(可从0、1、2、3、4、5、6、7、8、9、10、11中选择)
- Michael Anderson