检查通过python cmd模块传递的参数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了检查通过python cmd模块传递的参数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3235字,纯文字阅读大概需要5分钟。
内容图文
这是一个两部分的问题,请参见下面的内容:
>我需要创建某种控制台以供测试人员手动运行某些命令! cmd模块是一个好方法吗?
下面是我到目前为止使用cmd模块的代码,我只是想学习,到目前为止有两个问题:
>为什么自动补全功能不起作用?如果我将< TAB>我什么也没得到,光标向前移动.默认情况下不提供自动完成功能吗?
>是否必须为每种方法处理错误数量的参数?我希望方法-“帮助”文本在出现错误的自变量数量时调用方法,或者在应以自变量而不是自变量调用时自动显示.
.
class InteractiveConsole(cmd.Cmd):
""" Interactive command line """
def __init__(self):
cmd.Cmd.__init__(self)
self.prompt = "=>> "
self.intro = "Welcome to IRT console!"
def do_hist(self, args):
"""Print a list of commands that have been entered"""
print self._hist
def do_exit(self, args):
"""Exits from the console"""
return -1
def do_help(self, args):
"""Get help on commands
'help' or '?' with no arguments prints a list of commands for which help is available
'help <command>' or '? <command>' gives help on <command>
"""
# # The only reason to define this method is for the help text in the doc string
cmd.Cmd.do_help(self, args)
# # Override methods in Cmd object ##
def preloop(self):
"""Initialization before prompting user for commands.
Despite the claims in the Cmd documentaion, Cmd.preloop() is not a stub.
"""
cmd.Cmd.preloop(self) # # sets up command completion
self._hist = [] # # No history yet
self._locals = {} # # Initialize execution namespace for user
self._globals = {}
def postloop(self):
"""Take care of any unfinished business.
Despite the claims in the Cmd documentaion, Cmd.postloop() is not a stub.
"""
cmd.Cmd.postloop(self) # # Clean up command completion
print "Exiting..."
def precmd(self, line):
""" This method is called after the line has been input but before
it has been interpreted. If you want to modify the input line
before execution (for example, variable substitution) do it here.
"""
if line != '':
self._hist += [ line.strip() ]
return line
def postcmd(self, stop, line):
"""If you want to stop the console, return something that evaluates to true.
If you want to do some post command processing, do it here.
"""
return stop
def default(self, line):
"""Called on an input line when the command prefix is not recognized.
In that case we execute the line as Python code.
"""
try:
exec(line) in self._locals, self._globals
except Exception, e:
print e.__class__, ":", e
def emptyline(self):
"""Do nothing on empty input line"""
pass
def do_install(self, pathToBuild):
"""install [pathToBuild]
install using the specified file"""
if pathToBuild:
print "installing %s" % pathToBuild
else:
print "<ERROR> You must specify the absolute path to a file which should be used!"
def do_configure(self, pathToConfiguration):
"""configure [pathToConfiguration]
configure using the specified file"""
if pathToConfiguration:
print "configuring %s" % pathToConfiguration
else:
print "<ERROR> You must specify the absolute path to a file which should be used!"
解决方法:
The optional argument completekey is the
readline
name of a completion key; it defaults toTab
. If completekey is notNone
andreadline
is available, command completion is done automatically.
您需要具有readline才能使Tab完成工作.
命令方法只接受一个参数,您需要在命令方法本身中进行参数解析.当然,您可以根据需要调用self.do_help()或self.help_< cmd>()方法.
内容总结
以上是互联网集市为您收集整理的检查通过python cmd模块传递的参数全部内容,希望文章能够帮你解决检查通过python cmd模块传递的参数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。