Python command-line parsing tool argparse module "4"

Source: Internet
Author: User

In the previous section we explained the Add_argument () method, which we will learn about the Parse_args () method. The function of the Parse_args () method is to parse the command-line arguments and return the parsed namespace. By default, the parameters are obtained from the SYS.ARGV.      1. Parameter value SyntaxParse_args () supports a variety of syntax to parse parameters, the simplest way to do so is to pass parameters and values separately
>>> parser = Argparse. Argumentparser (prog='PROG')>>> Parser.add_argument ('- x')>>> Parser.add_argument ('--foo')>>> Parser.parse_args ('- X ×'. Split ()) Namespace (foo=none, x='X')>>> Parser.parse_args ('--foo Foo'. Split ()) Namespace (foo='FOO', X=none)
    For comparing long parameters with values, you can use = to pass
>>> Parser.parse_args ('--foo=foo'. Split ()) Namespace (foo='  FOO', X=none)
For simple parameters, parameters and values can be linked together
>>> Parser.parse_args ('-xx'. Split ()) Namespace (foo=none, x='  X')
    Several simple parameters can also be linked together, using only one-as long as the last parameter only needs a value of the case is established.
>>> parser = Argparse. Argumentparser (prog='PROG')>>> Parser.add_argument ('- x', action='store_true')>>> Parser.add_argument ('- y', action='store_true')>>> Parser.add_argument ('- Z')>>> Parser.parse_args ('-xyzz'. Split ()) Namespace (x=true, Y=true, z='Z')
   2. Non-legal parameters
Error when passing an illegal parameter, and exit return usage example
>>> parser = Argparse. Argumentparser (prog='PROG')>>> Parser.add_argument ('--foo', type=int)>>> Parser.add_argument ('Bar', nargs='?') >>>#Invalid type>>> Parser.parse_args (['--foo','spam']) Usage:prog [-h] [--Foo Foo] [Bar] PROG:error:argument--foo:invalid int Value:'spam'>>>#Invalid option>>> Parser.parse_args (['--bar']) Usage:prog [-h] [--Foo Foo] [Bar] PROG:error:no such option:--Bar>>>#wrong number of arguments>>> Parser.parse_args (['spam','Badger']) Usage:prog [-h] [--Foo Foo] [Bar] PROG:error:extra arguments Found:badger
3. Included-parametersIf the parameters contain--words, it is easy to cause ambiguity.
>>> parser = Argparse. Argumentparser (prog='PROG')>>> Parser.add_argument ('- x')>>> Parser.add_argument ('Foo', nargs='?') >>>#no negative number options, so-1 is a positional argument>>> Parser.parse_args (['- x','-1']) Namespace (foo=none, x='-1') >>>#no negative number options, So-1 and-5 is positional arguments>>> Parser.parse_args (['- x','-1','-5']) Namespace (foo='-5', x='-1') >>> parser = Argparse. Argumentparser (prog='PROG')>>> Parser.add_argument ('-1', dest=' One')>>> Parser.add_argument ('Foo', nargs='?') >>>#negative number options present, so-1 is an option>>> Parser.parse_args (['-1','X']) Namespace (foo=none, one='X') >>>#negative number options present, so-2 is an option>>> Parser.parse_args (['-2']) Usage:prog [-h] [-1One ] [Foo] PROG:error:no such option:-2 >>>#negative number options present, so both-1s is options>>> Parser.parse_args (['-1','-1']) Usage:prog [-h] [-1One ] [Foo] PROG:error:argument-1:expected one argument
    If you use a positional parameter that must precede the-prefix, and it does not look like a negative number, you can insert it at this point--to tell the argument behind the parser to be the positional parameter.
>>> Parser.parse_args (['---'-f']) Namespace ( Foo='-f', One=none)
 4. Parameter name abbreviation
The Parse_args () method allows long parameters to be abbreviated as long as there is no ambiguity.
>>> parser = Argparse. Argumentparser (prog='PROG')>>> Parser.add_argument ('-bacon')>>> Parser.add_argument ('-badger')>>> Parser.parse_args ('-bac MMM'. Split ()) Namespace (Bacon='MMM', badger=None)>>> Parser.parse_args ('-bad WOOD'. Split ()) Namespace (Bacon=none, badger='WOOD')>>> Parser.parse_args ('-ba BA'. Split ()) Usage:prog [-h] [-bacon bacon] [-Badger Badger] PROG:error:ambiguous option:-ba could Match-badger,-bacon
     5. Do not use SYS.ARGVIn addition to using SYS.ARGV to pass parameters, you can use a string list to pass parameters.
>>> parser =Argparse. Argumentparser ()>>>parser.add_argument ( ...'integers', metavar='int', Type=int, Choices=xrange (10),... Nargs='+', help='An integer in the range 0..9')>>>parser.add_argument ( ...'--sum', dest='Accumulate', action='Store_const', const=sum,... default=max, help='sum the integers (default:find the max)')>>> Parser.parse_args (['1','2','3','4']) Namespace (accumulate=<built-inchfunction Max>, integers=[1, 2, 3, 4])>>> Parser.parse_args ('1 2 3 4--sum'. Split ()) Namespace (Accumulate=<built-inchfunction Sum>, integers=[1, 2, 3, 4])
 6. Customizing the namespace
Sometimes you do not need to create a new namespace, you can pass a namespace directly.
>>>classC (object): ...Pass...>>> C =C ()>>> parser =Argparse. Argumentparser ()>>> Parser.add_argument ('--foo')>>> Parser.parse_args (args=['--foo','BAR'], namespace=c)>>>C.foo'BAR'




Python command-line parsing tool argparse module "4"

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.