Python3-argparse Module-parsing command-line arguments

Source: Internet
Author: User

Official documents

Http://python.usyiyi.cn/translate/python_352/library/argparse.html

code example

ImportArgparse#1. Get parameter Resolution objectsParser = Argparse. Argumentparser (description="Help information before the description of some information, can not write Oh")#2. Add resolvable parameters#add_argument () function Common parameters#name or List of flags option string, such as Foo or-F,--foo#Nargs should read the number of command line arguments, the allowed values are#N (n is a number) means that there must be n arguments#The ' * ' represents any parameter that can be#'? ' delegate can have 0 or 1 parameters#' + ' represents at least one parameter#if Nargs is specified and the value is multiple, then it returns a list#Default parameter Defaults#type command line arguments should be converted to (by default read into a string)#The choices parameter allows a container of values that are valid parameters#whether the required parameter is required#Short description of the help parameterParser.add_argument (" -A","--app", Default=0, Type=int, Choices=range (1, ten), help="app ' s code") parser.add_argument ("First_arg", nargs=3)#Position ParametersParser.add_argument ("Second_arg")#Position ParametersParser.add_argument ("- S","--student", nargs=2, help="Sub Need", required=True) parser.add_argument ("- y", action='store_true')#options can be no arguments, have-y is true, none is false#3. Parsing Parameters#Parse_args () returns a very simple namespace object that is designed#when Parse_args () is called, the optional arguments are identified with a-prefix, and the remaining parameters are assumed to be positional parameters#Note: Positional parameters are requirednamespace =Parser.parse_args ()#4. Get the parameter values from the parsed namespace object#Note:#if Add_argument () is given a method similar to-A,--app this parameter#then you can use-A to pass the pass, but to resolve to the Namespace object is only an app attribute, that is, the use of NAMESPACE.A is not recognizedPrint(namespace)Print(Namespace.app)Print(NAMESPACE.Y)

Python3-argparse Module-parsing command-line arguments

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.