简体   繁体   English

在Python中,如何以交互方式提供命令行参数

[英]In Python, how to supply the command line arguments in interactive mode

I am new to Python. 我是Python的新手。 I want to run a software in interactive mode. 我想以交互模式运行软件。 In the manual it says the usage 在手册中它说明了用法

python experiment.py --config config.yaml --out result/ python experiment.py --config config.yaml --out result /

The question is, how can I supply the command line arguments to experiment.py in interactive mode? 问题是,如何在交互模式下向experiment.py提供命令行参数?

The commandline arguments that eg optparse and argparse uses are taken by default from sys.argv element 1 and up. 例如optparseargparse使用的命令行参数默认来自sys.argv元素1及以上。 You can always do: 你可以随时做:

import sys
sys.argv[1:] = ['--config', 'config.yaml', '--out', 'result/']

Although eg in argparse you can provide the arguments explicitly to .parse_args() as well and then that method will not inspect sys.argv 虽然例如在argparse你可以显式地向.parse_args()提供参数,然后该方法不会检查sys.argv

If I understood you correctly you need something like this: 如果我理解正确你需要这样的东西:

while True:
    query = raw_input("> ")
    if query == "exit":
        break
    # do something useful

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM