Long time did not update the blog
Recently for a new job have time to organize a
In the command line CMD file in the implementation process and how to pass parameters to identify
. 1 # - * - Coding: UTF-. 8 - * - 2 # the CMD Run Class 3 # OF: Chen Lei 4 # Time: 2019-10-22 . 5 . 6 . 7 Import SYS . 8 Import the getopt . 9 10 . 11 from WorkUtils.UtilsLog Import UtilsLog 12 is 13 is 14 class UtilsCmd: 15 DEF the __init__ (Self): 16 self.log = UtilsLog () . 17 self.log.info ( " call run class CMD " ) 18 self.log.info (Self. __class__ ) 19 20 DEF Usage (Self): 21 self.log.debug ( " help: " ) 22 self.log.debug ( " * -h:. Print the this " ) 23 Self .log.debug ( " * -C [Val]: with the described " ) 24 self.log.debug ( " * -v [Val]: The version number " ) 25 26 is DEF case_cmd (Self): 27 self.log.debug ( " run mode execution parameter with Example ... " ) 28 description = "" 29 version = "" 30 try: 31 opts, args = getopt.getopt(sys.argv[1:], "hd:v:") 32 for op, value in opts: 33 if op == "-d": 34 description = value 35 elif op == "-v": 36 version = value 37 elif op == "-h": 38 # 帮助信息 39 self.usage() 40 sys.exit() 41 else: 42 sys.exit() 43 except getopt.GetoptError as e: 44 self.log.error("出现ERROR:") 45 self.log.error(e) 46 return { 47 "description": description, 48 "version": version 49 }
Here wrote a class
The method is used to output usage information to help
Case_cmd test method is used to identify process parameters passed with -d -v command line execution
opts, args = getopt.getopt(sys.argv[1:], "hd:v:")
This line is an execution file acquiring parameters may identify h d: v: three kinds of parameter format suffix
I can give you a look effect
No input parameters:
Input -h:
Enter the -v and -d: