Пример #1
0
    def test_get_synopsis(self):
        definition = InputDefinition([InputOption('foo')])
        self.assertEqual('[--foo]', definition.get_synopsis())
        definition = InputDefinition([InputOption('foo', 'f')])
        self.assertEqual('[-f|--foo]', definition.get_synopsis())
        definition = InputDefinition(
            [InputOption('foo', 'f', InputOption.VALUE_REQUIRED)])
        self.assertEqual('[-f|--foo="..."]', definition.get_synopsis())
        definition = InputDefinition(
            [InputOption('foo', 'f', InputOption.VALUE_OPTIONAL)])
        self.assertEqual('[-f|--foo[="..."]]', definition.get_synopsis())

        definition = InputDefinition([InputArgument('foo')])
        self.assertEqual('[foo]', definition.get_synopsis())
        definition = InputDefinition(
            [InputArgument('foo', InputArgument.REQUIRED)])
        self.assertEqual('foo', definition.get_synopsis())
        definition = InputDefinition(
            [InputArgument('foo', InputArgument.IS_LIST)])
        self.assertEqual('[foo1] ... [fooN]', definition.get_synopsis())
        definition = InputDefinition([
            InputArgument('foo',
                          InputArgument.REQUIRED | InputArgument.IS_LIST)
        ])
        self.assertEqual('foo1 ... [fooN]', definition.get_synopsis())
Пример #2
0
    def test_list_argument_has_to_be_last(self):
        self.initialize_arguments()

        definition = InputDefinition()
        definition.add_argument(InputArgument('foolist',
                                              InputArgument.IS_LIST))
        self.assertRaisesRegexp(
            Exception, 'Cannot add an argument after a list argument.',
            definition.add_argument, InputArgument('anotherbar'))
Пример #3
0
 def get_input_arguments(cls):
     return {
         'input_argument_1':
         InputArgument('argument_name', InputArgument.REQUIRED),
         'input_argument_2':
         InputArgument('argument_name', InputArgument.IS_LIST,
                       'argument description'),
         'input_argument_3':
         InputArgument('argument_name', InputArgument.OPTIONAL,
                       'argument description', 'default_value'),
         'input_argument_4':
         InputArgument('argument_name', InputArgument.REQUIRED,
                       'multiline\nargument description'),
     }
Пример #4
0
 def get_input_definitions(cls):
     return {
         'input_definition_1':
         InputDefinition(),
         'input_definition_2':
         InputDefinition(
             [InputArgument('argument_name', InputArgument.REQUIRED)]),
         'input_definition_3':
         InputDefinition(
             [InputOption('option_name', 'o', InputOption.VALUE_NONE)]),
         'input_definition_4':
         InputDefinition([
             InputArgument('argument_name', InputArgument.REQUIRED),
             InputOption('option_name', 'o', InputOption.VALUE_NONE)
         ])
     }
Пример #5
0
 def test_parse_empty_string_argument(self):
     input_ = ArgvInput(['cli.py', '-f', 'bar', ''])
     input_.bind(InputDefinition([
         InputArgument('empty'),
         InputOption('foo', 'f', InputOption.VALUE_OPTIONAL)
     ]))
     self.assertEqual({'empty': ''}, input_.get_arguments())
Пример #6
0
class NoConfigureCommand(Command):

    name = 'no:configure'

    description = 'description'

    help = 'help'

    arguments = [InputArgument('foo'), InputArgument('bar')]

    options = [InputOption('baz', 'z'), InputOption('bazz', 'Z')]

    def execute(self, input_, output_):
        output_.writeln('execute called')

    def interact(self, input_, output_):
        output_.writeln('interact called')
