argparse参数我有我的包的setup.py
指定为入口点main
功能,使用包才能通过命令行参数(见discussion here):如何设置从python脚本
# file with main routine specified as entry point in setup.py
import argparse
def main():
parser = argparse.ArgumentParser()
parser.add_argument('a', type=str, help='mandatory argument a')
args = parser.parse_args()
理想的情况下,我想在包的测试中使用与0123'建议的相同main
函数。在后一种情况下,我想从测试类中调用main
函数,并在函数调用之前设置(一些)命令行参数(否则将由于缺少参数而失败)。
# file in the tests folder calling the above main function
class TestConsole(TestCase):
def test_basic(self):
set_value_of_a()
main()
这可能吗?
为什么不把'kwargs'添加到'main',并且只有当它们是'None'时,才将它们设置为'parse_args'? –
@William Fernandes非常好,谢谢! – XabiFermal