python getopt argparse_python学习之argparse模块-Go语言中文社区

Introducing Positional arguments

An example:

import argparse

parser = argparse.ArgumentParser()

parser.add_argument("echo")

args = parser.parse_args()

print args.echo

And running the code:

$ python prog.py

usage: prog.py [-h] echo

prog.py: error: the following arguments are required: echo

$ python prog.py --help

usage: prog.py [-h] echo

positional arguments:

echo

optional arguments:

-h, --help show this help message and exit

$ python prog.py foo

foo

Here is what’s happening:

We’ve added the add_argument() method,

which is what we use to specify which command-line options the program is willing to accept. In this case, I’ve named it echo so

that it’s in line with its function.

Calling our program now requires us to specify an option.

The parse_args() method actually returns

some data from the options specified, in this case, echo.

The variable is some form of ‘magic’ that argparse performs

for free (i.e. no need to specify which variable that value is stored in). You will also notice that its name matches the string argument given to the method, echo.

Note however that, although the help display looks nice and all, it currently is not as helpful as it can be. For example we see that we got echo as

a positional argument, but we don’t know what it does, other than by guessing or by reading the source code. So, let’s make it a bit more useful:

import argparse

parser = argparse.ArgumentParser()

parser.add_argument("echo", help="echo the string you use here")

args = parser.parse_args()

print args.echo

And we get:

$ python prog.py -h

usage: prog.py [-h] echo

positional arguments:

echo echo the string you use here

optional arguments:

-h, --help show this help message and exit

Now, how about doing something even more useful:

import argparse

parser = argparse.ArgumentParser()

parser.add_argument("square", help="display a square of a given number")

args = parser.parse_args()

print args.square**2

Following is a result of running the code:

$ python prog.py 4

Traceback (most recent call last):

File "prog.py", line 5, in

print args.square**2

TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

That didn’t go so well. That’s because argparse treats

the options we give it as strings, unless we tell it otherwise. So, let’s tell argparse to

treat that input as an integer:

import argparse

parser = argparse.ArgumentParser()

parser.add_argument("square", help="display a square of a given number",

type=int)

args = parser.parse_args()

print args.square**2

Following is a result of running the code:

$ python prog.py 4

16

$ python prog.py four

usage: prog.py [-h] square

prog.py: error: argument square: invalid int value: 'four'

That went well. The program now even helpfully quits on bad illegal input before proceeding.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值