Python's ease of use Cmd learning module

Yesterday Gangster Bing to send a code that gave me the feeling of watching a lot of fun, which is to write a command line right, I finished articles to find more little detail blog to learn a bit

 cmd main methods and properties

method:

  • (1) cmdloop (): similar to the Tkinter mainloop, Cmd run the parser;
  • (2) onecmd (str): read the input and processed, typically need to override this function, but to perform a particular command more specific do_command;
  • (3) emptyline (): invoke the blank line when the input method;
  • (4) default (line): the method is called when unrecognized command input;
  • (5) completedefault (text, line, begidx, endidx): If complete _ * () method for does not exist, it will call the function, which is mainly used to supplement tab, and can only be used under linux.
  • (6) precmd (line): the command line is invoked prior to parsing method;
  • (7) postcmd (stop, line): the command line is invoked after parsing method;
  • This method is called before () run cmdloop;: (8) preloop ()
  • (9) postloop (): cmdloop () This method is called after the withdrawal;
  • (10) help_command (): Description of the command command, wherein the command is a character variable

Attributes:

  • (1) prompt: interactive prompt character, which is just (Cmd) can be replaced with the character you want us
  • (2) intro: string output before entering interactive shell, it can be identified as the language flag or the like. In fact, this method can also output the same in preloop

Code:

On the basis of the code written on Bing added a bit of code based on the above reference thing I read blog, make themselves more clear understanding

Do_ attention to the role and function of the beginning of the help_ (beginning do_ * for the command, the name of the instruction execution to the beginning of the help_ * To help, the help of the corresponding command description)

from cmd Import Cmd 


class BingCmd (Cmd):
     "" " the Just the try the try " "" 

    prompt = " BingCmd> " 
    Intro = " is available for purchase BingCMD to " 

    # in cmd.Cmd derived class, beginning with do_ * for the command to help_ * to help beginning 
    DEF print_log (Self, argv):
         Print ( " La La La " ) 

    DEF do_bing (Self, argv): 
        self.print_log (argv) 

    DEF help_bing (Self):
         Print ( " the Just Print La La La ")

    defdo_hu (Self, argv):
         Print ( " call feature hu " ) 

    DEF help_hu (Self):
         Print ( " output hu " ) 

    DEF do_exit (Self, argv):
         # treatment method returns True, then exit the loop. 
        Print ( " Exit () " )
         return True 

    DEF help_exit (Self):
         Print ( " to quit " ) 

    DEF preloop (Self):
         Print ( " This method is called before cmdloop () Run " ) 

    DEFpostloop (Self):
         Print ( " cmdloop () call after exiting the method " ) 


IF  __name__ == " __main__ " : 
    BingCmd () cmdloop ().

Run with screenshots:

 

 

 

Guess you like

Origin www.cnblogs.com/dong973711/p/11989819.html