def test_commands(self): p = Parser(commands={ 'foo': Command(short_description=u'foo description'), 'bar': Command(short_description=u'bar description') }, description=u'The script description', out_file=self.out_file) self.assertRaises(SystemExit, p.evaluate, [u'help']) output = self.out_file.getvalue() self.assertContainsAll(output, [ u'usage: script [commands]', p.long_description, u'Commands:', u' foo', p.commands['foo'].short_description, u' bar', p.commands['bar'].short_description ])
def test_commands_and_options(self): p = Parser(options={'foo': Option('f')}, commands={'bar': Command()}, out_file=self.out_file) self.assertRaises(SystemExit, p.evaluate, [u'help']) output = self.out_file.getvalue() self.assertContains(output, u'usage: script [options] [commands]')
def test_commands_and_options(self): p = Parser(commands={ 'spam': Command(), 'eggs': Command() }, options={ 'foo': Option('f'), 'bar': Option('b') }, out_file=self.out_file) self.assertRaises(SystemExit, p.evaluate, [u'help']) output = self.out_file.getvalue() self.assertContainsAll(output, [ u'usage: script [options] [commands]', u'Commands:', u' spam', u' eggs', u'Options:', u' -f', u' -b' ])
def test_alternative_commands(self): p = Parser(commands={ 'stack': Command(), 'stash': Command(), }, out_file=self.out_file, takes_arguments=False) for cmd in [u's', u'st', u'sta']: self.assertRaises(SystemExit, p.evaluate, [cmd]) output = self.out_file.getvalue() self.assertContains(output, u'usage: script [commands]') self.assertContains( output, u'command "{0}" does not exist, did you mean?'.format(cmd)) self.assertContains(output, u'stack') self.assertContains(output, u'stash')
def test_disallow_abbreviated_commands(self): class NewCommand(Command): allow_abbreviated_commands = False c = NewCommand(commands={'foo': Command()}) p = Parser(commands=dict(c=c)) self.assertEqual(p.evaluate([u'c', u'f']), ({'c': ({}, [u'f'])}, []))
def test_options(self): class TestDeclarative(Command): spam = Option('a', 'asomething') eggs = Option('b', 'bsomething') a = TestDeclarative() b = Command(options={ 'spam': Option('a', 'asomething'), 'eggs': Option('b', 'bsomething') }) for c in [a, b]: p = Parser(commands=dict(c=c)) self.assertEqual(p.evaluate([u'c', u'-a', u'foo']), ({ 'c': ({ 'spam': u'foo' }, []) }, [])) self.assertEqual(p.evaluate([u'c', u'--asomething', u'foo']), ({ 'c': ({ 'spam': u'foo' }, []) }, [])) self.assertEqual(p.evaluate([u'c', u'-b', u'foo']), ({ 'c': ({ u'eggs': u'foo' }, []) }, [])) self.assertEqual(p.evaluate([u'c', u'--bsomething', u'foo']), ({ 'c': ({ u'eggs': u'foo' }, []) }, []))
def test_commands(self): class TestDeclarative(Command): spam = Command() eggs = Command() a = TestDeclarative() b = Command(commands={'spam': Command(), 'eggs': Command()}) cp = [u'script_name'] for c in [a, b]: p = Parser(commands=dict(c=c)) self.assertEqual(p.evaluate([u'c', u'spam']), ({ 'c': ({ u'spam': ({}, []) }, []) }, [])) self.assertEqual(p.evaluate([u'c', u'eggs']), ({ 'c': ({ 'eggs': ({}, []) }, []) }, []))
def test_getattr(self): p = Parser(options={'activate': Option('a')}, commands={ 'foo': Command(options={ 'spam': Option('b'), 'eggs': Option('c') }) }) p.activate p.foo p.foo.spam p.foo.eggs
def test_abbreviations(self): c = Command(options={ 'stack': Option(long='stack'), 'stash': Option(long='stash') }, commands={ 'stack': Command(), 'stash': Command() }) p = Parser(commands=dict(c=c)) cp = [u'script_name'] for s in [u's', u'st', u'sta']: cmd = [u'c', s] result = ({'c': ({}, [s])}, []) self.assertEqual(p.evaluate(cmd), result) self.assertEqual(p.evaluate(cmd), result) self.assertEqual(p.evaluate(cmd), result) self.assertEqual(p.evaluate([u'c', u'stac']), ({ 'c': ({ u'stack': ({}, []) }, []) }, [])) self.assertEqual(p.evaluate([u'c', u'stas']), ({ 'c': ({ u'stash': ({}, []) }, []) }, [])) self.assertEqual(p.evaluate([u'c', u'--stac', u'foo']), ({ 'c': ({ u'stack': u'foo' }, []) }, [])) self.assertEqual(p.evaluate([u'c', u'--stas', u'foo']), ({ 'c': ({ u'stash': u'foo' }, []) }, []))
def test_dynamically_adding_nodes(self): p = Parser() p.commands['foo'] = Command() p.commands['foo'].options['a'] = BooleanOption('a') p.options['bar'] = Option('b') self.assertEquals(p.evaluate([u'-b', u'spam']), ({'bar': u'spam'}, [])) self.assertEquals(p.evaluate([u'foo']), ({ 'foo': ({ 'a': False }, []) }, [])) self.assertEquals(p.evaluate([u'foo', u'-a']), ({ 'foo': ({ 'a': True }, []) }, []))
def test_remaining_arguments(self): c = Command(options={'a': Option('a')}) p = Parser(commands=dict(c=c)) self.assertEqual(p.evaluate([u'c', u'foo']), ({ 'c': ({}, [u'foo']) }, [])) self.assertEqual(p.evaluate([u'c', u'-a', u'foo']), ({ 'c': ({ 'a': u'foo' }, []) }, [])) self.assertEqual(p.evaluate([u'c', u'-a', u'foo', u'bar']), ({ u'c': ({ 'a': u'foo' }, [u'bar']) }, []))
def test_only_commands(self): p = Parser(commands={'foo': Command()}, out_file=self.out_file) self.assertRaises(SystemExit, p.evaluate, [u'help']) output = self.out_file.getvalue() self.assertContains(output, u'usage: script [commands]')
class FooParser(Parser): activate = BooleanOption('a') foo = Command(options={'spam': Option('a'), 'eggs': Option('b')})
class TestDeclarative(Command): spam = Command() eggs = Command()