import cmd
class MyCmd(cmd.Cmd):
prompt = '(mycmd) ' # 设置命令行提示符
def do_greet(self, line):
"""greet [name] - greet the person with the given name"""
if line:
print(f"Hello, {line}!")
else:
print("Hello!")
def do_exit(self, line):
"""exit - exit the application."""
print("Exiting...")
return True # 返回 True 表示退出命令循环
if __name__ == '__main__':
MyCmd().cmdloop('Starting mycmd...')
cmd 模块:cmd 是 Python 的标准库模块,用于创建简单的命令行解释器。MyCmd 类:继承自 cmd.Cmd,这是命令行解释器的基类。prompt 属性定义了命令行提示符的样子。do_greet(self, line):处理 greet 命令,根据输入的名字进行问候。do_exit(self, line):处理 exit 命令,用于退出程序。cmdloop() 方法启动命令行解释器,进入命令处理循环。上一篇:python any函数
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站