如何在Spyder中使用可选参数执行? [英] How to execute with optional argument in Spyder?

查看:865
本文介绍了如何在Spyder中使用可选参数执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何在Spyder上执行带有可选参数的程序.我知道如何将变量传递给它,但是我的程序使用argparse,并且我想使用"-h"或"--help"选项执行它,代码如下.

I don't know how to execute a program with optional arguments on Spyder. I know how to pass variables to it, but my program uses argparse, and I want to execute it with the "-h" or "--help" option, the code is the following one

import argparse
parser = argparse.ArgumentParser()
parser.parse_args()

目前,它仅具有默认的可选参数"-h"/-help",我尝试将其放在命令行选项"上,但不起作用.

For now, it only has the default optional argument of "-h"/"--help", I tried putting it on "Command line options" but it doesn't work.

推荐答案

您必须定义参数才能使用它们.看起来它只是使用默认的argparse方法,该方法仅在构造函数中定义了help方法.

You would have to define the arguments in order for them to be used. It looks like it is just using the default argparse method which only defines the help method in the constructor.

在此处检查文档: https://docs.python.org/3/library/argparse.html

这是解析我之前使用过的args的示例方法:

Here is an example method for parsing the args I have used before:

import argparse


def process_args(source=None):
    parser = argparse.ArgumentParser(prog='my-awesome-program')
    parser.add_argument('--value1', dest='value1', type=str)
    parser.add_argument('--value2', dest='value2', type=str)

    args = parser.parse_args(source)
    return args


def main():
    args = process_args()
    args = vars(args)

    my_value_1 = args['value1']
    my_value_2 = args['value2']
    print('%s, %s' % (my_value_1, my_value_2))


if __name__ == '__main__':
    main()

这篇关于如何在Spyder中使用可选参数执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