Python's argparse module processing parameters

Source: Internet
Author: User

Import argparseusage = "" "%prog-q phylogroup-o Out_file, you shall provide the phyloname of query" "" parser = Argparse. Argumentparser (description = USAGE) parser.add_argument ('-O ', '--output ', default = ' Test_result.txt ') parser.add_ Argument ("-Q", "--query", default = "CD12") args = Parser.parse_args () print Args.outputprint args.query

In this example, two option parameters--------------------and-Q are accepted, and the default


If you want to add an option that does not accept parameter passing.

Then use the following methods

Parser.add_argument ('-a ', action= "Store_true", Default=false) parser.add_argument ('-t ', '--tcp ', help= ' TCP service ', action= ' Store_true ')


If you want to change the data type of a parameter, the default is a string, or it can be set to int or float

Parser.add_argument ('-P ',--Port ', Type=int, help= "use a number as port") parser.add_argument ("Square", help= "display a Square of a given number ", Type=int)

In fact, the simplest notation is

Parser.add_argument (' Bar ') parser.add_argument ("echo") args = Parser.parse_args () # # accesses the parameters passed to bar and Echo, using the following method: Print Args.barprint Args.echo


For example, the following is saved to the 12.py file:

#!/usr/bin/env python#-*-coding:utf-8-*-import argparseusage = ""%prog-q phylogroup-o Out_file, you shall provide The Phyloname of query "" "parser = Argparse. Argumentparser (description = USAGE) parser.add_argument ("Bar") parser.add_argument ("echo") args = Parser.parse_args () Print Args.echoprint args.bar## The string that corresponds to the output parameter is in sequence.

The results of the implementation are as follows:

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/83/49/wKiom1dvbLKAeoqIAAB1BoRUNF8119.jpg-wh_500x0-wm_3 -wmp_4-s_3726590244.jpg "title=" poo. JPG "alt=" Wkiom1dvblkaeoqiaab1borunf8119.jpg-wh_50 "/>

Python's argparse module processing parameters

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.