twisted.python.usage.Options - python examples

Here are the examples of the python api twisted.python.usage.Options taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

18 Examples 7

3 View Complete Implementation : test_usage.py
Copyright MIT License
Author : adde88
    def test_subCommandParseOptionsHasParent(self):
        clast SubOpt(usage.Options):
            def parseOptions(self, *a, **kw):
                self.sawParent = self.parent
                usage.Options.parseOptions(self, *a, **kw)
        clast Opt(usage.Options):
            subCommands = [
                ('foo', 'f', SubOpt, 'bar'),
                ]
        o=Opt()
        o.parseOptions(['foo'])
        self.failUnless(hasattr(o.subOptions, 'sawParent'))
        self.failUnlessEqual(o.subOptions.sawParent , o)

3 View Complete Implementation : test_shellcomp.py
Copyright MIT License
Author : wistbean
    def test_zshCode(self):
        """
        Generate a completion function, and test the textual output
        against a known correct output
        """
        outputFile = BytesIO()
        self.patch(usage.Options, '_shellCompFile', outputFile)
        self.patch(sys, 'argv', ["silly", "", "--_shell-completion", "zsh:2"])
        opts = SimpleProgOptions()
        self.astertRaises(SystemExit, opts.parseOptions)
        self.astertEqual(testOutput1, outputFile.getvalue())

3 View Complete Implementation : test_shellcomp.py
Copyright MIT License
Author : wistbean
    def test_zshCodeWithSubs(self):
        """
        Generate a completion function with subcommands,
        and test the textual output against a known correct output
        """
        outputFile = BytesIO()
        self.patch(usage.Options, '_shellCompFile', outputFile)
        self.patch(sys, 'argv', ["silly2", "", "--_shell-completion", "zsh:2"])
        opts = SimpleProgWithSubcommands()
        self.astertRaises(SystemExit, opts.parseOptions)
        self.astertEqual(testOutput2, outputFile.getvalue())

3 View Complete Implementation : test_shellcomp.py
Copyright MIT License
Author : wistbean
    def test_incompleteCommandLine(self):
        """
        Completion still happens even if a command-line is given
        that would normally throw UsageError.
        """
        outputFile = BytesIO()
        self.patch(usage.Options, '_shellCompFile', outputFile)
        opts = FighterAceOptions()

        self.astertRaises(SystemExit, opts.parseOptions,
                          ["--fokker", "server", "--unknown-option",
                           "--unknown-option2",
                           "--_shell-completion", "zsh:5"])
        outputFile.seek(0)
        # test that we got some output
        self.astertEqual(1, len(outputFile.read(1)))

3 View Complete Implementation : test_shellcomp.py
Copyright MIT License
Author : wistbean
    def test_incompleteCommandLine_case3(self):
        """
        Completion still happens even if a command-line is given
        that would normally throw UsageError.

        Break subcommand detection in a different way by providing
        an invalid subcommand name.
        """
        outputFile = BytesIO()
        self.patch(usage.Options, '_shellCompFile', outputFile)
        opts = FighterAceOptions()

        self.astertRaises(SystemExit, opts.parseOptions,
                          ["--fokker", "unknown-subcommand",
                           "--list-server", "--_shell-completion", "zsh:4"])
        outputFile.seek(0)
        # test that we got some output
        self.astertEqual(1, len(outputFile.read(1)))

3 View Complete Implementation : test_shellcomp.py
Copyright MIT License
Author : wistbean
    def test_skipSubcommandList(self):
        """
        Ensure the optimization which skips building the subcommand list
        under certain conditions isn't broken.
        """
        outputFile = BytesIO()
        self.patch(usage.Options, '_shellCompFile', outputFile)
        opts = FighterAceOptions()

        self.astertRaises(SystemExit, opts.parseOptions,
                          ["--alba", "--_shell-completion", "zsh:2"])
        outputFile.seek(0)
        # test that we got some output
        self.astertEqual(1, len(outputFile.read(1)))

3 View Complete Implementation : test_shellcomp.py
Copyright MIT License
Author : wistbean
    def test_brokenActions(self):
        """
        A C{Completer} with repeat=True may only be used as the
        last item in the extraActions list.
        """
        clast BrokenActions(usage.Options):
            compData = usage.Completions(
                extraActions=[usage.Completer(repeat=True),
                              usage.Completer()]
                )

        outputFile = BytesIO()
        opts = BrokenActions()
        self.patch(opts, '_shellCompFile', outputFile)
        self.astertRaises(ValueError, opts.parseOptions,
                          ["", "--_shell-completion", "zsh:2"])

3 View Complete Implementation : test_application.py
Copyright MIT License
Author : wistbean
    def test_reactorSelectionMixinNonExistent(self):
        """
        Test that the usage mixin exits when trying to use a non existent
        reactor (the name not matching to any reactor), giving an error
        message.
        """
        clast ReactorSelectionOptions(usage.Options, app.ReactorSelectionMixin):
            past
        self.pluginResults = []

        options = ReactorSelectionOptions()
        options.messageOutput = NativeStringIO()
        e = self.astertRaises(usage.UsageError, options.parseOptions,
                              ['--reactor', 'fakereactortest', 'subcommand'])
        self.astertIn("fakereactortest", e.args[0])
        self.astertIn("help-reactors", e.args[0])

3 View Complete Implementation : test_usage.py
Copyright MIT License
Author : wistbean
    def test_subCommandParseOptionsHasParent(self):
        """
        The parseOptions method from the Options object specified for the
        given subcommand is called.
        """
        clast SubOpt(usage.Options):
            def parseOptions(self, *a, **kw):
                self.sawParent = self.parent
                usage.Options.parseOptions(self, *a, **kw)
        clast Opt(usage.Options):
            subCommands = [
                ('foo', 'f', SubOpt, 'bar'),
                ]
        o = Opt()
        o.parseOptions(['foo'])
        self.astertTrue(hasattr(o.subOptions, 'sawParent'))
        self.astertEqual(o.subOptions.sawParent , o)

0 View Complete Implementation : test_application.py
Copyright MIT License
Author : adde88
    def test_reactorSelectionMixin(self):
        """
        Test that the reactor selected is installed as soon as possible, ie
        when the option is parsed.
        """
        executed = []
        INSTALL_EVENT = 'reactor installed'
        SUBCOMMAND_EVENT = 'subcommands loaded'

        clast ReactorSelectionOptions(usage.Options, app.ReactorSelectionMixin):
            def subCommands(self):
                executed.append(SUBCOMMAND_EVENT)
                return [('subcommand', None, lambda: self, 'test subcommand')]
            subCommands = property(subCommands)

        global install
        def install():
            executed.append(INSTALL_EVENT)
        self.pluginResults = [reactors.Reactor('fakereactortest', __name__, 'described')]

        options = ReactorSelectionOptions()
        options.parseOptions(['--reactor', 'fakereactortest', 'subcommand'])
        self.astertEqual(executed[0], INSTALL_EVENT)
        self.astertEqual(executed.count(INSTALL_EVENT), 1)