Пример #7
0
    def test_get_synopsis(self):
        definition = InputDefinition([InputOption('foo')])
        self.assertEqual('[--foo]', definition.get_synopsis())
        definition = InputDefinition([InputOption('foo', 'f')])
        self.assertEqual('[-f|--foo]', definition.get_synopsis())
        definition = InputDefinition([InputOption('foo', 'f', InputOption.VALUE_REQUIRED)])
        self.assertEqual('[-f|--foo FOO]', definition.get_synopsis())
        definition = InputDefinition([InputOption('foo', 'f', InputOption.VALUE_OPTIONAL)])
        self.assertEqual('[-f|--foo [FOO]]', definition.get_synopsis())

        definition = InputDefinition([InputArgument('foo')])
        self.assertEqual('[<foo>]', definition.get_synopsis())
        definition = InputDefinition([InputArgument('foo', InputArgument.REQUIRED)])
        self.assertEqual('<foo>', definition.get_synopsis())
        definition = InputDefinition([InputArgument('foo', InputArgument.IS_LIST)])
        self.assertEqual('[<foo>]...', definition.get_synopsis())
        definition = InputDefinition([InputArgument('foo', InputArgument.REQUIRED | InputArgument.IS_LIST)])
        self.assertEqual('<foo> (<foo>)...', definition.get_synopsis())
Пример #8
0
    def test_negative_number_after_double_dash(self):
        input_ = ArgvInput(['cli.py', '--', '-1'])
        input_.bind(
            InputDefinition([
                InputArgument('number')
            ])
        )
        self.assertEqual({'number': '-1'}, input_.get_arguments())

        input_ = ArgvInput(['cli.py', '-f', 'bar', '--', '-1'])
        input_.bind(
            InputDefinition([
                InputArgument('number'),
                InputOption('foo', 'f', InputOption.VALUE_OPTIONAL)
            ])
        )
        self.assertEqual({'foo': 'bar'}, input_.get_options())
        self.assertEqual({'number': '-1'}, input_.get_arguments())
Пример #9
0
    def test_get_argument_default(self):
        definition = InputDefinition([
            InputArgument('foo1', InputArgument.OPTIONAL),
            InputArgument('foo2', InputArgument.OPTIONAL, '', 'default'),
            InputArgument('foo3', InputArgument.OPTIONAL | InputArgument.IS_LIST),
        ])
        self.assertEqual(
            {
                'foo1': None,
                'foo2': 'default',
                'foo3': []
            },
            definition.get_argument_defaults()
        )

        definition = InputDefinition([
            InputArgument('foo4', InputArgument.OPTIONAL | InputArgument.IS_LIST, '', [1, 2])
        ])
        self.assertEqual({'foo4': [1, 2]}, definition.get_argument_defaults())
Пример #10
0
 def test_as_text(self):
     definition = InputDefinition([
         InputArgument('foo', InputArgument.OPTIONAL, 'The foo argument'),
         InputArgument('baz', InputArgument.OPTIONAL, 'The baz argument',
                       True),
         InputArgument('bar',
                       InputArgument.OPTIONAL | InputArgument.IS_LIST,
                       'The bar argument', ['http://foo.com/']),
         InputOption('foo', 'f', InputOption.VALUE_REQUIRED,
                     'The foo option'),
         InputOption('baz', None, InputOption.VALUE_OPTIONAL,
                     'The baz option', False),
         InputOption('bar', 'b', InputOption.VALUE_OPTIONAL,
                     'The bar option', 'bar'),
         InputOption('qux', '',
                     InputOption.VALUE_OPTIONAL | InputOption.VALUE_IS_LIST,
                     'The qux option', ['http://foo.com/', 'bar'])
     ])
     self.assertEqual(self.open_fixture('definition_astext.txt'),
                      definition.as_text())
Пример #11
0
    def test_parse_list_argument(self):
        input_ = ArgvInput(['cli.py', 'foo', 'bar', 'baz', 'bat'])
        input_.bind(
            InputDefinition([
                InputArgument('name', InputArgument.IS_LIST)
            ])
        )

        self.assertEqual(
            {'name': ['foo', 'bar', 'baz', 'bat']},
            input_.get_arguments()
        )
Пример #12
0
    def test_parse_arguments(self):
        input_ = ArgvInput(['cli.py', 'foo'])
        input_.bind(
            InputDefinition([
                InputArgument('name')
            ])
        )

        self.assertEqual({'name': 'foo'}, input_.get_arguments())

        # parse is stateless
        self.assertEqual({'name': 'foo'}, input_.get_arguments())
