如何使用 Python 构建 CLI 客户端模型
我需要提供一个 Python 客户端来表示 Cisco 路由器的 CLI。这种客户端的要求是它允许自动完成 PyCharm 内的命令。
例如,如果开发人员想要调用 CLI 命令:
commit confirm <commit_confirm_time> log <log>
开发人员可以键入
commit.confirm.
并获取自动完成值的列表:
.log(log='log.txt')
commit_confirm_time('aaaa')
因为
cli.commit.confirm.log(log='log.txt')
cli.commit.confirm.commit_confirm_time('aaaa').log('file1.log')
代表
commit confirm log <log>
commit confirm <commit_confirm_time> log <log>
我编写了这段简短的代码来对特定命令系列进行建模,但完整集为 5000 +命令的独特排列,因此我正在寻找一种更好的方法来对命令进行建模,同时保持允许自动完成命令的要求
from typing import List
commands: List[str] = ['commit log <log>',
'commit no-warning',
'commit and-exit log <log>',
'commit check log <log>',
'commit check no-warning',
'commit confirm log <log>',
'commit confirm <commit_confirm_time> log <log>',
]
class CliLog:
def __init__(self):
pass
def log(self, log=''):
pass
class CliAndExit:
def __init__(self):
pass
def log(self, log=''):
pass
class CliCheck(object):
def __init__(self):
pass
def log(self, log=''):
pass
def no_warning(self):
pass
class CliConfirm(object):
def __init__(self):
self.log: CliLog = CliLog()
def log(self, log=''):
pass
def commit_confirm_time(self, commit_confirm_time='') -> CliLog:
return self.log()
class CliCommit:
def __init__(self):
self.and_exit: CliAndExit = CliAndExit()
self.check: CliCheck = CliCheck()
self.confirm: CliConfirm = CliConfirm()
def log(self, log=''):
pass
def no_warning(self):
pass
class Cli:
def __init__(self):
self.commit: CliCommit = CliCommit
cli: Cli = Cli()
cli.commit.log('file1.log')
cli.commit.no_warning()
cli.commit.and_exit.log('file1.log')
cli.commit.check.log('file1.log')
cli.commit.check.no_warning()
cli.commit.confirm.log(log='log.txt')
cli.commit.confirm.commit_confirm_time('aaaa').log('file1.log')
I need to provide a Python client to represent a CLI of a Cisco router. The requirement of such a client is that it allows auto-complete of the commands inside PyCharm.
For example, if a developer wants to call the CLI command:
commit confirm <commit_confirm_time> log <log>
The developer can type
commit.confirm.
and get a list of the auto-complete values:
.log(log='log.txt')
commit_confirm_time('aaaa')
because
cli.commit.confirm.log(log='log.txt')
cli.commit.confirm.commit_confirm_time('aaaa').log('file1.log')
stand for
commit confirm log <log>
commit confirm <commit_confirm_time> log <log>
I wrote this short code to model a specific family of commands, but the full set is 5000+ unique permutations of commands, so I am looking for a better way to model the commands while keeping the requirement of allowing to auto-complete the commands
from typing import List
commands: List[str] = ['commit log <log>',
'commit no-warning',
'commit and-exit log <log>',
'commit check log <log>',
'commit check no-warning',
'commit confirm log <log>',
'commit confirm <commit_confirm_time> log <log>',
]
class CliLog:
def __init__(self):
pass
def log(self, log=''):
pass
class CliAndExit:
def __init__(self):
pass
def log(self, log=''):
pass
class CliCheck(object):
def __init__(self):
pass
def log(self, log=''):
pass
def no_warning(self):
pass
class CliConfirm(object):
def __init__(self):
self.log: CliLog = CliLog()
def log(self, log=''):
pass
def commit_confirm_time(self, commit_confirm_time='') -> CliLog:
return self.log()
class CliCommit:
def __init__(self):
self.and_exit: CliAndExit = CliAndExit()
self.check: CliCheck = CliCheck()
self.confirm: CliConfirm = CliConfirm()
def log(self, log=''):
pass
def no_warning(self):
pass
class Cli:
def __init__(self):
self.commit: CliCommit = CliCommit
cli: Cli = Cli()
cli.commit.log('file1.log')
cli.commit.no_warning()
cli.commit.and_exit.log('file1.log')
cli.commit.check.log('file1.log')
cli.commit.check.no_warning()
cli.commit.confirm.log(log='log.txt')
cli.commit.confirm.commit_confirm_time('aaaa').log('file1.log')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论