Python CLI 中的命令前缀使用 Python 标准库中的 cmd
Command prefixes in Python CLI using cmd in Python's standard library
我正在尝试为 IRC 客户端创建一个 CLI,我只使用标准 python 包编写它。我一直在使用 python 中的本机 cmd 库,它(目前)非常适合我的需要,但有一个问题我无法解决。
在 cmd 库中有一个 public 实例变量用于包含命令前缀,但我终究无法让它正常工作;它只是给我一个 "Unknown syntax" 错误。此处的目标是“/help”或其他带有 / 前缀的命令将调用该方法,只需键入“help”即可将 "help" 发送到服务器。
这是我的代码中正在进行的 CLI class:
class lircCLI(Cmd):
intro = 'Welcome to LIRC \nType help or ? for command list'
prompt = 'lirc> '
identchars = '/' << the problem
#---------commands-----------
def do_sync(self):
'Force synchronize with the IRC server'
connectionHandler.syncMessages()
def do_whoami(self, arg):
'Good question'
print(user.getName())
def do_changename(self, arg):
'Change your name'
user.setName(arg)
print("Username has been changed to '"+user.name+"'")
def default(self, line):
'Sends a message'
#compiles message request
message = str(user.getName().replace(" ", "_") + " SHOUTS " + line + " END")
connectionHandler.sendMessage(message)
logHandler.updateChatLog({time.strftime("%d/%m/%Y") : {'time': time.strftime("%I:%M:%S"), 'user': user.getName(),'text': line}})
identchars
属性 实际上定义了可以从中提取命令的字符集。它的默认值几乎是 "ascii chars".
您要做的是使用 .precmd
方法来反转正常处理。寻找前导斜杠,如果找到则将其删除。如果未找到,请在前面添加一些无效前缀或一些 "default" 命令名称。这些都可以工作:
def precmd(self, line):
if line[0] == '/':
line = line[1:]
else:
line = '>' + line
return line
def default(self, line):
line = line[1:]
print("You said, '"+line+"'")
或者:
def precmd(self, line):
if line[0] == '/':
line = line[1:]
else:
line = 'say ' + line
return line
def do_say(self, arg):
print("You said, '"+arg+"'")
def default(self, line):
raise ValueError("Inconceivable!")
我正在尝试为 IRC 客户端创建一个 CLI,我只使用标准 python 包编写它。我一直在使用 python 中的本机 cmd 库,它(目前)非常适合我的需要,但有一个问题我无法解决。
在 cmd 库中有一个 public 实例变量用于包含命令前缀,但我终究无法让它正常工作;它只是给我一个 "Unknown syntax" 错误。此处的目标是“/help”或其他带有 / 前缀的命令将调用该方法,只需键入“help”即可将 "help" 发送到服务器。
这是我的代码中正在进行的 CLI class:
class lircCLI(Cmd):
intro = 'Welcome to LIRC \nType help or ? for command list'
prompt = 'lirc> '
identchars = '/' << the problem
#---------commands-----------
def do_sync(self):
'Force synchronize with the IRC server'
connectionHandler.syncMessages()
def do_whoami(self, arg):
'Good question'
print(user.getName())
def do_changename(self, arg):
'Change your name'
user.setName(arg)
print("Username has been changed to '"+user.name+"'")
def default(self, line):
'Sends a message'
#compiles message request
message = str(user.getName().replace(" ", "_") + " SHOUTS " + line + " END")
connectionHandler.sendMessage(message)
logHandler.updateChatLog({time.strftime("%d/%m/%Y") : {'time': time.strftime("%I:%M:%S"), 'user': user.getName(),'text': line}})
identchars
属性 实际上定义了可以从中提取命令的字符集。它的默认值几乎是 "ascii chars".
您要做的是使用 .precmd
方法来反转正常处理。寻找前导斜杠,如果找到则将其删除。如果未找到,请在前面添加一些无效前缀或一些 "default" 命令名称。这些都可以工作:
def precmd(self, line):
if line[0] == '/':
line = line[1:]
else:
line = '>' + line
return line
def default(self, line):
line = line[1:]
print("You said, '"+line+"'")
或者:
def precmd(self, line):
if line[0] == '/':
line = line[1:]
else:
line = 'say ' + line
return line
def do_say(self, arg):
print("You said, '"+arg+"'")
def default(self, line):
raise ValueError("Inconceivable!")