Python argparse module

Source: Internet
Author: User

The function of the Python argparse module is to parse the command line and check whether the command line conforms to the predefined format.

How to use:

1. Import Argparse Module Imports Argparse

2. Create Argparse Object parser = Argparse. Argumentparser ()

3. Add command line related parameters, option parser.add_argument ("...")

4, analytic Parser.parse_args ()

Example one: (delete the specified Zabbix screen)

#!/usr/bin/env python2.7#coding=utf-8import sysimport argparseimport zabbixauthimport  zabbixScreenif __name__ ==  "__main__":     #if  len (SYS.ARGV)  < 2 :    #    print  "Usage:", sys.argv[0], " Screenname "    #    sys.exit (1)      #if   sys.argv[1] ==  "-H"  or sys.argv[1] ==  "--help":    #     print  "Usage:", sys.argv[0], "Screenname"     #     sys.exit ()      #以下三行的功能等效于以上的if   Statements     parser =  Argparse. Argumentparser ()    #创建argparse对象     parser.add_argument ("Screenname", help= " Specifies the screen name of the is will be deleted ")      parser.parse_args ()     userauth=zabbixauth.zabbix_auth ()     token=userauth.user_login ()      screenobj=zabbixscreen.zabbix_screen ()     screenidlist=screenobj.screen_ Get (token,sys.argv[1])     if screenidList != None:         print  "Screenid:", Screenobj.screen_delete (Token,screenidlist[0]), "delete  Success "    else:        print " the  Screen: ", sys.argv[1]," does not exists "

Example Two: (Create a custom Zabbix screen)

#!/usr/bin/env python2.7#coding=utf-8import sysimport argparseimport zabbixauthimport  zabbixScreenif __name__ ==  "__main__":     #if  len (SYS.ARGV)  < 4 :    #    print  "Usage:", sys.argv[0], " Screenname rows columns "    #    sys.exit (1)       #if  sys.argv[1] ==  "-H"  or sys.argv[1] ==  "--help":     #    print  "Usage:", sys.argv[0], "Screenname rows columns"      #    sys.exit ()      #if  sys.argv[2].isdigit () = = False or sys.argv[3].isdigit () ==false:    #    print  " Note: rows and columns is number "    #     Sys.exit ()    &nbspThe #以上注释的if   statement is equivalent to the following 5 lines of     parser = argparse. Argumentparser ()     parser.add_argument ("Screenname", help= "given a string  To set the screen name ")     parser.add_argument (" Rows ", help=" Given  a number to set the lines ", Type=int)     parser.add_argument ("Columns", help= "Given a number to set the columns", Type=int)      parser.parse_args ()     if int (sys.argv[3]) >3 :         print  "Note: columns must be less than 3"          sys.exit ()     userauth=zabbixauth.zabbix_auth ()     token=userauth.user_login ()     screenobj=zabbixscreen.zabbix_ Screen ()     screenidlist=screenobj.screen_get (token,sys.argv[1])     if screenidList != None:         print  "Screen already exists" else:         print  "Screnn", sys.argv[1], "Create success, screenid is", Screenobj.screen_ Create (Token,sys.argv[2],sys.argv[3],sys.argv[1])


This article is from the "Worknote" blog, make sure to keep this source http://caiyuanji.blog.51cto.com/11462293/1896437

Python argparse module

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.