python - 如何在命令行中编写选项卡功能?
问题描述
我正在尝试在键入时在 python 中构建此功能,shell> listeners
然后它将更改为shell>listeners>
,但我不知道如何或从哪里开始执行此操作。我试过这个,但我想这样输出shell (hello)(aa)(aaa) >>
sections = ["shell"]
def render_sections():
return ">".join([*sections, ''])
def get_command():
return input(render_sections()).strip()
while 1:
command = get_command()
if command == 'exit':
break
elif command == '':
continue
else:
sections.append(command)
解决方案
所以你想要这样的东西:
def render_sections():
first, rest = sections[:1], sections[1:]
return ' '.join(first + [''.join(['(%s)' % s for s in rest])] + [">>"])
产生:
shell >>hello
shell (hello) >>aa
shell (hello)(aa) >>aaa
shell (hello)(aa)(aaa) >>
推荐阅读
- angularjs - 如何从复选框更新其他复选框
- ios - 带有内容框架更新关闭的 SwiftUI ScrollView
- ios - Firebase 匿名登录 - 为什么 FirebaseID 在登录到 Facebook 时会发生变化,但在未登录到 Facebook 时保持不变?
- python - 如何在 spark textFile 函数中使用自定义换行符?
- azure - 将数据从 DB2 加载到 Azure SQL DB
- bash - 试图了解 Bash 文件设置/结构
- html - Angular 服务器端渲染 - 向 angular.json 添加什么?
- oracle - 尝试从 selenium 连接到 oracle DB 时出错
- swift - 从函数 Swift 获取通用类型 UIView
- c# - PDFLib set_graphics_option如何在fillcolor选项中传递颜色列表