Example #1
0
def test_arg_from_free_value():
    cli = CliApp()

    assert cli._arg_from_free_value('dummy', None) \
        == (('--dummy',), {'default': None})

    assert cli._arg_from_free_value('dummy', True) \
        == (('--dummy',), {'default': True, 'action': 'store_false'})
    assert cli._arg_from_free_value('dummy', False) \
        == (('--dummy',), {'default': False, 'action': 'store_true'})

    assert cli._arg_from_free_value('dummy', []) \
        == (('--dummy',), {'action': 'append', 'type': None, 'default': []})
    assert cli._arg_from_free_value('dummy', [str]) \
        == (('--dummy',), {'action': 'append', 'type': str, 'default': []})
    assert cli._arg_from_free_value('dummy', ['string here']) \
        == (('--dummy',), {'action': 'append', 'type': str, 'default': []})

    assert cli._arg_from_free_value('dummy', ['one', 'two', 'three']) \
        == (('--dummy',), {'type': 'choice',
                           'choices': ['one', 'two', 'three'],
                           'default': 'one'})

    assert cli._arg_from_free_value('dummy', str) \
        == (('--dummy',), {'type': str, 'default': None})
    assert cli._arg_from_free_value('dummy', 'hello') \
        == (('--dummy',), {'type': str, 'default': 'hello'})
Example #2
0
def test_make_sure_we_clean_default_args(sample_script, capsys):
    ## todo: we have more cleanup + tests to do!

    cli = CliApp()

    @cli.command
    def cmd_with_explicit_args(aaa=cli.arg(default='spam'),
                               bbb=cli.arg(type=int, default=100),
                               ccc='example'):
        print('aaa: {0}'.format(aaa))
        print('bbb: {0}'.format(bbb))
        print('ccc: {0}'.format(ccc))

    cli.run(['cmd_with_explicit_args', '--aaa=AAA'])
    out, err = capsys.readouterr()
    assert out == 'aaa: AAA\nbbb: 100\nccc: example\n'

    cmd_with_explicit_args()
    out, err = capsys.readouterr()
    assert out == 'aaa: spam\nbbb: 100\nccc: example\n'
Example #3
0
def test_make_sure_we_clean_default_args(sample_script, capsys):
    ## todo: we have more cleanup + tests to do!

    cli = CliApp()

    @cli.command
    def cmd_with_explicit_args(aaa=cli.arg(default='spam'),
                               bbb=cli.arg(type=int, default=100),
                               ccc='example'):
        print('aaa: {0}'.format(aaa))
        print('bbb: {0}'.format(bbb))
        print('ccc: {0}'.format(ccc))

    cli.run(['cmd_with_explicit_args', '--aaa=AAA'])
    out, err = capsys.readouterr()
    assert out == 'aaa: AAA\nbbb: 100\nccc: example\n'

    cmd_with_explicit_args()
    out, err = capsys.readouterr()
    assert out == 'aaa: spam\nbbb: 100\nccc: example\n'
Example #4
0
def test_function_registration():
    """
    Pin-point a nasty bug that was occurring due to a wrong name..
    """

    cli = CliApp()

    def hello(name='world'):
        pass

    func_info = cli._analyze_function(hello)
    assert func_info['keyword_args'] == [('name', 'world')]

    a, kw = cli._arg_from_free_value('name', 'world')
    assert a == ('--name', )
    assert kw == {'default': 'world', 'type': str}

    subparser = cli._register_command(hello)
    assert subparser.get_default('func').func is hello
    assert subparser.get_default('name') == 'world'
Example #5
0
def test_function_registration():
    """
    Pin-point a nasty bug that was occurring due to a wrong name..
    """

    cli = CliApp()

    def hello(name='world'):
        pass

    func_info = cli._analyze_function(hello)
    assert func_info['keyword_args'] == [('name', 'world')]

    a, kw = cli._arg_from_free_value('name', 'world')
    assert a == ('--name',)
    assert kw == {'default': 'world', 'type': str}

    subparser = cli._register_command(hello)
    assert subparser.get_default('func').func is hello
    assert subparser.get_default('name') == 'world'
Example #6
0
def sample_script():

    cli = CliApp()

    @cli.command
    def hello():
        print("Hello, world!")

    @cli.command
    def command_example():
        print("Example text")

    @cli.command
    def hello_required_name(name):
        print("Hello, {0}!".format(name))

    @cli.command
    def hello_optional_name(name='world'):
        print("Hello, {0}!".format(name))

    @cli.command
    def hello_list(name=[str]):
        for nm in name:
            print("Hello, {0}".format(nm))

    @cli.command
    def cmd_with_many_args(arg1, kw1='val1', kw2=123, kw3=False):
        assert isinstance(kw3, bool)
        print('arg1: {0!s}'.format(arg1))
        print('kw1: {0!s}'.format(kw1))
        print('kw2: {0!s}'.format(kw2))
        print('kw3: {0!s}'.format(kw3))

    @cli.command
    def cmd_with_explicit_args(aaa=cli.arg(default='spam'),
                               bbb=cli.arg(type=int, default=100),
                               ccc='example'):
        print('aaa: {0!s}'.format(aaa))
        print('bbb: {0!s}'.format(bbb))
        print('ccc: {0!s}'.format(ccc))

    @cli.command(name='custom_name')
    def command_with_custom_name():
        print('itworks')

    return cli
Example #7
0
#!/usr/bin/env python

from __future__ import print_function

from clitools import CliApp

cli = CliApp()


@cli.command
def hello(name='world', bye=False):
    greet = 'Bye' if bye else 'Hello'
    print("{0}, {1}".format(greet, name))


if __name__ == '__main__':
    cli.run()
Example #8
0
#!/usr/bin/env python

from __future__ import print_function

from clitools import CliApp


cli = CliApp()


@cli.command
def hello(name="world", bye=False):
    greet = "Bye" if bye else "Hello"
    print("{0}, {1}".format(greet, name))


if __name__ == "__main__":
    cli.run()
Example #9
0
def test_arg_from_free_value():
    cli = CliApp()

    assert cli._arg_from_free_value('dummy', None) \
        == (('--dummy',), {'default': None})

    assert cli._arg_from_free_value('dummy', True) \
        == (('--dummy',), {'default': True, 'action': 'store_false'})
    assert cli._arg_from_free_value('dummy', False) \
        == (('--dummy',), {'default': False, 'action': 'store_true'})

    assert cli._arg_from_free_value('dummy', []) \
        == (('--dummy',), {'action': 'append', 'type': None, 'default': []})
    assert cli._arg_from_free_value('dummy', [str]) \
        == (('--dummy',), {'action': 'append', 'type': str, 'default': []})
    assert cli._arg_from_free_value('dummy', ['string here']) \
        == (('--dummy',), {'action': 'append', 'type': str, 'default': []})

    assert cli._arg_from_free_value('dummy', ['one', 'two', 'three']) \
        == (('--dummy',), {'type': 'choice',
                           'choices': ['one', 'two', 'three'],
                           'default': 'one'})

    assert cli._arg_from_free_value('dummy', str) \
        == (('--dummy',), {'type': str, 'default': None})
    assert cli._arg_from_free_value('dummy', 'hello') \
        == (('--dummy',), {'type': str, 'default': 'hello'})
Example #10
0
def test_analyze_function(func, info):
    assert CliApp()._analyze_function(func) == info