Esempio n. 1
0
    def test_set_propegate(self):
        child = commands.Command()

        cmd = commands.Command()
        cmd.set(foo="bar")
        cmd.add_child("command", child)

        result = cmd.parse(["command"])
        assert result.foo == "bar"
Esempio n. 2
0
    def test_invalid_subcommand(self):
        cmd = commands.Command()
        cmd.add_child('baz', commands.Command())

        with self.assertRaises(SystemExit):
            cmd.parse(['bar'], prog='foo')

        self.exit_mock.assert_called_once_with(1, 'unrecognized command: bar',
                                               'usage: foo')
Esempio n. 3
0
    def test_subcommand_may_have_positional(self):
        child = commands.Command()
        child.add_argument('bar')

        cmd = commands.Command()
        cmd.add_child('foo', child)

        result = cmd.parse(['foo', 'baz'])
        self.assertEqual(result.bar, 'baz')
Esempio n. 4
0
    def test_invalid_subcommand(self):
        cmd = commands.Command()
        cmd.add_child("baz", commands.Command())

        with self.assertRaises(SystemExit):
            cmd.parse(["bar"], prog="foo")

        self.exit_mock.assert_called_once_with(1, "unrecognized command: bar",
                                               "usage: foo")
Esempio n. 5
0
    def test_subcommand_may_have_positional(self):
        child = commands.Command()
        child.add_argument("bar")

        cmd = commands.Command()
        cmd.add_child("foo", child)

        result = cmd.parse(["foo", "baz"])
        assert result.bar == "baz"
Esempio n. 6
0
    def test_subcommand_may_have_remainder(self):
        child = commands.Command()
        child.add_argument('bar', nargs=argparse.REMAINDER)

        cmd = commands.Command()
        cmd.add_child('foo', child)

        result = cmd.parse(['foo', 'baz', 'bep', 'bop'])
        self.assertEqual(result.bar, ['baz', 'bep', 'bop'])
Esempio n. 7
0
    def test_set_propegate(self):
        child = commands.Command()

        cmd = commands.Command()
        cmd.set(foo='bar')
        cmd.add_child('command', child)

        result = cmd.parse(['command'])
        self.assertEqual(result.foo, 'bar')
Esempio n. 8
0
    def test_subcommand_may_have_remainder(self):
        child = commands.Command()
        child.add_argument("bar", nargs=argparse.REMAINDER)

        cmd = commands.Command()
        cmd.add_child("foo", child)

        result = cmd.parse(["foo", "baz", "bep", "bop"])
        assert result.bar == ["baz", "bep", "bop"]
Esempio n. 9
0
    def test_subcommand_with_documentation_shown(self):
        child = commands.Command()
        child.help = "some text about everything this command does."

        cmd = commands.Command()
        cmd.add_child("bar", child)
        expected = ("usage: foo\n\n"
                    "COMMANDS:\n\n"
                    "bar\n\n"
                    "  some text about everything this command does.")
        assert expected == cmd.format_help("foo").strip()
Esempio n. 10
0
    def test_command_arguments_and_sub_command(self):
        child = commands.Command()
        child.add_argument('--baz')

        cmd = commands.Command()
        cmd.add_argument('--bar')
        cmd.add_child('foo', child)

        result = cmd.parse(['--bar', 'baz', 'foo'])
        self.assertEqual(result.bar, 'baz')
        self.assertEqual(result.baz, None)
Esempio n. 11
0
    def test_command_arguments_and_sub_command(self):
        child = commands.Command()
        child.add_argument("--baz")

        cmd = commands.Command()
        cmd.add_argument("--bar")
        cmd.add_child("foo", child)

        result = cmd.parse(["--bar", "baz", "foo"])
        assert result.bar == "baz"
        assert result.baz is None
Esempio n. 12
0
    def test_innermost_set_wins(self):
        child = commands.Command()
        child.set(foo="bar", baz=1)

        cmd = commands.Command()
        cmd.set(foo="baz", baz=None)
        cmd.add_child("command", child)

        result = cmd.parse(["command"])
        assert result.foo == "bar"
        assert result.baz == 1
