小编Car*_*l H的帖子

将 ArgumentParser 移至单独的文件中

我的 argparser 变得很长。我想将它移到一个单独的文件中以保持我的主脚本干净。现在我有:

主要.py

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--test',
                    default=0.1,
                    type=float,
                    help='test')
# Many more arguments
args = parser.parse_args()
# My main code
print("Argument in my code:{}".format(args.test))
...
Run Code Online (Sandbox Code Playgroud)

有没有办法将其变成两个文件以保持我的主文件干净?

主要.py

import parsing_file
# My main code
print("Argument in my code:{}".format(args.test))
Run Code Online (Sandbox Code Playgroud)

解析文件.py

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--test',
                    default=0.1,
                    type=float,
                    help='test')
# Many more arguments
args = parser.parse_args()
Run Code Online (Sandbox Code Playgroud)

python argparse

5
推荐指数
1
解决办法
1801
查看次数

标签 统计

argparse ×1

python ×1