在 Python 中检查 Argparse 的参数

本篇文章将讨论 argparse 的使用,我们将使用条件语句和 Python 中的参数名称检查 argparse 中是否存在参数。


在 Python 中检查 argparse 的参数

在命令行中使用 Python 的 argparse 库来编写用户友好的界面。 例如,我们可以使用脚本名称运行脚本并提供运行脚本所需的参数。

我们可以在脚本文件的名称后添加参数,argparse 库会将这些参数转换为可以在脚本中使用的对象来执行所需的任务。 我们还可以在每个参数中附加帮助、用法和错误消息,以帮助用户。

例如,如果用户输入无效参数,argparse 库将显示错误以及用户应如何输入参数。 例如,让我们讨论 argparse 库的一个简单示例。

请参阅下面的代码。

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

输出:

PS C:\Users\ammar> python test.py -h
usage: test.py [-h]

optional arguments:
  -h, --help  show this help message and exit

要运行上述脚本,我们必须在 cmd(命令提示符)中编写 python test.py -h 并确保将 Python 添加到 Windows 上的系统路径或环境变量中。 如果未添加,我们可以使用 python.exe 文件的完整路径。

我们还必须确保命令提示符的当前目录设置为 Python 文件的目录; 如果不是,我们必须提供 Python 文件的完整路径。

例如,假设我们必须将 Python 添加到系统路径,并且我们也不在 Python 文件的当前目录中。 我们必须将上面输出中的第一行更改为下面一行。

& C:/Users/ammar/python.exe "c:/Users/ammar/test.py" -h

上面的行在 Windows PowerShell 中进行了测试,对于 cmd,我们必须删除行开头的 &。 在上面的输出中,我们可以观察到只有一个参数用于帮助。

我们可以使用 add_argument() 函数在参数解析器中添加参数。 我们可以设置两种类型的参数:一种是位置参数,另一种是可选参数。

我们必须在参数前使用单 - 或双连字符 - 使其成为可选的。 我们还可以使用 help 参数为参数添加帮助,并将其值设置为 add_argument() 函数内的字符串。

我们可以使用 ArgumentParser() 函数内的 description 和 epilog 参数在帮助部分前后添加文本。 parse_args() 将命令提示符中传递的参数转换为对象并返回它们,可用于稍后执行操作。

例如,让我们从用户那里获取一个字符串参数并显示它。 请参阅下面的代码。

import argparse
import string
My_parser = argparse.ArgumentParser(description="text before help.....", epilog="text after help.....")
My_parser.add_argument("firstArg", help="You can use string here", type=str)
My_args = My_parser.parse_args()

print(My_args.firstArg)

输出:

PS C:\Users\ammar> python test.py
usage: test.py [-h] firstArg
test.py: error: the following arguments are required: firstArg


PS C:\Users\ammar> python test.py -h
usage: test.py [-h] firstArg

text before help.....

positional arguments:
  firstArg    You can use string here

optional arguments:
  -h, --help  show this help message and exit

text after help.....


PS C:\Users\ammar> python test.py hello
hello

我们已经运行了 3 次上述 Python 文件,可以在输出中看到结果。 第一次,我们在没有任何参数的情况下运行该文件,并出现一条错误消息,指出需要一个参数,但未通过。

第二次,我们使用-h 参数运行脚本,这个参数用于帮助,代码显示了脚本的用法和帮助。 我们使用命令提示符上显示的字符串第三次运行脚本。

我们可以使用 add_argument() 函数的类型参数来设置参数的数据类型,例如 str 用于字符串,int 用于整数数据类型。 所有传递的参数都存储在 My_args 变量中,我们可以使用此变量来检查是否传递了特定参数。


检查是否设置了 argparse 可选参数

在可选参数的情况下,如果没有传递参数,则 parse_args() 方法将为该特定参数返回 None 。 我们可以使用条件语句来检查参数是否为 None,如果参数为 None,则表示未传递参数。

例如,让我们添加一个可选参数并检查参数是否被传递,并相应地显示结果。 请参阅下面的代码。

import argparse
import string
My_parser = argparse.ArgumentParser(description="text before help.....", epilog="text after help.....")
My_parser.add_argument("-firstArg", help="You can use string here", type=str)
My_args = My_parser.parse_args()

if My_args.firstArg is not None:
    print("Argument passed")
else:
    print("No Argument passed")

输出:

PS C:\Users\ammar> python test.py
No Argument passed

PS C:\Users\ammar> python test.py -firstArg hello
Argument passed

我们运行了两次上面的脚本,有参数和没有参数,并相应地显示了一个文本。 我们可以使用带有条件语句的 is not None 和 is None 语句来确定是否传递参数。

%> 请注意 ,在可选参数的情况下,我们还必须在传递参数之前使用它们的名称。 确保我们要检查的参数没有设置默认值,因为在默认值的情况下,参数永远不会是 None 。

例如,让我们在上面的代码中使用 add_argument() 函数中的 default 关键字添加一个默认值,然后重复上面的过程。 请参阅下面的代码。

import argparse
import string
My_parser = argparse.ArgumentParser(description="text before help.....", epilog="text after help.....")
My_parser.add_argument("-firstArg", help="You can use string here", type=str, default="default_value")
My_args = My_parser.parse_args()

if My_args.firstArg is not None:
    print("Argument passed")
else:
    print("No Argument passed")

输出:

PS C:\Users\ammar> python test.py
Argument passed

PS C:\Users\ammar> python test.py -firstArg hello
Argument passed

我们可以在上面的输出中观察到,如果我们不传递参数,由于默认值,代码仍然会显示传递的参数。 我们可以多次使用 add_argument() 函数来添加多个参数。

如果我们想限制用户对输入的选择,我们可以在 add_argument() 中使用 choices 参数,并将其值设置为一个值列表,该列表只允许作为参数传递给脚本。

你可能感兴趣的:(Python,实用技巧,python,开发语言)