我想完成这样的事情:
-LoadFiles
-SourceFile "" -DestPath ""
-SourceFolder "" -DestPath ""
-GenericOperation
-SpecificOperation -Arga "" -Argb ""
-OtherOperation -Argc "" -Argb "" -Argc ""
Run Code Online (Sandbox Code Playgroud)
用户应该能够执行以下操作:
-LoadFiles -SourceFile"somePath"-DestPath"somePath"
要么
-LoadFiles -SourceFolder"somePath"-DestPath"somePath"
基本上,如果你有-LoadFiles,则需要在之后使用-SourceFile或-SourceFolder.如果你有-SourceFile,你需要-DestPath等.
其他参数的必需参数链是否可能?如果没有,我至少可以做一些事情,如果你有-SourceFile,你必须有-DestPath?
ran*_*ame 16
在调用您创建parse_args的ArgumentParser实例后,它会为您提供一个Namespace对象.只需检查一个参数是否存在,那么另一个参数也必须存在.喜欢:
args = parser.parse_args()
if ('LoadFiles' in vars(args) and
'SourceFolder' not in vars(args) and
'SourceFile' not in vars(args)):
parser.error('The -LoadFiles argument requires the -SourceFolder or -SourceFile')
Run Code Online (Sandbox Code Playgroud)
您可以在argparse中使用子解析器
import argparse
parser = argparse.ArgumentParser(prog='PROG')
parser.add_argument('--foo', required=True, help='foo help')
subparsers = parser.add_subparsers(help='sub-command help')
# create the parser for the "bar" command
parser_a = subparsers.add_parser('bar', help='a help')
parser_a.add_argument('bar', type=int, help='bar help')
print(parser.parse_args())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12800 次 |
| 最近记录: |