Python argparse Module parsing command-line options simple to use

Source: Internet
Author: User

Parsing command-line options for Argparse modules simple to use

util.py

#!/usr/bin/env python#Coding=utf-8ImportArgparseparser= Argparse. Argumentparser (prog="MyProgram", description="a test of Argparse") parser.add_argument (" -A", type=int,dest="Start", choices={1,2,3},help="Start number") parser.add_argument ("- b", type=int,dest="End", help="End Point") parser.add_argument ("- S","--sum", dest="Data", required=true,type=int,nargs="*", help="numbers to add") parser.add_argument ("- o", required=true,dest="FW", help="Output File")defCal_sum (A, B): s=0 forIinchRange (A, b): s+=Ireturnsif __name__=="__main__": Args=Parser.parse_args ()PrintargsPrintVARs (args) data=Args.dataPrintsum (data) Res=str (SUM (data)) with open (ARGS.FW,'WB') as Fw:fw.write (res)

Look at the effect:

>:~/$ python util.py-s 1 2 3-o output.txtnamespace (data=[1, 2, 3], End=none, fw='output.txt', start=None) {'Start': None,'FW':'output.txt','End': None,'Data': [1, 2, 3]}6[email protected]-ipin:~/workplace/mygithub/leetcodex$ more Output.txt6

Python argparse Module parsing command-line options simple to use

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.