您的位置 首页 > 数码极客

如何看懂python的help——如何卸载Python!

使用 argparse 模块像专业人士一样解析参数。

-- Seth Kenlon(作者)

如果你在使用 Python 进行开发,你可能会在终端中使用命令,即使只是为了启动 Python 脚本或使用 pip 安装 Python 模块。命令可能简单而单一:

$ ls

命令也可能需要参数:

$ ls example

命令也可以有选项或标志:

$ ls --color example

有时选项也有参数:

$ sudo firewall-cmd --list-all --zone home

参数

POSIX shell 会自动将你输入的内容作为命令分成数组。例如,这是一个简单的命令:

$ ls example

命令 ls 的位置是 $0,参数 example 位置是 $1。

你可以写一个循环迭代每项。确定它是否是命令、选项还是参数。并据此采取行动。幸运的是,已经有一个名为 argparse 的模块。

argparse

argparse 模块很容易集成到 Python 程序中,并有多种便利功能。例如,如果你的用户更改选项的顺序或使用一个不带参数的选项(称为布尔,意味着选项可以打开或关闭设置),然后另一个需要参数(例如 --color red),argparse 可以处理多种情况。如果你的用户忘记了所需的选项,那么 argparse 模块可以提供友好的错误消息。

要在应用中使用 argparse,首先要定义为用户提供的选项。你可以接受几种不同的参数,而语法一致又简单。

这是一个简单的例子:

#!/usr/bin/env python import argparse import sys def getOptions(args=[1:]): parser = arg(description="Parses command.") ("-i", "--input", help="Your input file.") ("-o", "--output", help="Your destination output file.") ("-n", "--number", type=int, help="A number.") ("-v", "--verbose",dest='verbose',action='store_true', help="Verbose mode.") options = (args) return options

此示例代码创建一个名为 getOptions 的函数,并告诉 Python 查看每个可能的参数,前面有一些可识别的字符串(例如 --input 或者 -i)。 Python 找到的任何选项都将作为 options 对象从函数中返回(options 是一个任意名称,且没有特殊含义。它只是一个包含函数已解析的所有参数的摘要的数据对象)。

默认情况下,Python 将用户给出的任何参数视为字符串。如果需要提取整数(数字),则必须指定选项 type=int,如示例代码中的 --number 选项。

如果你有一个只是关闭和打开功能的参数,那么你必须使用 boolean 类型,就像示例代码中的 --verbose 标志一样。这种选项只保存 True 或 False,用户用来指定是否使用标志。如果使用该选项,那么会激活 stored_true。

当 getOptions 函数运行时,你就可以使用 options 对象的内容,并让程序根据用户调用命令的方式做出决定。你可以使用测试打印语句查看 options 的内容。将其添加到示例文件的底部:

print(getOptions())

然后带上参数运行代码:

$ python3 . -i foo -n 4 Namespace(input='foo', number=4, output=None, verbose=False)

检索值

示例代码中的 options 对象包含了用户提供的选项后面的值(或派生的布尔值)。例如,在示例代码中,可以通过 o 来检索 --number。

options = getOptions([1:]) if o: print("Verbose mode on") else: print("Verbose mode off") prin) prin) print(o) # 这里插入你的 Python 代码

示例中的布尔选项 --verbose 是通过测试 o 是否为 True(意味着用户使用了 --verbose 标志)或 False(用户没有使用 --verbose 标志),并采取相应的措施。

帮助和反馈

argparse 还包含一个内置的 --help(简写 -h)选项,它提供了有关如何使用命令的提示。这是从你的代码派生的,因此生成此帮助系统不需要额外的工作:

$ . --help usage: exam [-h] [-i INPUT] [-o OUTPUT] [-n NUMBER] [-v] Parses command. optional arguments: -h, --help show this help message and exit -i INPUT, --input INPUT Your input file. -o OUTPUT, --output OUTPUT Your destination output file. -n NUMBER, --number NUMBER A number. -v, --verbose Verbose mode.

像专业人士一样用 Python 解析

这是一个简单的示例,来演示如何在 Python 应用中的解析参数以及如何快速有效地记录它的语法。下次编写 Python 脚本时,请使用 argparse 为其提供一些选项。你以后会感到自得,你的命令不会像一个快速的临时脚本,更像是一个“真正的” Unix 命令!

以下是可用于测试的示例代码:

#!/usr/bin/env python3 # GNU All-Permissive License # Copying and distribution of this file, with or without modification, # are permitted in any medium without royalty provided the copyright # notice and this notice are preserved. This file is offered as-is, # without any warranty. import argparse import sys def getOptions(args=[1:]): parser = arg(description="Parses command.") ("-i", "--input", help="Your input file.") ("-o", "--output", help="Your destination output file.") ("-n", "--number", type=int, help="A number.") ("-v", "--verbose",dest='verbose',action='store_true', help="Verbose mode.") options = (args) return options options = getOptions([1:]) if o: print("Verbose mode on") else: print("Verbose mode off") prin) prin) print(o)

via:

作者: Seth Kenlon 选题: lujun9972 译者: geekpi 校对: wxy

本文由 LCTT 原创编译, Linux中国 荣誉推出

点击“了解更多”可访问文内链接

责任编辑: 鲁达

1.内容基于多重复合算法人工智能语言模型创作,旨在以深度学习研究为目的传播信息知识,内容观点与本网站无关,反馈举报请
2.仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证;
3.本站属于非营利性站点无毒无广告,请读者放心使用!

“如何看懂python的help,如何卸载Python,如何看懂python的库,如何安装python,如何卸载python,如何用python画图”边界阅读