Esempio n. 13
0
    def test_subcommand_with_documentation_shown(self):
        child = commands.Command()
        child.help = 'some text about everything this command does.'

        cmd = commands.Command()
        cmd.add_child('bar', child)
        expected = ('usage: foo\n\n'
                    'COMMANDS:\n\n'
                    'bar\n\n'
                    '  some text about everything this command does.')
        self.assertEqual(expected, cmd.format_help('foo').strip())
Esempio n. 14
0
    def test_innermost_set_wins(self):
        child = commands.Command()
        child.set(foo='bar', baz=1)

        cmd = commands.Command()
        cmd.set(foo='baz', baz=None)
        cmd.add_child('command', child)

        result = cmd.parse(['command'])
        self.assertEqual(result.foo, 'bar')
        self.assertEqual(result.baz, 1)
Esempio n. 15
0
    def test_missing_positionals_subcommand(self):
        child = commands.Command()
        child.add_argument('baz')

        cmd = commands.Command()
        cmd.add_child('bar', child)

        with self.assertRaises(SystemExit):
            cmd.parse(['bar'], prog='foo')

        self.exit_mock.assert_called_once_with(1, 'too few arguments',
                                               'usage: foo bar baz')
Esempio n. 16
0
    def test_subcommand_with_positional_shown(self):
        child = commands.Command()
        child.add_argument("baz", help="the great and wonderful")

        cmd = commands.Command()
        cmd.add_child("bar", child)

        expected = ("usage: foo\n\n"
                    "COMMANDS:\n\n"
                    "bar baz\n\n"
                    "    baz  the great and wonderful")
        assert expected == cmd.format_help("foo").strip()
Esempio n. 17
0
    def test_subcommand_with_positional_shown(self):
        child = commands.Command()
        child.add_argument('baz', help='the great and wonderful')

        cmd = commands.Command()
        cmd.add_child('bar', child)

        expected = ('usage: foo\n\n'
                    'COMMANDS:\n\n'
                    'bar baz\n\n'
                    '    baz  the great and wonderful')
        self.assertEqual(expected, cmd.format_help('foo').strip())
Esempio n. 18
0
    def test_subcommand_with_options_shown(self):
        child = commands.Command()
        child.add_argument('-h', '--help', action='store_true',
                           help='show this message')

        cmd = commands.Command()
        cmd.add_child('bar', child)

        expected = ('usage: foo\n\n'
                    'COMMANDS:\n\n'
                    'bar [-h]\n\n'
                    '    -h, --help  show this message')
        self.assertEqual(expected, cmd.format_help('foo').strip())
Esempio n. 19
0
    def test_nested_usage(self):
        child = commands.Command()
        cmd = commands.Command()
        cmd.add_child("bar", child)

        assert "usage: foo" == cmd.format_usage("foo").strip()
        assert "usage: foo bar" == cmd.format_usage("foo bar").strip()

        cmd.add_argument("-h", "--help", action="store_true")
        assert "usage: foo bar" == child.format_usage("foo bar").strip()

        child.add_argument("-h", "--help", action="store_true")
        assert "usage: foo bar [-h]" == child.format_usage("foo bar").strip()
Esempio n. 20
0
    def test_subcommand_with_options_shown(self):
        child = commands.Command()
        child.add_argument("-h",
                           "--help",
                           action="store_true",
                           help="show this message")

        cmd = commands.Command()
        cmd.add_child("bar", child)

        expected = ("usage: foo\n\n"
                    "COMMANDS:\n\n"
                    "bar [-h]\n\n"
                    "    -h, --help  show this message")
        assert expected == cmd.format_help("foo").strip()
Esempio n. 21
0
    def test_nested_subcommands_skipped_intermediate(self):
        subchild = commands.Command()
        subchild.add_argument("--test", help="the great and wonderful")

        child = commands.Command()
        child.add_child("baz", subchild)

        cmd = commands.Command()
        cmd.add_child("bar", child)

        expected = ("usage: foo\n\n"
                    "COMMANDS:\n\n"
                    "bar baz [--test TEST]\n\n"
                    "    --test TEST  the great and wonderful")
        assert expected == cmd.format_help("foo").strip()
