no matter how tactical change, don't forget the strategy.
In the earlier days, White has used getopt to get command-line tools.
However, in order to complete a seemingly simple, actually have N more than the logic of the case is a bit annoying.
Enthusiastic walker, again made a noise: "Use cmd!" ”
The cmd module is a module that supports the command line interface specifically. Let's refactor it:
# -*- coding: utf-8 -*-import sysimport cmdclass pycdc (cmd. CMD): def __init__ (self): # Initialize the base class, the quantification of the class should be declared at initialization cmd.cmd.__init__ ( Self) # define command line prompt self.prompt = ">" # Define the Operation def do_walk (Self, filename) performed by the Walk command: if filename == "": filename = input ("Please enter CDC file name:") print ("Scan disc contents saved to: '%s '" % filename) # defines the help output of the Walk Command def help_walk (self): print ("Scan disc contents walk cd and export init '. CDC ') # define the actions performed by the dir command def do_dir (Self, pathname): if pathname == "": pathname = input ("Please enter the specified save/Search directory:") # define DIR (command's help output Def help_dir (self): print ("Specify Save/Search directory") # define the actions performed by the Find command def&Nbsp;do_find (Self, keyword): if keyword == "": keyword = Input ("Please enter search keywords:") # define the help output of the Find command def help_find (self): print ("Search keywords" # defines the action that the Quit command performs def do_quit (self, arg): sys.exit (1) # define the Help output def help_quit (self) for the Quit command: print ("Syntax:quit") print ("--terminates the application") # Defines the shortcut for quit do_q = do_quit if __name__ == ' __main__ ':     CDC = PYCDC () Cdc.cmdloop ()
The results are as follows:
>helpdocumented commands (Type help <topic>): ========================================dir find help quit Walku ndocumented Commands:======================q>dir Please enter the specified save/Search directory: Cdc>walk Enter the CDC file name: TEST.CDC The contents of the scanned disc are saved to: ' TEST.CDC ' >?find search keywords >xx*** Unknown syntax:xx>q>>> ==========================> Find Please enter search keywords: images>quit
As can be seen, this code is purely to try the CMD module function, only print output information, without any practical effect.
As can be seen from this example, first the PYCDC class inherits the Cmd.cmd class, then Walk,dir,find and quit are defined in the class, and the command Q is the short command form of quit. (That is, if you need to define another command, such as command, just add a do_command function to the PYCDC Class) and the Help information for that command is given by the Help_command function.
As the example writes, the custom PYCDC class provides commands that can be used normally, and the xx command is undefined, so the command-line prompt is an unknown syntax. The last Q command and quit are the same function, that is, exiting the program.
#-*-Coding:utf-8-*-import osimport sysfrom cdctools Import * # can introduce all functions in script Cdctools def cdwalker (CDROM, Cdcfil E): Export = "" For root, dirs, files in Os.walk (CDROM): Export + = "\ n%s;%s;%s"% (root, dirs, files) ope N (cdcfile, ' W '). Write (export) if __name__ = = "__main__": CDC = PYCDC () cdc.cmdloop ()
Haha, can run up! Can see in the code, according to the code of the reuse scale to divide, from small to large should be: code line → function → class → module
There seems to be a bigger package, which is still not available, so let it go.
"Cute Python" reading notes (v)