我使用argparse
使用以下helper函数解析参数:
def get_cli_arguments():
parser = argparse.ArgumentParser(prog='Xtrayce')
parser.add_argument(
"-o", "--output",
default=get_default_output_path(),
help="Supply an output path.",
type=argparse.FileType('wb'),
)
parser.add_argument(
"-d", "--dry",
help="Don't save a file with the output.",
action="store_true",
)
parser.add_argument(
"-s", "--standard",
help="Also scan standard library and modules.",
action="store_true",
)
我希望每当用户指定--dry
标志时,都不会从--output
参数创建任何文件。你知道吗
当用户指定--dry
,同时仍然使用default=
和type=argparse.FileType("wb")
时,“取消”文件创建的最佳方式是什么?你知道吗
没有简单的方法可以通过默认的
ArgumentParser
实现这一点,因为在解析参数的过程中已经创建了文件。你知道吗您可以将
output
的类型更改为字符串,并在写入之前添加检查:或者不使用
dry
参数,您可以提供-
作为output
参数,它将写入sys.stdout
而不是文件。你知道吗从the docs:
相关问题 更多 >
编程相关推荐