首页 > 编程 > Python > 正文

Python cmd模块

2019-11-11 01:36:27
字体:
来源:转载
供稿:网友

cmd模块为命令行接口(command-line interfaces, CLI)提供了一个简单的框架,可以在自己的程序中使用它来创建自己的命令行程序。

# -*- coding: utf-8 -*-import cmdimport sysclass CLI(cmd.Cmd): def __init__(self): cmd.Cmd.__init__(self) self.PRompt = '> ' # 定义命令行操作符 def do_hello(self, arg): # 定义hello命令所执行的操作 print "hello again", arg, "!" def help_hello(self): # 定义hello命令的帮助输出 print "syntax: hello [message]", print "-- prints a hello message" def do_quit(self, arg): # 定义quit命令所执行的操作 sys.exit(1) def help_quit(self): # 定义quit命令的帮助输出 print "syntax: quit", print "-- terminates the application" # 定义quit的快捷方式 do_q = do_quit# 创建CLI实例并运行cli = CLI()cli.cmdloop()

终端执行:

> help hellosyntax: hello [message] -- prints a hello message> hello pythonhello again python !> haha*** Unknown syntax: haha> q
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表