简体   繁体   English

使用argparse允许未知参数

[英]Allow unknown arguments using argparse

I have a python script that requires the user to enter two arguments to run it, the arguments could be named anything. 我有一个python脚本,要求用户输入两个参数来运行它,这些参数可以命名为任何东西。

I have also used argparse to allow the users to use a switch '-h' to get instructions of what is required to run the script. 我还使用了argparse,以允许用户使用开关'-h'来获取运行脚本所需的指令。

The problem is that now I have used argparse I am getting an error when I pass my two randomly named arguments with the script. 问题是,现在我使用argparse时,在脚本中传递两个随机命名的参数时出现错误。

import argparse

parser = argparse.ArgumentParser(add_help=False)

parser.add_argument('-h', '--help', action='help',
                    help='To run this script please provide two arguments')
parser.parse_args()

currently when I run python test.py arg1 arg2 the error is 当前当我运行python test.py arg1 arg2时 ,错误是

error: unrecognized arguments: arg1 arg2

I would like the code to allow the user to run test.py with a -h if required to see the instructions but also allow them to run the script with any two arguments as well. 我希望该代码允许用户使用-h运行test.py(如果需要查看说明),但也允许他们使用任意两个参数运行脚本。

Resolution with help tag to provide the user with context regarding the arguments required. 带有帮助标签的分辨率 ,可为用户提供有关所需参数的上下文。

   parser = argparse.ArgumentParser(add_help=False)

    parser.add_argument('-h', '--help', action='help', help='To run this script please provide two arguments: first argument should be your scorm package name, second argument should be your html file name. Note: Any current zipped folder in the run directory with the same scorm package name will be overwritten.')
    parser.add_argument('package_name', action="store",  help='Please provide your scorm package name as the first argument')
    parser.add_argument('html_file_name', action="store", help='Please provide your html file name as the second argument')

    parser.parse_args()
import argparse

parser = argparse.ArgumentParser(description='sample')

# Add mandatory arguments
parser.add_argument('arg1', action="store")
parser.add_argument('arg2', action="store")

# Parse the arguments
args = parser.parse_args()
# sample usage of args
print (float(args.arg1) + float(args.arg2))

You need to add those arguments to the parser: 您需要将这些参数添加到解析器中:

parser.add_argument("--arg1", "-a1", dest='arg1', type=str)
parser.add_argument("--arg2","-a2", dest='arg2', type=str)

If those arguments don't have the param required=true , you will be able to call the program without this arguments, so you can run the program with only the -h flag. 如果这些参数没有参数required = true ,则可以在没有此参数的情况下调用该程序,因此可以仅使用-h标志运行该程序。 To run the program with the arguments: 要使用参数运行程序:

python test.py --arg1 "Argument" --arg2 "Argument"

Then, to have the arguments in variables you have to read them: 然后,要在变量中包含参数,您必须阅读它们:

args = parser.parse_args()
argument1=args.arg1
argument2=args.arg2

Try the following code :- 尝试以下代码:-

 import argparse

 parser = argparse.ArgumentParser(add_help=False)

 parser.add_argument('-h', '--help', action='help',
                help='To run this script please provide two arguments')
 parser.add_argument('arg1')
 parser.add_argument('arg2')

 args, unknown = parser.parse_known_args()

All your unknown arguments will be parsed in unknown and all known in args . 您所有未知的参数将在unknown解析,而所有args均在args解析。

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

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