def test_case_insensitive_choice(runner): @click.command() @click.option('--foo', type=click.Choice(['Orange', 'Apple'], case_sensitive=False)) def cmd(foo): click.echo(foo) result = runner.invoke(cmd, ['--foo', 'apple']) assert result.exit_code == 0 result = runner.invoke(cmd, ['--foo', 'oRANGe']) assert result.exit_code == 0 result = runner.invoke(cmd, ['--foo', 'Apple']) assert result.exit_code == 0 @click.command() @click.option('--foo', type=click.Choice(['Orange', 'Apple'])) def cmd2(foo): click.echo(foo) result = runner.invoke(cmd2, ['--foo', 'apple']) assert result.exit_code == 2 result = runner.invoke(cmd2, ['--foo', 'oRANGe']) assert result.exit_code == 2 result = runner.invoke(cmd2, ['--foo', 'Apple']) assert result.exit_code == 0
def test_option_choice(): @click.command() @click.option('--opt1', type=click.Choice(['opt11', 'opt12']), help='opt1 help') @click.option('--opt2', type=click.Choice(['opt21', 'opt22']), default='opt21') @click.option('--opt3', type=click.Choice(['opt', 'option'])) def cli(): pass assert choices_with_help(cli, [], '-') == [('--opt1', 'opt1 help'), ('--opt2', None), ('--opt3', None)] assert choices_without_help(cli, [], '--opt') == ['--opt1', '--opt2', '--opt3'] assert choices_without_help(cli, [], '--opt1=') == ['opt11', 'opt12'] assert choices_without_help(cli, [], '--opt2=') == ['opt21', 'opt22'] assert choices_without_help(cli, ['--opt2'], '=') == ['opt21', 'opt22'] assert choices_without_help(cli, ['--opt2', '='], 'opt') == ['opt21', 'opt22'] assert choices_without_help(cli, ['--opt1'], '') == ['opt11', 'opt12'] assert choices_without_help(cli, ['--opt2'], '') == ['opt21', 'opt22'] assert choices_without_help(cli, ['--opt1', 'opt11', '--opt2'], '') == ['opt21', 'opt22'] assert choices_without_help(cli, ['--opt2', 'opt21'], '-') == ['--opt1', '--opt3'] assert choices_without_help(cli, ['--opt1', 'opt11'], '-') == ['--opt2', '--opt3'] assert choices_without_help(cli, ['--opt1'], 'opt') == ['opt11', 'opt12'] assert choices_without_help(cli, ['--opt3'], 'opti') == ['option'] assert choices_without_help(cli, ['--opt1', 'invalid_opt'], '-') == ['--opt2', '--opt3']
def test_option_choice(): @click.command() @click.option('--opt1', type=click.Choice(['opt11', 'opt12'])) @click.option('--opt2', type=click.Choice(['opt21', 'opt22'])) @click.option('--opt3', type=click.Choice(['opt', 'option'])) def cli(): pass assert list(get_choices(cli, 'lol', [], '-')) == ['--opt1', '--opt2', '--opt3'] assert list(get_choices(cli, 'lol', [], '--opt')) == ['--opt1', '--opt2', '--opt3'] assert list(get_choices(cli, 'lol', [], '--opt1=')) == ['opt11', 'opt12'] assert list(get_choices(cli, 'lol', [], '--opt2=')) == ['opt21', 'opt22'] assert list(get_choices(cli, 'lol', ['--opt2'], '=')) == ['opt21', 'opt22'] assert list(get_choices(cli, 'lol', ['--opt2', '='], 'opt')) == ['opt21', 'opt22'] assert list(get_choices(cli, 'lol', ['--opt1'], '')) == ['opt11', 'opt12'] assert list(get_choices(cli, 'lol', ['--opt2'], '')) == ['opt21', 'opt22'] assert list(get_choices(cli, 'lol', ['--opt1', 'opt11', '--opt2'], '')) == ['opt21', 'opt22'] assert list(get_choices(cli, 'lol', ['--opt2', 'opt21'], '-')) == ['--opt1', '--opt3'] assert list(get_choices(cli, 'lol', ['--opt1', 'opt11'], '-')) == ['--opt2', '--opt3'] assert list(get_choices(cli, 'lol', ['--opt1'], 'opt')) == ['opt11', 'opt12'] assert list(get_choices(cli, 'lol', ['--opt3'], 'opti')) == ['option'] assert list(get_choices(cli, 'lol', ['--opt1', 'invalid_opt'], '-')) == ['--opt2', '--opt3']
def test_long_chain_choice(): @click.group() def cli(): pass @cli.group() @click.option('--sub-opt', type=click.Choice(['subopt1', 'subopt2'])) @click.argument('sub-arg', required=False, type=click.Choice(['subarg1', 'subarg2'])) def sub(sub_opt, sub_arg): pass @sub.command(short_help='bsub help') @click.option('--bsub-opt', type=click.Choice(['bsubopt1', 'bsubopt2'])) @click.argument('bsub-arg1', required=False, type=click.Choice(['bsubarg1', 'bsubarg2'])) @click.argument('bbsub-arg2', required=False, type=click.Choice(['bbsubarg1', 'bbsubarg2'])) def bsub(bsub_opt): pass @sub.group('csub') def csub(): pass @csub.command() def dsub(): pass assert choices_with_help(cli, ['sub', 'subarg1'], '') == [('bsub', 'bsub help'), ('csub', '')] assert choices_without_help(cli, ['sub'], '') == ['subarg1', 'subarg2'] assert choices_without_help(cli, ['sub', '--sub-opt'], '') == ['subopt1', 'subopt2'] assert choices_without_help(cli, ['sub', '--sub-opt', 'subopt1'], '') == \ ['subarg1', 'subarg2'] assert choices_without_help( cli, ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub'], '-') == ['--bsub-opt'] assert choices_without_help( cli, ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub'], '') == ['bsubarg1', 'bsubarg2'] assert choices_without_help(cli, ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt'], '') == \ ['bsubopt1', 'bsubopt2'] assert choices_without_help(cli, [ 'sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt', 'bsubopt1', 'bsubarg1' ], '') == ['bbsubarg1', 'bbsubarg2'] assert choices_without_help( cli, ['sub', '--sub-opt', 'subopt1', 'subarg1', 'csub'], '') == ['dsub']
def test_variadic_argument_choice(): @click.command() @click.option('--opt', type=click.Choice(['opt1', 'opt2'])) @click.argument('src', nargs=-1, type=click.Choice(['src1', 'src2'])) def cli(local_opt): pass assert choices_without_help(cli, ['src1', 'src2'], '') == ['src1', 'src2'] assert choices_without_help(cli, ['src1', 'src2'], '--o') == ['--opt'] assert choices_without_help(cli, ['src1', 'src2', '--opt'], '') == ['opt1', 'opt2'] assert choices_without_help(cli, ['src1', 'src2'], '') == ['src1', 'src2']
def test_multi_option_choice(): @click.command() @click.option('--message', '-m', multiple=True, type=click.Choice(['m1', 'm2'])) @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2'])) def cli(local_opt): pass assert choices_without_help(cli, ['-m'], '') == ['m1', 'm2'] assert choices_without_help(cli, ['-m', 'm1', '-m'], '') == ['m1', 'm2'] assert choices_without_help(cli, ['-m', 'm1'], '') == ['arg1', 'arg2']
def test_multi_value_option_choice(): @click.command() @click.option('--pos', nargs=2, type=click.Choice(['pos1', 'pos2'])) @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2'])) def cli(local_opt): pass assert choices_without_help(cli, ['--pos'], '') == ['pos1', 'pos2'] assert choices_without_help(cli, ['--pos', 'pos1'], '') == ['pos1', 'pos2'] assert choices_without_help(cli, ['--pos', 'pos1', 'pos2'], '') == ['arg1', 'arg2'] assert choices_without_help(cli, ['--pos', 'pos1', 'pos2', 'arg1'], '') == []
def test_option_and_arg_choice(): @click.command() @click.option('--opt1', type=click.Choice(['opt11', 'opt12'])) @click.argument('arg1', required=False, type=click.Choice(['arg11', 'arg12'])) @click.option('--opt2', type=click.Choice(['opt21', 'opt22'])) def cli(): pass assert list(get_choices(cli, 'lol', ['--opt1'], '')) == ['opt11', 'opt12'] assert list(get_choices(cli, 'lol', [''], '--opt1=')) == ['opt11', 'opt12'] assert list(get_choices(cli, 'lol', [], '')) == ['arg11', 'arg12'] assert list(get_choices(cli, 'lol', ['--opt2'], '')) == ['opt21', 'opt22']
def test_chaining(): @click.group('cli', chain=True) @click.option('--cli-opt') @click.argument('arg', type=click.Choice(['cliarg1', 'cliarg2'])) def cli(cli_opt, arg): pass @cli.command() @click.option('--asub-opt') def asub(asub_opt): pass @cli.command(help='bsub help') @click.option('--bsub-opt') @click.argument('arg', type=click.Choice(['arg1', 'arg2'])) def bsub(bsub_opt, arg): pass @cli.command() @click.option('--csub-opt') @click.argument('arg', type=click.Choice(['carg1', 'carg2']), default='carg1') def csub(csub_opt, arg): pass assert choices_without_help(cli, [], '-') == ['--cli-opt'] assert choices_without_help(cli, [], '') == ['cliarg1', 'cliarg2'] assert choices_without_help(cli, ['cliarg1', 'asub'], '-') == ['--asub-opt'] assert choices_without_help(cli, ['cliarg1', 'asub'], '') == ['bsub', 'csub'] assert choices_without_help(cli, ['cliarg1', 'bsub'], '') == ['arg1', 'arg2'] assert choices_without_help(cli, ['cliarg1', 'asub', '--asub-opt'], '') == [] assert choices_without_help(cli, ['cliarg1', 'asub', '--asub-opt', '5', 'bsub'], '-') == ['--bsub-opt'] assert choices_without_help(cli, ['cliarg1', 'asub', 'bsub'], '-') == ['--bsub-opt'] assert choices_without_help(cli, ['cliarg1', 'asub', 'csub'], '') == ['carg1', 'carg2'] assert choices_without_help(cli, ['cliarg1', 'bsub', 'arg1', 'csub'], '') == ['carg1', 'carg2'] assert choices_without_help(cli, ['cliarg1', 'asub', 'csub'], '-') == ['--csub-opt'] assert choices_with_help(cli, ['cliarg1', 'asub'], 'b') == [('bsub', 'bsub help')]
def test_hidden(): @click.group() @click.option('--name', hidden=True) @click.option('--choices', type=click.Choice([1, 2]), hidden=True) def cli(name): pass @cli.group(hidden=True) def hgroup(): pass @hgroup.group() def hgroupsub(): pass @cli.command() def asub(): pass @cli.command(hidden=True) @click.option('--hname') def hsub(): pass assert choices_without_help(cli, [], '--n') == [] assert choices_without_help(cli, [], '--c') == [] # If the user exactly types out the hidden param, complete its options. assert choices_without_help(cli, ['--choices'], '') == [1, 2] assert choices_without_help(cli, [], '') == ['asub'] assert choices_without_help(cli, [], '') == ['asub'] assert choices_without_help(cli, [], 'h') == [] # If the user exactly types out the hidden command, complete its subcommands. assert choices_without_help(cli, ['hgroup'], '') == ['hgroupsub'] assert choices_without_help(cli, ['hsub'], '--h') == ['--hname']
def test_multiple_param_decls_not_allowed(runner): with pytest.raises(TypeError): @click.command() @click.argument('x', click.Choice(['a', 'b'])) def copy(x): click.echo(x)
def test_choice_normalization(runner): @click.command(context_settings=CONTEXT_SETTINGS) @click.option('--choice', type=click.Choice(['Foo', 'Bar'])) def cli(choice): click.echo('Foo') result = runner.invoke(cli, ['--CHOICE', 'FOO']) assert result.output == 'Foo\n'
def test_option_and_arg_choice(): @click.command() @click.option('--opt1', type=click.Choice(['opt11', 'opt12'])) @click.argument('arg1', required=False, type=click.Choice(['arg11', 'arg12'])) @click.option('--opt2', type=click.Choice(['opt21', 'opt22'])) def cli(): pass assert choices_without_help(cli, ['--opt1'], '') == ['opt11', 'opt12'] assert choices_without_help(cli, [''], '--opt1=') == ['opt11', 'opt12'] assert choices_without_help(cli, [], '') == ['arg11', 'arg12'] assert choices_without_help(cli, ['--opt2'], '') == ['opt21', 'opt22'] assert choices_without_help(cli, ['arg11'], '--opt') == ['--opt1', '--opt2'] assert choices_without_help(cli, [], '--opt') == ['--opt1', '--opt2']
def test_variadic_argument_choice(): @click.command() @click.argument('src', nargs=-1, type=click.Choice(['src1', 'src2'])) def cli(local_opt): pass assert list(get_choices(cli, 'lol', ['src1', 'src2'], '')) == ['src1', 'src2']
def test_boolean_flag_choice(): @click.command() @click.option('--shout/--no-shout', default=False) @click.argument('arg', required=False, type=click.Choice(['arg1', 'arg2'])) def cli(local_opt): pass assert list(get_choices(cli, 'lol', [], '-')) == ['--shout', '--no-shout'] assert list(get_choices(cli, 'lol', ['--shout'], '')) == ['arg1', 'arg2']
def test_long_chain_choice(): @click.group() def cli(): pass @cli.group('sub') @click.option('--sub-opt', type=click.Choice(['subopt1', 'subopt2'])) @click.argument('sub-arg', required=False, type=click.Choice(['subarg1', 'subarg2'])) def sub(sub_opt): pass @sub.command('bsub') @click.option('--bsub-opt', type=click.Choice(['bsubopt1', 'bsubopt2'])) @click.argument('bsub-arg1', required=False, type=click.Choice(['bsubarg1', 'bsubarg2'])) @click.argument('bbsub-arg2', required=False, type=click.Choice(['bbsubarg1', 'bbsubarg2'])) def bsub(bsub_opt): pass assert list(get_choices(cli, 'lol', ['sub'], '')) == ['subarg1', 'subarg2', 'bsub'] assert list(get_choices(cli, 'lol', ['sub', '--sub-opt'], '')) == ['subopt1', 'subopt2'] assert list(get_choices(cli, 'lol', ['sub', '--sub-opt', 'subopt1'], '')) == \ ['subarg1', 'subarg2', 'bsub'] assert list( get_choices(cli, 'lol', ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub'], '-')) == ['--bsub-opt'] assert list(get_choices(cli, 'lol', ['sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt'], '')) == \ ['bsubopt1', 'bsubopt2'] assert list( get_choices(cli, 'lol', [ 'sub', '--sub-opt', 'subopt1', 'subarg1', 'bsub', '--bsub-opt', 'bsubopt1', 'bsubarg1' ], '')) == ['bbsubarg1', 'bbsubarg2']
def test_missing_choice(runner): @click.command() @click.option('--foo', type=click.Choice(['foo', 'bar']), required=True) def cmd(foo): click.echo(foo) result = runner.invoke(cmd) assert result.exit_code == 2 assert 'Error: Missing option "--foo". Choose from foo, bar.' \ in result.output
def test_choices_list_in_prompt(runner, monkeypatch): @click.command() @click.option('-g', type=click.Choice(['none', 'day', 'week', 'month']), prompt=True) def cli_with_choices(g): pass @click.command() @click.option('-g', type=click.Choice(['none', 'day', 'week', 'month']), prompt=True, show_choices=False) def cli_without_choices(g): pass result = runner.invoke(cli_with_choices, [], input='none') assert '(none, day, week, month)' in result.output result = runner.invoke(cli_without_choices, [], input='none') assert '(none, day, week, month)' not in result.output
def test_argument_choice(): @click.command() @click.argument('arg1', required=True, type=click.Choice(['arg11', 'arg12'])) @click.argument('arg2', type=click.Choice(['arg21', 'arg22']), default='arg21') @click.argument('arg3', type=click.Choice(['arg', 'argument']), default='arg') def cli(): pass assert choices_without_help(cli, [], '') == ['arg11', 'arg12'] assert choices_without_help(cli, [], 'arg') == ['arg11', 'arg12'] assert choices_without_help(cli, ['arg11'], '') == ['arg21', 'arg22'] assert choices_without_help(cli, ['arg12', 'arg21'], '') == ['arg', 'argument'] assert choices_without_help(cli, ['arg12', 'arg21'], 'argu') == ['argument']
def test_argument_choice(): @click.command() @click.argument('arg1', required=False, type=click.Choice(['arg11', 'arg12'])) @click.argument('arg2', required=False, type=click.Choice(['arg21', 'arg22'])) @click.argument('arg3', required=False, type=click.Choice(['arg', 'argument'])) def cli(): pass assert list(get_choices(cli, 'lol', [], '')) == ['arg11', 'arg12'] assert list(get_choices(cli, 'lol', [], 'arg')) == ['arg11', 'arg12'] assert list(get_choices(cli, 'lol', ['arg11'], '')) == ['arg21', 'arg22'] assert list(get_choices(cli, 'lol', ['arg12', 'arg21'], '')) == ['arg', 'argument'] assert list(get_choices(cli, 'lol', ['arg12', 'arg21'], 'argu')) == ['argument']
def test_chaining(): @click.group('cli', chain=True) @click.option('--cli-opt') def cli(cli_opt): pass @cli.command('asub') @click.option('--asub-opt') def asub(asub_opt): pass @cli.command('bsub') @click.option('--bsub-opt') @click.argument('arg', type=click.Choice(['arg1', 'arg2']), required=True) def bsub(bsub_opt, arg): pass @cli.command('csub') @click.option('--csub-opt') @click.argument('arg', type=click.Choice(['carg1', 'carg2']), required=False) def csub(csub_opt, arg): pass assert list(get_choices(cli, 'lol', [], '-')) == ['--cli-opt'] assert list(get_choices(cli, 'lol', [], '')) == ['asub', 'bsub', 'csub'] assert list(get_choices(cli, 'lol', ['asub'], '-')) == ['--asub-opt'] assert list(get_choices(cli, 'lol', ['asub'], '')) == ['bsub', 'csub'] assert list(get_choices(cli, 'lol', ['bsub'], '')) == ['arg1', 'arg2'] assert list(get_choices(cli, 'lol', ['asub', '--asub-opt'], '')) == [] assert list( get_choices(cli, 'lol', ['asub', '--asub-opt', '5', 'bsub'], '-')) == ['--bsub-opt'] assert list(get_choices(cli, 'lol', ['asub', 'bsub'], '-')) == ['--bsub-opt'] assert list(get_choices(cli, 'lol', ['asub', 'csub'], '')) == ['carg1', 'carg2', 'bsub'] assert list(get_choices(cli, 'lol', ['asub', 'csub'], '-')) == ['--csub-opt']
def test_missing_choice(runner): @click.command() @click.option('--foo', type=click.Choice(['foo', 'bar']), required=True) def cmd(foo): click.echo(foo) result = runner.invoke(cmd) assert result.exit_code == 2 error, separator, choices = result.output.partition('Choose from') assert 'Error: Missing option "--foo". ' in error assert 'Choose from' in separator assert 'foo' in choices assert 'bar' in choices
def test_choice_option(runner): @click.command() @click.option('--method', type=click.Choice(['foo', 'bar', 'baz'])) def cli(method): click.echo(method) result = runner.invoke(cli, ['--method=foo']) assert not result.exception assert result.output == 'foo\n' result = runner.invoke(cli, ['--method=meh']) assert result.exit_code == 2 assert 'Invalid value for "--method": invalid choice: meh. ' \ '(choose from foo, bar, baz)' in result.output result = runner.invoke(cli, ['--help']) assert '--method [foo|bar|baz]' in result.output
from qbapi.app import spider from qbapi.common import read_data_file, create_pair_data @click.command() @click.option('--from_api', default="https://api.github.com/user", help='Fetch API endpoint', type=str, show_default=True) @click.option('--from_cred', help='An API credentials eg. token or password', type=str) @click.option('--from_auth', type=click.Choice(['Token', 'Basic'], case_sensitive=False), show_default=True, default='Token') @click.option('--from_map', help="A list of keys used by API as mapping fields", multiple=True) @click.option('--to_api', default="https://targolini.freshdesk.com/api/v2/contacts", help='An API create/update endpoint', type=str, show_default=True) @click.option('--to_cred', help='An API credentials eg. token or password', type=str) @click.option('--to_auth', type=click.Choice(['Token', 'Basic'], case_sensitive=False),
def test_long_chain(): @click.group('cli') @click.option('--cli-opt') def cli(cli_opt): pass @cli.group('asub') @click.option('--asub-opt') def asub(asub_opt): pass @asub.group('bsub') @click.option('--bsub-opt') def bsub(bsub_opt): pass COLORS = ['red', 'green', 'blue'] def get_colors(ctx, args, incomplete): for c in COLORS: if c.startswith(incomplete): yield c def search_colors(ctx, args, incomplete): for c in COLORS: if incomplete in c: yield c CSUB_OPT_CHOICES = ['foo', 'bar'] CSUB_CHOICES = ['bar', 'baz'] @bsub.command('csub') @click.option('--csub-opt', type=click.Choice(CSUB_OPT_CHOICES)) @click.option('--csub', type=click.Choice(CSUB_CHOICES)) @click.option('--search-color', autocompletion=search_colors) @click.argument('color', autocompletion=get_colors) def csub(csub_opt, color): pass assert list(get_choices(cli, 'lol', [], '-')) == ['--cli-opt'] assert list(get_choices(cli, 'lol', [], '')) == ['asub'] assert list(get_choices(cli, 'lol', ['asub'], '-')) == ['--asub-opt'] assert list(get_choices(cli, 'lol', ['asub'], '')) == ['bsub'] assert list(get_choices(cli, 'lol', ['asub', 'bsub'], '-')) == ['--bsub-opt'] assert list(get_choices(cli, 'lol', ['asub', 'bsub'], '')) == ['csub'] assert list( get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], '-')) == ['--csub-opt', '--csub', '--search-color'] assert list( get_choices(cli, 'lol', ['asub', 'bsub', 'csub', '--csub-opt'], '')) == CSUB_OPT_CHOICES assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], '--csub')) == ['--csub-opt', '--csub'] assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub', '--csub'], '')) == CSUB_CHOICES assert list( get_choices(cli, 'lol', ['asub', 'bsub', 'csub', '--csub-opt'], 'f')) == ['foo'] assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], '')) == COLORS assert list(get_choices(cli, 'lol', ['asub', 'bsub', 'csub'], 'b')) == ['blue'] assert list( get_choices(cli, 'lol', ['asub', 'bsub', 'csub', '--search-color'], 'een')) == ['green']