python 命令行 参数_Python命令行参数

python 命令行 参数

Python Command line arguments are input parameters passed to the script when executing them. Almost all programming language provide support for command line arguments. Then we also have command line options to set some specific options for the program.

Python命令行参数是执行它们时传递给脚本的输入参数。 几乎所有的编程语言都支持命令行参数。 然后,我们还有命令行选项可以为程序设置一些特定选项。

Python命令行参数 (Python Command Line Arguments)

There are many options to read python command line arguments. The three most common ones are:

有很多选项可以读取python命令行参数。 三种最常见的是:

  1. Python sys.argv

    Python sys.argv
  2. Python getopt module

    Python getopt模块
  3. Python argparse module

    Python argparse模块

Let’s look through simple program to learn how to read and use python command line arguments.

让我们看一个简单的程序,以学习如何阅读和使用python命令行参数。

Python sys模块 (Python sys module)

Python sys module stores the command line arguments into a list, we can access it using sys.argv. This is very useful and simple way to read command line arguments as String. Let’s look at a simple example to read and print command line arguments using python sys module.

Python sys模块将命令行参数存储到列表中,我们可以使用sys.argv进行访问。 这是将命令行参数读取为String的非常有用且简单的方法。 让我们看一个使用python sys模块读取和打印命令行参数的简单示例。

import sys

print(type(sys.argv))
print('The command line arguments are:')
for i in sys.argv:
    print(i)

Below image illustrates the output of a sample run of above program.

下图显示了以上程序的示例运行的输出。

There are many other useful functions in sys module, read them in detail at python sys module.

sys模块中还有许多其他有用的功能,请在python sys module上详细阅读它们。

Python getopt模块 (Python getopt module)

Python getopt module is very similar in working as the C getopt() function for parsing command-line parameters.

Python的getopt模块与C的getopt()函数用于解析命令行参数非常相似。

Python getopt module is useful in parsing command line arguments where we want user to enter some options too. Let’s look at a simple example to understand this.

Python getopt模块在解析命令行参数时非常有用,我们也希望用户也输入一些选项。 让我们看一个简单的例子来理解这一点。

import getopt
import sys

argv = sys.argv[1:]
try:
    opts, args = getopt.getopt(argv, 'hm:d', ['help', 'my_file='])
    print(opts)
    print(args)
except getopt.GetoptError:
    # Print a message or do something useful
    print('Something went wrong!')
    sys.exit(2)

Above example is very simple, but we can easily extend it to do various things. For example, if help option is passed then print some user friendly message and exit. Here getopt module will automatically parse the option value and map them. Below image shows a sample run.

上面的示例非常简单,但是我们可以轻松地将其扩展为执行各种操作。 例如,如果通过了帮助选项,则打印一些用户友好的消息并退出。 在这里,getopt模块将自动分析选项值并映射它们。 下图显示了运行示例。

To learn more, read python getopt module.

要了解更多信息,请阅读python getopt模块

Python argparse模块 (Python argparse module)

Python argparse module is the preferred way to parse command line arguments. It provides a lot of option such as positional arguments, default value for arguments, help message, specifying data type of argument etc. At the very simplest form, we can use it like below.

Python argparse模块是解析命令行参数的首选方法。 它提供了很多选项,例如位置参数,参数的默认值,帮助消息,指定参数的数据类型等。以最简单的形式,我们可以像下面这样使用它。

import argparse

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

Below is the output from quick run of above script.

以下是上述脚本快速运行的输出。

Python argparse module provide a lot many features, you should read about them at python argparse tutorial for clear understanding.

Python argparse模块提供了许多功能,您应该在python argparse教程中阅读有关它们的详细信息。

That’s all for different options to read and parse command line arguments in python, you should decide which is the one for your specific requirements and then use it.

这就是在python中读取和解析命令行参数的不同选项的全部,您应该确定哪一种是针对您的特定要求的,然后再使用它。

References:

参考文献:

翻译自: https://www.journaldev.com/17743/python-command-line-arguments

python 命令行 参数

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值