我浏览了所有示例和答案,但在某种程度上,它们并没有满足我的需求。所以我会给她列出一个我需要更多帮助的场景,我希望这可以更多地解释这个想法。
最初的问题
我需要开发一个工具来获取文件来处理它,它需要一些可选的配置文件来配置工具。
所以我需要的是类似下面的东西
mytool.py file.text -config config-file.json
解决方案
这里是解决方案代码
import argparse
def main():
parser = argparse.ArgumentParser(description='This example for a tool to process a file and configure the tool using a config file.')
parser.add_argument('filename', help="Input file either text, image or video")
# parser.add_argument('config_file', help="a JSON file to load the initial configuration ")
# parser.add_argument('-c', '--config_file', help="a JSON file to load the initial configuration ", default='configFile.json', required=False)
parser.add_argument('-c', '--config', default='configFile.json', dest='config_file', help="a JSON file to load the initial configuration " )
parser.add_argument('-d', '--debug', action="store_true", help="Enable the debug mode for logging debug statements." )
args = parser.parse_args()
filename = args.filename
configfile = args.config_file
print("The file to be processed is", filename)
print("The config file is", configfile)
if args.debug:
print("Debug mode enabled")
else:
print("Debug mode disabled")
print("and all arguments are: ", args)
if __name__ == '__main__':
main()
我将在多个增强中展示解决方案以展示想法
第一轮:列出论据
将所有输入列为强制输入,因此第二个参数将是
parser.add_argument('config_file', help="a JSON file to load the initial configuration ")
当我们获得此工具的帮助命令时,我们会发现以下结果
(base) > python .\argparser_example.py -h
usage: argparser_example.py [-h] filename config_file
This example for a tool to process a file and configure the tool using a config file.
positional arguments:
filename Input file either text, image or video
config_file a JSON file to load the initial configuration
optional arguments:
-h, --help show this help message and exit
当我如下执行时
(base) > python .\argparser_example.py filename.txt configfile.json
结果是
The file to be processed is filename.txt
The config file is configfile.json
and all arguments are: Namespace(config_file='configfile.json', filename='filename.txt')
但配置文件应该是可选的,我从参数中删除了它
(base) > python .\argparser_example.py filename.txt
结果将是:
usage: argparser_example.py [-h] filename config_file
argparser_example.py: error: the following arguments are required: c
这意味着我们的工具有问题
第二轮:优化
因此,为了使其成为可选,我将程序修改如下
parser.add_argument('-c', '--config', help="a JSON file to load the initial configuration ", default='configFile.json', required=False)
帮助结果应该是
usage: argparser_example.py [-h] [-c CONFIG] filename
This example for a tool to process a file and configure the tool using a config file.
positional arguments:
filename Input file either text, image or video
optional arguments:
-h, --help show this help message and exit
-c CONFIG, --config CONFIG
a JSON file to load the initial configuration
所以当我执行程序时
(base) > python .\argparser_example.py filename.txt
结果是
The file to be processed is filename.txt
The config file is configFile.json
and all arguments are: Namespace(config_file='configFile.json', filename='filename.txt')
带有类似的参数
(base) > python .\argparser_example.py filename.txt --config_file anotherConfig.json
结果是
The file to be processed is filename.txt
The config file is anotherConfig.json
and all arguments are: Namespace(config_file='anotherConfig.json', filename='filename.txt')
第 3 轮:增强功能
要将标志名称从--config_file 更改为--config,同时保持变量名称不变,我们修改代码以包含dest='config_file',如下所示:
parser.add_argument('-c', '--config', help="a JSON file to load the initial configuration ", default='configFile.json', dest='config_file')
命令将是
(base) > python .\argparser_example.py filename.txt --config anotherConfig.json
要添加对具有调试模式标志的支持,我们需要在参数中添加一个标志以支持布尔调试标志。为了实现它,我添加了以下内容:
parser.add_argument('-d', '--debug', action="store_true", help="Enable the debug mode for logging debug statements." )
工具命令将是:
(carnd-term1-38) > python .\argparser_example.py image.jpg -c imageConfig,json --debug
结果是
The file to be processed is image.jpg
The config file is imageConfig,json
Debug mode enabled
and all arguments are: Namespace(config_file='imageConfig,json', debug=True, filename='image.jpg')