Пример #13
0
 def provide_invalid_input(self):
     return [
         (
             ['cli.py', '--foo'],
             InputDefinition([
                 InputOption('foo', 'f', InputOption.VALUE_REQUIRED)
             ]),
             'The "--foo" option requires a value.'
         ),
         (
             ['cli.py', '-f'],
             InputDefinition([
                 InputOption('foo', 'f', InputOption.VALUE_REQUIRED)
             ]),
             'The "--foo" option requires a value.'
         ),
         (
             ['cli.py', '-ffoo'],
             InputDefinition([
                 InputOption('foo', 'f', InputOption.VALUE_NONE)
             ]),
             'The "-o" option does not exist.'
         ),
         (
             ['cli.py', '--foo=bar'],
             InputDefinition([
                 InputOption('foo', 'f', InputOption.VALUE_NONE)
             ]),
             'The "--foo" option does not accept a value.'
         ),
         (
             ['cli.py', 'foo', 'bar'],
             InputDefinition(),
             'Too many arguments.'
         ),
         (
             ['cli.py', '--foo'],
             InputDefinition(),
             'The "--foo" option does not exist.'
         ),
         (
             ['cli.py', '-f'],
             InputDefinition(),
             'The "-f" option does not exist.'
         ),
         (
             ['cli.py', '-1'],
             InputDefinition([
                 InputArgument('number')
             ]),
             'The "-1" option does not exist.'
         )
     ]
Пример #14
0
    def test_validate_arguments(self):
        definition = InputDefinition(
            [InputArgument('arg', validator=Integer())])

        input = MockInput(definition)

        input.validate_arguments()

        # Wrong type
        input = MockWrongInput(definition)

        self.assertRaises(InvalidArgument, input.validate_arguments)
Пример #15
0
 def provider_invalid_input(self):
     return [
         ([('foo', 'bar')], InputDefinition([InputArgument('name')]),
          'The "foo" argument does not exist.'),
         (['--foo'],
          InputDefinition([
              InputOption('foo', 'f', InputOption.VALUE_REQUIRED)
          ]), 'The "--foo" option requires a value.'),
         ([('--foo', 'foo')], InputDefinition(),
          'The "--foo" option does not exist.'),
         ([('-f', 'foo')], InputDefinition(),
          'The "-f" option does not exist.'),
     ]
Пример #16
0
    def test_init(self):
        input = MockInput()

        self.assertIsNotNone(input.definition)
        self.assertEqual(input.arguments, {})
        self.assertEqual(input.arguments, {})

        definition = InputDefinition(
            [InputArgument('arg'), InputOption('opt')])

        input = MockInput(definition)

        self.assertEqual(definition, input.definition)
        self.assertEqual('57', input.arguments['arg'])
        self.assertEqual('37.25', input.options['opt'])
Пример #17
0
    def test_adding_already_set_definition_element_data(self):
        data = [[InputArgument('command', InputArgument.REQUIRED)],
                [InputOption('quiet', '', InputOption.VALUE_NONE)],
                [InputOption('query', 'q', InputOption.VALUE_NONE)]]

        for d in data:
            application = Application()
            application.set_auto_exit(False)
            application.set_catch_exceptions(False)
            application.register('foo')\
                .set_definition(d)\
                .set_code(lambda in_, out_: None)

            input_ = ListInput([('command', 'foo')])
            output_ = NullOutput()

            self.assertRaises(Exception, application.run, input_, output_)
Пример #18
0
    def test_parse_arguments(self):
        input_ = ListInput([('name', 'foo')],
                           InputDefinition([InputArgument('name')]))

        self.assertEqual({'name': 'foo'}, input_.get_arguments())
Пример #19
0
 def test_parse_single_dash_argument(self):
     input_ = ArgvInput(['cli.py', '-'])
     input_.bind(InputDefinition([
         InputArgument('file')
     ]))
     self.assertEqual({'file': '-'}, input_.get_arguments())
