class TestPositionalAndOptions(unittest.TestCase): def setUp(self): self.parser = ArgumentParser() def test_postionals_and_options(self): self.parser.add_positional("enemy") self.parser.add_option("-em", nargs='*') self.parser.add_option("-am", nargs='*') self.assertEqual(self.parser.parse("ham -am 1 1 1 2 -1 -em 3 2 1 -1"), {'enemy': 'ham', 'am': ['1', '1', '1', '2', '-1'], 'em': ['3', '2', '1', '-1']})
class TestPositionalAndOptions(unittest.TestCase): def setUp(self): self.parser = ArgumentParser() def test_postionals_and_options(self): self.parser.add_positional("enemy") self.parser.add_option("-em", nargs='*') self.parser.add_option("-am", nargs='*') self.assertEqual( self.parser.parse("ham -am 1 1 1 2 -1 -em 3 2 1 -1"), { 'enemy': 'ham', 'am': ['1', '1', '1', '2', '-1'], 'em': ['3', '2', '1', '-1'] })
def _make_parser(func): """ Build a parser using the params set using the option and positional decorators Used by the command and command_method decorators to build a parser using the arguments that have been added by the option and positional decorators param: func - The function being decorated param: args - A string containing command line like arguments """ params = _get_params(func) del func.__yoga_params__ params.options.reverse() params.positionals.reverse() parser = ArgumentParser() for o in params.options: parser.add_option(*o) for p in params.positionals: parser.add_positional(p) return parser
class TestPositionalArguments(unittest.TestCase): def setUp(self): self.parser = ArgumentParser() def test_single_positional(self): self.parser.add_positional("foo") result = self.parser.parse("baz") self.assertEqual(result, {"foo": "baz"}) with self.assertRaises(ArgumentError): self.parser.parse("") def test_multiple_positionals(self): self.parser.add_positional("foo") self.parser.add_positional("bar") self.parser.add_positional("baz") result = self.parser.parse("ham spam eggs") self.assertEqual(result, {'foo':'ham', 'bar': 'spam', 'baz': 'eggs'}) with self.assertRaises(ArgumentError): self.parser.parse("") with self.assertRaises(ArgumentError): self.parser.parse("ham spam") with self.assertRaises(ArgumentError): self.parser.parse("spam") with self.assertRaises(ArgumentError): self.parser.parse("ham eggs spam bacon")
class TestPositionalArguments(unittest.TestCase): def setUp(self): self.parser = ArgumentParser() def test_single_positional(self): self.parser.add_positional("foo") result = self.parser.parse("baz") self.assertEqual(result, {"foo": "baz"}) with self.assertRaises(ArgumentError): self.parser.parse("") def test_multiple_positionals(self): self.parser.add_positional("foo") self.parser.add_positional("bar") self.parser.add_positional("baz") result = self.parser.parse("ham spam eggs") self.assertEqual(result, {'foo': 'ham', 'bar': 'spam', 'baz': 'eggs'}) with self.assertRaises(ArgumentError): self.parser.parse("") with self.assertRaises(ArgumentError): self.parser.parse("ham spam") with self.assertRaises(ArgumentError): self.parser.parse("spam") with self.assertRaises(ArgumentError): self.parser.parse("ham eggs spam bacon")