Introduction to Python Argparse use method

Source: Internet
Author: User
Tags constant in python


The simplest and most primitive way to resolve command-line parsing in Python is to use SYS.ARGV, and more advanced use of the Argparse module. Argparse was added to the standard library from Python 2.7, so if your Python version is still under 2.7, you need to install it manually.

Basic use

Import Argparse
Parser=argparse. Argumentparser ()
Parser.add_argument ("echo", help= "echo the string")
Args=parser.parse_args ()
Print Args.echo
Parameter introduction

The above example is the simplest example of a usage, and the function is to print your input parameters to the screen. However, for basic usage requirements, these lines of code should be sufficient, and more advanced usage can refer to the official documentation.

The following example code is described below.

1, Import argparse module

2, create the parser object Argumentparser, you can add parameters.

Description: Describe the program

Parser=argparse. Argumentparser (description= "This are a example program")

Add_help: Default is True, you can set false to disable

3, Add_argument () method to specify the command parameters to be accepted by the program

Positioning parameters:

Parser.add_argument ("echo", help= "echo the string")

Optional Parameters:

Parser.add_argument ("--verbosity", help= "increase output verbosity")

In the execution of the program, positional parameters are required, optional parameters optional.

Add_argument () commonly used parameters:

Dest: If you provide dest, such as dest= "a", you can access the parameter through ARGS.A

Default: Set parameter defaults

Action: Motion of the parameter starting

Store: Save parameters, default

Store_const: Saves a value (constant) that is defined as part of the parameter specification, rather than a value that comes from a parameter resolution.

Store_ture/store_false: Save the corresponding Boolean value

Append: Saves the value in a list.

Append_const: Saves a value (constant) defined in the parameter specification in a list.

Count: Number of times the parameter appears

Parser.add_argument ("-V", "--verbosity", action= "Count", default=0, help= "increase output verbosity")

Version: Print program versioning information

Type: Converting the result entered from the command line into a set

Choice: Allowable parameter values

Parser.add_argument ("-V", "--verbosity", Type=int, Choices=[0, 1, 2], help= "Increase output verbosity")

Help: Introduction to Parameter Commands

See an example

#! /usr/bin/env python
Import sys, OS
Sys.path.insert (0, "private")
Import Argparse
Parser = Argparse. Argumentparser ()
Parser.add_argument ("-C", "--config", help= "the full path of config file")
args = Parser.parse_args ()

if __name__ = = ' __main__ ':
If Args.config:
Configpath = Args.config
# # do something following
Exit ()
Else
Print "Unknown parameter, please refer--help for more detail."

If you need option with no parameters

P.add_argument (' f ', '--foo ', action= ' store_true ')

Related Article

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.