Пример #20
0
 def provide_options(self):
     return [
         (
             ['cli.py', '--foo'],
             [InputOption('foo')],
             {'foo': True},
             '.parse() parses long options without a value'
         ),
         (
             ['cli.py', '--foo=bar'],
             [InputOption('foo', 'f', InputOption.VALUE_REQUIRED)],
             {'foo': 'bar'},
             '.parse() parses long options with a required value (with a = separator)'
         ),
         (
             ['cli.py', '--foo', 'bar'],
             [InputOption('foo', 'f', InputOption.VALUE_REQUIRED)],
             {'foo': 'bar'},
             '.parse() parses long options with a required value (with a space separator)'
         ),
         (
             ['cli.py', '-f'],
             [InputOption('foo', 'f')],
             {'foo': True},
             '.parse() parses short options without a value'
         ),
         (
             ['cli.py', '-fbar'],
             [InputOption('foo', 'f', InputOption.VALUE_REQUIRED)],
             {'foo': 'bar'},
             '.parse() parses short options with a required value (with no separator)'
         ),
         (
             ['cli.py', '-f', 'bar'],
             [InputOption('foo', 'f', InputOption.VALUE_REQUIRED)],
             {'foo': 'bar'},
             '.parse() parses short options with a required value (with a space separator)'
         ),
         (
             ['cli.py', '-f', ''],
             [InputOption('foo', 'f', InputOption.VALUE_OPTIONAL)],
             {'foo': None},
             '.parse() parses short options with an optional empty value'
         ),
         (
             ['cli.py', '-f', '', 'foo'],
             [
                 InputArgument('name'),
                 InputOption('foo', 'f', InputOption.VALUE_OPTIONAL)
             ],
             {'foo': None},
             '.parse() parses short options with an optional empty value followed by an argument'
         ),
         (
             ['cli.py', '-f', '', '-b'],
             [
                 InputOption('foo', 'f', InputOption.VALUE_OPTIONAL),
                 InputOption('bar', 'b')
             ],
             {'foo': None, 'bar': True},
             '.parse() parses short options with an optional empty value followed by an option'
         ),
         (
             ['cli.py', '-f', '-b', 'foo'],
             [
                 InputArgument('name'),
                 InputOption('foo', 'f', InputOption.VALUE_OPTIONAL),
                 InputOption('bar', 'b')
             ],
             {'foo': None, 'bar': True},
             '.parse() parses long options with an optional value which is not present'
         ),
         (
             ['cli.py', '-fb'],
             [
                 InputOption('foo', 'f'),
                 InputOption('bar', 'b')
             ],
             {'foo': True, 'bar': True},
             '.parse() parses short options when they are aggregated as a single one'
         ),
         (
             ['cli.py', '-fb', 'bar'],
             [
                 InputOption('foo', 'f'),
                 InputOption('bar', 'b', InputOption.VALUE_REQUIRED)
             ],
             {'foo': True, 'bar': 'bar'},
             '.parse() parses short options when they are aggregated as a single one '
             'and the last one has a required value'
         ),
         (
             ['cli.py', '-fb', 'bar'],
             [
                 InputOption('foo', 'f'),
                 InputOption('bar', 'b', InputOption.VALUE_OPTIONAL)
             ],
             {'foo': True, 'bar': 'bar'},
             '.parse() parses short options when they are aggregated as a single one '
             'and the last one has an optional value'
         ),
         (
             ['cli.py', '-fbbar'],
             [
                 InputOption('foo', 'f'),
                 InputOption('bar', 'b', InputOption.VALUE_OPTIONAL)
             ],
             {'foo': True, 'bar': 'bar'},
             '.parse() parses short options when they are aggregated as a single one '
             'and the last one has a required value with no separator'
         ),
         (
             ['cli.py', '-fbbar'],
             [
                 InputOption('foo', 'f', InputOption.VALUE_OPTIONAL),
                 InputOption('bar', 'b', InputOption.VALUE_OPTIONAL)
             ],
             {'foo': 'bbar', 'bar': None},
             '.parse() parses short options when they are aggregated as a single one '
             'and one of them takes a value'
         )
     ]
Пример #21
0
 def initialize_arguments(self):
     self.foo = InputArgument('foo')
     self.bar = InputArgument('bar')
     self.foo1 = InputArgument('foo')
     self.foo2 = InputArgument('foo2', InputArgument.REQUIRED)