Brief introduction:
This module provides command-line option resolution and currently supports short format and long format options
Quick installation:
Description: Built-in modules do not need to be installed
Parsing method:
Getopt (args, shortopts, longopts = []), (opts, args)
Description: Args is a sequence of arguments to parse, often defining a string for sys.argv[1:],shortopts as a single-character option, if an option requires a parameter, the response letter must be preceded by a colon, longopts is a long-form sequence of option names, can contain multiple characters, The sequence element must contain a--prefix, and if this long option requires a parameter its name should contain the suffix =, and the short and long formats can be used in combination with the call, eventually returning an option tuple sequence opts and a non-option tuple sequence args
Application Scenarios:
1. Standardized program directory structure and code style recommended our program configuration from the external loading, do not write dead, think about the Operation Dimension Nginx/mysql and other applications are using getopt parsing command line parameters?
#!/usr/bin/env python# -*- coding: utf-8 -*-"" "## authors: limanman# oschina: http://xmdevops.blog.51cto.com/# purpose:# "" "# Description: Import public module Import osimport sysimport getoptimport ConfigParser# Instructions: Import other modules program = __file__version = ' 1.0.0.1 ' # description: display instructions for use Def show_usage_info (): message = '%s version: %s/%susage: xmzoomeye-agent [-hv] [-c filename] [-l filename]options: -h : this help -v : show version and exit -c : set running configuration file (Default: docs/default.ini) -l : set logging configuration file (Default: docs/logging.ini) " % (program, program, version) print message# Description: Detection parameters specify Def parameters_test (): try: opts, args = getopt.getopt (sys.argv[1 :], ' hvc:l: ', []) except getopt. getopterror, e: print '%s: %s ' % ( program, e) sys.exit () exit_flag = [0, 0, 0] for key, val in opts: if '-V ' == key: print ' version: %s/%s ' % (program, version) exit_flag[0] = 1 if '-H ' == key: show_usage_info () exit_flag[0] = 1 if '-C ' == key: exit_flag[1] = 1 if '-l ' == key: exit_flag[2] = 1 if exit_flag[0]: sys.exit () if not all (exit_flag[1:]): print '%s: option -c/-l requires arguments ' % (program,) sys.exit () &Nbsp; return dict (opts) # description: detection configuration file Def configuration_test (opts): cf = {} cp = configparser.configparser () for opt, arg in opts.iteritems (): if not os.path.exists (ARG): print '%s: no such config file %s ' % (program, arg) sys.exit () Runconfig = os.path.abspath (opts['-C ') with open (runconfig, ' r+b ')  AS HANDLER:        CP.READFP (handler, runconfig) sections = cp.sections () if ' Redis ' not in sections or ' Agent ' not in sections: print '%s: no redis and agent section ' % (program,) sys.exit () for section in sections: if section not in cf: cf.setdefault (section, {}) cf[section].update (Cp.items (section)) return cfif __name__ == ' __main__ ': pass
Note: As the case of the above simple use of getopt parsing command line parameters, the use of Configparser parsing INI configuration file eventually returned to the dictionary, convenient for the main program to load, and did not write in the main program to die, so more convenient program unit test code writing and review.
This article is from the "ζ Automated operation and maintenance development Road ζ" blog, please be sure to keep this source http://xmdevops.blog.51cto.com/11144840/1857229
Basic Primer _python-modules and packages. What are the best practices for building built-in modules getopt in operational development?