Example #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())
Example #2
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.'
         )
     ]
Example #3
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)
         ])
     }
Example #4
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())
Example #5
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')
Example #6
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_)
Example #7
0
    def test_parse_list_option(self):
        input_ = ArgvInput(['cli.py', '--name=foo', '--name=bar', '--name=baz'])
        input_.bind(
            InputDefinition([
                InputOption('name', None, InputOption.VALUE_OPTIONAL | InputOption.VALUE_IS_LIST)
            ])
        )
        self.assertEqual(
            {'name': ['foo', 'bar', 'baz']},
            input_.get_options()
        )

        input_ = ArgvInput(['cli.py', '--name', 'foo', '--name', 'bar', '--name', 'baz'])
        input_.bind(
            InputDefinition([
                InputOption('name', None, InputOption.VALUE_OPTIONAL | InputOption.VALUE_IS_LIST)
            ])
        )
        self.assertEqual(
            {'name': ['foo', 'bar', 'baz']},
            input_.get_options()
        )

        input_ = ArgvInput(['cli.py', '--name=foo', '--name=bar', '--name='])
        input_.bind(
            InputDefinition([
                InputOption('name', None, InputOption.VALUE_OPTIONAL | InputOption.VALUE_IS_LIST)
            ])
        )
        self.assertEqual(
            {'name': ['foo', 'bar', None]},
            input_.get_options()
        )

        input_ = ArgvInput(['cli.py', '--name', 'foo', '--name', 'bar', '--name', '--another-option'])
        input_.bind(
            InputDefinition([
                InputOption('name', None, InputOption.VALUE_OPTIONAL | InputOption.VALUE_IS_LIST),
                InputOption('another-option', None, InputOption.VALUE_NONE)
            ])
        )
        self.assertEqual(
            {'name': ['foo', 'bar', None], 'another-option': True},
            input_.get_options()
        )
Example #8
0
    def test_get_options_defaults(self):
        definition = InputDefinition([
            InputOption('foo1', None, InputOption.VALUE_NONE),
            InputOption('foo2', None, InputOption.VALUE_REQUIRED),
            InputOption('foo3', None, InputOption.VALUE_REQUIRED, '',
                        'default'),
            InputOption('foo4', None, InputOption.VALUE_OPTIONAL),
            InputOption('foo5', None, InputOption.VALUE_OPTIONAL, '',
                        'default'),
            InputOption('foo6', None, InputOption.VALUE_OPTIONAL
                        | InputOption.VALUE_IS_LIST),
            InputOption('foo7', None,
                        InputOption.VALUE_OPTIONAL | InputOption.VALUE_IS_LIST,
                        '', [1, 2])
        ])
        defaults = {
            'foo1': False,
            'foo2': None,
            'foo3': 'default',
            'foo4': None,
            'foo5': 'default',
            'foo6': [],
            'foo7': [1, 2]
        }

        self.assertEqual(defaults, definition.get_option_defaults())
Example #9
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())
Example #10
0
    def test_validate_options(self):
        definition = InputDefinition([InputOption('opt', validator=Float())])

        input = MockInput(definition)

        input.validate_options()

        # Wrong type
        input = MockWrongInput(definition)

        self.assertRaises(InvalidOption, input.validate_options)
Example #11
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())
Example #12
0
 def provide_options(self):
     return [
         ([('--foo', 'bar')], [InputOption('foo')], {
             'foo': 'bar'
         }),
         ([('--foo', 'bar')], [
             InputOption('foo', 'f', InputOption.VALUE_OPTIONAL, '',
                         'default')
         ], {
             'foo': 'bar'
         }),
         (['--foo'], [
             InputOption('foo', 'f', InputOption.VALUE_OPTIONAL, '',
                         'default')
         ], {
             'foo': 'default'
         }),
         ([('-f', 'bar')], [InputOption('foo', 'f')], {
             'foo': 'bar'
         }),
     ]
Example #13
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.'),
     ]
Example #14
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'])
Example #15
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())
Example #16
0
    def test_setting_input_definition_overwrites_default_values(self):
        application = Application()
        application.set_auto_exit(False)
        application.set_catch_exceptions(False)

        application.set_definition(InputDefinition([
            InputOption('--custom', '-c',
                        InputOption.VALUE_NONE,
                        'Set the custom input definition.')
        ]))

        definition = application.get_definition()

        self.assertFalse(definition.has_argument('command'))

        self.assertFalse(definition.has_option('help'))
        self.assertFalse(definition.has_option('quiet'))
        self.assertFalse(definition.has_option('verbose'))
        self.assertFalse(definition.has_option('version'))
        self.assertFalse(definition.has_option('ansi'))
        self.assertFalse(definition.has_option('no-ansi'))
        self.assertFalse(definition.has_option('no-interaction'))

        self.assertTrue(definition.has_option('custom'))
Example #17
0
 def get_input_options(cls):
     return {
         'input_option_1':
         InputOption('option_name', 'o', InputOption.VALUE_NONE),
         'input_option_2':
         InputOption('option_name', 'o', InputOption.VALUE_OPTIONAL,
                     'option description', 'default_value'),
         'input_option_3':
         InputOption('option_name', 'o', InputOption.VALUE_REQUIRED,
                     'option description'),
         'input_option_4':
         InputOption('option_name', 'o',
                     InputOption.VALUE_IS_LIST | InputOption.VALUE_OPTIONAL,
                     'option description'),
         'input_option_5':
         InputOption('option_name', 'o', InputOption.VALUE_REQUIRED,
                     'multiline\noption description'),
         'input_option_6':
         InputOption('option_name', ['o', 'O'], InputOption.VALUE_REQUIRED,
                     'option with multiple shortcuts'),
     }
Example #18
0
 def initialize_options(self):
     self.foo = InputOption('foo', 'f')
     self.bar = InputOption('bar', 'b')
     self.foo1 = InputOption('fooBis', 'f')
     self.foo2 = InputOption('foo', 'p')
     self.multi = InputOption('multi', 'm|mm|mmm')
Example #19
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'
         )
     ]
Example #20
0
 def get_default_input_definition(self):
     return InputDefinition([
         InputOption('--custom', '-c',
                     InputOption.VALUE_NONE,
                     'Set the custom input definition.')
     ])