基本代码
如果想增加功能,可以在功能区直接声明新的函数,然后改一下大纲和入口。
python3
import re
# 大纲
def print_help():
print("""[help
输入指令以执行。
输入"."以退出。
hello : 示例功能,输出"Hello World"
in [str] [str] : 示例功能,接收两个字符串
""")
# 功能区
def f1():
print("Hello World")
def f2(s1, s2):
print(f"收到字符串{s1}和{s2}")
#入口
while True:
cmd = input("\n_ ")
if cmd == ".":
break
elif cmd == "hello":
f1()
elif s := re.match(r"in (\S+) (\S+)", cmd):
f2(s.group(1), s.group(2))
else:
print_help()
静态化
增加一个设计,可以在执行之前决定一定要执行哪些指令。
python
import re
# 大纲
def print_help():
print("""[help]
输入指令以执行。
输入"."以退出。
hello : 示例功能,输出"Hello World"
in [str] [str] : 示例功能,接收两个字符串
""")
# 功能区
def f1():
print("Hello World")
def f2(s1, s2):
print(f"收到字符串{s1}和{s2}")
#入口
def process(cmd):
if cmd == ".":
return False
elif cmd == "hello":
f1()
elif s := re.match(r"in (\S+) (\S+)", cmd):
f2(s.group(1), s.group(2))
else:
print_help()
return True
def run_script():
while True:
cmd = input("_")
if not process(cmd)
break
# 静态测试
process("hello")
# 启用脚本
run_script()