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())
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'))
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'), }
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) ]) }
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())
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')
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())
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())
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())
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())
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() )
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())
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.' ) ]
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)
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.'), ]
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'])
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_)
def test_parse_arguments(self): input_ = ListInput([('name', 'foo')], InputDefinition([InputArgument('name')])) self.assertEqual({'name': 'foo'}, input_.get_arguments())
def test_parse_single_dash_argument(self): input_ = ArgvInput(['cli.py', '-']) input_.bind(InputDefinition([ InputArgument('file') ])) self.assertEqual({'file': '-'}, input_.get_arguments())
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' ) ]
def initialize_arguments(self): self.foo = InputArgument('foo') self.bar = InputArgument('bar') self.foo1 = InputArgument('foo') self.foo2 = InputArgument('foo2', InputArgument.REQUIRED)