Esempio n. 22
0
    def test_missing_positionals_subcommand(self):
        child = commands.Command()
        child.add_argument("baz")

        cmd = commands.Command()
        cmd.add_child("bar", child)

        with self.assertRaises(SystemExit):
            cmd.parse(["bar"], prog="foo")

        self.exit_mock.assert_called_once_with(
            1,
            "the following arguments are required: baz, _args",
            "usage: foo bar baz",
        )
Esempio n. 23
0
    def test_nested_subcommands_skipped_intermediate(self):
        subchild = commands.Command()
        subchild.add_argument('--test', help='the great and wonderful')

        child = commands.Command()
        child.add_child('baz', subchild)

        cmd = commands.Command()
        cmd.add_child('bar', child)

        expected = ('usage: foo\n\n'
                    'COMMANDS:\n\n'
                    'bar baz [--test TEST]\n\n'
                    '    --test TEST  the great and wonderful')
        self.assertEqual(expected, cmd.format_help('foo').strip())
Esempio n. 24
0
    def test_nested_usage(self):
        child = commands.Command()
        cmd = commands.Command()
        cmd.add_child('bar', child)

        self.assertEqual('usage: foo', cmd.format_usage('foo').strip())
        self.assertEqual('usage: foo bar', cmd.format_usage('foo bar').strip())

        cmd.add_argument('-h', '--help', action='store_true')
        self.assertEqual('usage: foo bar',
                         child.format_usage('foo bar').strip())

        child.add_argument('-h', '--help', action='store_true')
        self.assertEqual('usage: foo bar [-h]',
                         child.format_usage('foo bar').strip())
Esempio n. 25
0
    def test_command_with_option_and_subcommand_with_option(self):
        child = commands.Command()
        child.add_argument('--test', help='the great and wonderful')

        cmd = commands.Command()
        cmd.add_argument('-h', '--help', action='store_true',
                         help='show this message')
        cmd.add_child('bar', child)

        expected = ('usage: foo [-h]\n\n'
                    'OPTIONS:\n\n'
                    '  -h, --help  show this message\n\n'
                    'COMMANDS:\n\n'
                    'bar [--test TEST]\n\n'
                    '    --test TEST  the great and wonderful')
        self.assertEqual(expected, cmd.format_help('foo').strip())
Esempio n. 26
0
    def test_command_with_documentation(self):
        cmd = commands.Command()
        cmd.help = "some text about everything this command does."

        expected = ("usage: foo\n\n"
                    "some text about everything this command does.")
        assert expected == cmd.format_help("foo").strip()
Esempio n. 27
0
    def test_command_with_documentation(self):
        cmd = commands.Command()
        cmd.help = 'some text about everything this command does.'

        expected = ('usage: foo\n\n'
                    'some text about everything this command does.')
        self.assertEqual(expected, cmd.format_help('foo').strip())
Esempio n. 28
0
    def test_unknown_command(self):
        cmd = commands.Command()

        with self.assertRaises(SystemExit):
            cmd.parse(['--help'], prog='foo')

        self.exit_mock.assert_called_once_with(
            1, 'unrecognized arguments: --help', 'usage: foo')
Esempio n. 29
0
    def test_command_with_option(self):
        cmd = commands.Command()
        cmd.add_argument('-h', '--help', action='store_true',
                         help='show this message')

        expected = ('usage: foo [-h]\n\n'
                    'OPTIONS:\n\n'
                    '  -h, --help  show this message')
        self.assertEqual(expected, cmd.format_help('foo').strip())
Esempio n. 30
0
    def test_missing_required(self):
        cmd = commands.Command()
        cmd.add_argument('--bar', required=True)

        with self.assertRaises(SystemExit):
            cmd.parse([], prog='foo')

        self.exit_mock.assert_called_once_with(1, 'argument --bar is required',
                                               'usage: foo --bar BAR')