Python:argparse command-line parsing module detailed

Source: Internet
Author: User

Argparse command-line parsing module learning
import argparseparser = argparse.ArgumentParser() #类似于初始化吧parser.parse_args()  #解析参数//默认存在-h,--help参数

The output is as follows

python test.py --helpusage: prog.py [-h]optional arguments:-h, --help  show this help message and exit

Next

import argparseparser = argparse.ArgumentParser()parser.add_argument("echo") #定义了一个名为echo的参数,默认必选args = parser.parse_args()print(args.echo)//在这里echo是positional arguments,中文翻译是定位参数,也就是不需要使用-或者--就可用

Output example

python test.py flagflag

--
Next look at the following code

import argparseparser = argparse.ArgumentParser()parser.add_argument("--verbosity", help="increase output verbosity") #help参数,是你输入-h或者--help时会显示的这个--verbosity的帮助信息args = parser.parse_args()if args.verbosity:  #这里就是判断参数是否存在,如果存在就执行下面语句,就可以用来实现一些功能    print("verbosity turned on")

Then look at the output

python test.py --verbosity testverbosity turned on//这里必须指定一个输入,这里我的输入是test,我还打印了下args.verbosity 输出是test

Look, it's changed a little bit.

import argparseparser = argparse.ArgumentParser()parser.add_argument("--verbosity", help="increase output verbosity",action="store_true") #增加了一个action属性args = parser.parse_args()if args.verbose:    print("verbosity turned on")//这个直接python --verbosity 就可以运行if后面的语句,而不用指定参数

-V Short parameter

import argparseparser = argparse.ArgumentParser()parser.add_argument("-v", "--verbose", help="increase output verbosity",action="store_true")args = parser.parse_args()if args.verbose:    print("verbosity turned on")

You can also specify the type, the default parameter type is str

import argparseparser = argparse.ArgumentParser()parser.add_argument("square", help="display a square of a given number",type=int)args = parser.parse_args()print(args.square**2) //这就要求输入必须参数必须是int,否则会报错

Also a property choices=[], define the optional, and then use the IF statement to determine, such as the following

import argparseparser = argparse.ArgumentParser()parser.add_argument("square", type=int,                    help="display a square of a given number")parser.add_argument("-v", "--verbosity", type=int, choices=[0, 1, 2],                    help="increase output verbosity")args = parser.parse_args()answer = args.square**2if args.verbosity == 2:    print ("the square of {} equals {}".format(args.square, answer))elif args.verbosity == 1:    print ("{}^2 == {}".format(args.square, answer))else:    print (answer)

Property default, you can specify that when a parameter is not given, the parameter
***
Help information for the whole program

parser = argparse.ArgumentParser(description="calculate X to the power of Y")

Also a mutex group

group = parser.add_mutually_exclusive_group()

The parameters added here cannot occur at the same time, at most one, or none

Python:argparse command-line parsing module detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.