Python command Module Argparse Learning notes (i)

Source: Internet
Author: User

The first is about setting the-h/--help parameter

Description: Before help information can be used to describe the help
Prog: Describes the name of the program in the Help information
Epilog: After Help information
Usage: Describes the purpose of the program
ADD_HELP: The default is true, set to false, can not display the Help information, execution-h/--help will be error
Conflict_handler: Resolving parameter conflicts
Prefix_chars: Parameter prefix, default to "-"
Fromfile_prefix_chars: Set prefix character, put in file name, read and execute parameters
Argument_default: Global default values for parameters

Description/epilog
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import argparseparser = Argparse. Argumentparser (description= "The help of Python") parser.add_argument ("-T", "--thread", help= "thread Run", action= " Store_true ") args = Parser.parse_args () If Args.thread:    print (args) Else:    print (" Error ")

Execute parameter-H

Run results

Prog/usage
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import argparseparser = Argparse. Argumentparser (description= "The Help in Python", epilog= "End of Help", usage= "python Run Thread") parser.add_argument ("- T ","--thread ", help=" thread Run ", action=" store_true ") args = Parser.parse_args () If Args.thread:    print (args) Else:    print ("Error")

Run results

The default is

If prog and usage are not set, the default, prog, and usage settings are displayed and the usage

Add_help
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import argparseparser = Argparse. Argumentparser (description= "The Help in Python", epilog= "End of Help", Add_help=false) parser.add_argument ("-T", "-- Thread ", help=" thread Run ", action=" store_true ") args = Parser.parse_args () If Args.thread:    print (args) Else:    Print ("Error")

Run results

Conflict_handler

When the parameters are repeated, the program will error, the Conflict_handler set to Resovle can be resolved

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import argparseparser = Argparse. Argumentparser (description= "The Help in Python", epilog= "End of Help") parser.add_argument ("-T", "--thread", help= " Thread Run ", action=" Store_true ") parser.add_argument ("-T ","--thread ", help=" thread run (2) ", action=" Store_true ") args = Parser.parse_args () If Args.thread:    print (args) Else:    print ("Error")

Run, error

To Argparse. Argumentparser () Add conflict_handler= "resolve"

#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import argparseparser = Argparse. Argumentparser (description= "The Help in Python", epilog= "End of Help", conflict_handler= "resolve") parser.add_argument ("-T", "--thread", help= "thread Run", action= "Store_true") parser.add_argument ("-T", "--thread", help= "thread run (2)", action= "Store_true") args = Parser.parse_args () If Args.thread:    print (args) Else:    print ("Error")

Run results

The original-t/--thread parameter is overwritten

Python command Module Argparse Learning notes (i)

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.