我有一个实用程序,它允许用户读取他们的~/.aws/credentials文件和导出环境变量。
目前,CLI接口如下所示:
usage: aws-env [-h] [-n] profile
Extract AWS credentials for a given profile as environment variables.
positional arguments:
profile The profile in ~/.aws/credentials to extract credentials
for.
optional arguments:
-h, --help show this help message and exit
-n, --no-export Do not use export on the variables.我在这里要做的是提供一个ls子解析器,允许用户在他们的~/.aws/credentials中列出有效的配置文件名称。
接口应该是这样的:
$ aws-env ls
profile-1
profile-2...etcetera。是否有一种方法可以在a解析中本机执行此操作,以便在我的-h输出中出现一个选项,这表明ls是一个有效命令?
发布于 2017-04-03 21:04:33
如果您走subparsers路线,您可以定义两个解析器,'ls‘和’提取‘。‘'ls’不会有任何争论;‘提取’将取一个位置,'profile‘。
子解析器是可选的,(具有所需子解析器的Argparse),但是像当前定义的'profile‘是必需的。
另一种方法是定义两个选项,并省略位置。
'-ls', True/False, if True to the list
'-e profile', if not None, do the extract.或者您可以离开位置profile,但是使它是可选的(nargs='?')。
另一种可能是解析后查看profile值。如果它是一个字符串,如'ls',然后列出而不是提取。这似乎是最干净的选择,然而,使用不会记录这一点。
parser.add_argument('-l','--ls', action='store_true', help='list')
parser.add_argument('profile', nargs='?', help='The profile')或
sp = parser.add_subparsers(dest='cmd')
sp.add_parser('ls')
sp1 = sp.add_parser('extract')
sp1.add_argument('profile', help='The profile')所需的互斥组
gp = parser.add_mutually_exclusive_group(required=True)
gp.add_argument('--ls', action='store_true', help='list')
gp.add_argument('profile', nargs='?', default='adefault', help='The profile')生产:
usage: aws-env [-h] [-n] (--ls | profile)https://stackoverflow.com/questions/43192905
复制相似问题