当前位置: 首页>>代码示例>>Python>>正文


Python CLI.add方法代码示例

本文整理汇总了Python中cli.CLI.add方法的典型用法代码示例。如果您正苦于以下问题:Python CLI.add方法的具体用法?Python CLI.add怎么用?Python CLI.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在cli.CLI的用法示例。


在下文中一共展示了CLI.add方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: CLIApplication

# 需要导入模块: from cli import CLI [as 别名]
# 或者: from cli.CLI import add [as 别名]
class CLIApplication(object):
    """An object that runs commands passed from an external invocation of the program.

    .. inheritance-diagram:: CommandLineApp

    Command Line Options::

            {autodoc}
    """
    Runner = HTMLTestRunner or TextTestRunner
    """The TestRunner class."""

    def __init__(self):
        self.cli = CLI()
        self.cli.add(self._cmds)

    def print_help(self, arg):
        """Print help / usage message."""
        return str(self.cli)

    def build_docs(self, format):
        """Build the Sphinx Help Documentation (html or pdf)."""
        self.shell('cd %s; make %s' % (abspath('.') , format or 'html'))

    def run_tests(self, arg):
        """Run the Test Suite (-t 2 for verbose, --quick to skip slow tests).
        """
        verbosity = int(arg) if arg and arg[0] in digits else 1
        testsuite = TestLoader().discover('.', 'test*.py', '.')
        testrunner = self.Runner(verbosity=verbosity)
        result = testrunner.run(testsuite)
        if hasattr(testrunner, 'fname'):
            self.open_file(testrunner.fname)
        return result


    _cmds = {
        ('-h', '--help'):  print_help,
        ('-b', '--build'): build_docs,
        ('-t', '--test'):  run_tests,
    }

    @classmethod
    def new_application(cls):
        """This method can be subclassed to return a alternative application
        object when testing. (Useful for mocking).
        """
        return cls()

    @classmethod
    def isTesting(cls):
        return cls.cmd in ['-t', '--test']

    @classmethod
    def run_application(cls):
        """Run the application using the args passed from the command line.
        This is the main entry point to the application."""
        app = cls.new_application()
        return app.cli(app)

    def shell(self, args, stdin=None, stdout=None, stderr=None, shell=True):
        """Call a shell command."""
        call(args, stdin=stdin, stdout=stdout, stderr=stderr, shell=shell)

    def open_file(self, fname):
        """Open local file `fname` in a web browser."""
        open('file://{}'.format(fname))
开发者ID:caryt,项目名称:utensils,代码行数:69,代码来源:cliapp.py


注:本文中的cli.CLI.add方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。