diff --git a/pynation/cli.py b/pynation/cli.py index 0673994..66c4888 100644 --- a/pynation/cli.py +++ b/pynation/cli.py @@ -1,22 +1,16 @@ import click from pynation.data import country_data, currency_data, country_calling_code +from pynation.utils import return_country, DefaultCommand -def return_country(data_source, column): - _country = data_source.get(column.title(), None) - if _country is None: - return - return _country - - -@click.group() +@click.group(cls=DefaultCommand) def cli1(): """Gives information about a country""" pass -@cli1.command() +@cli1.command(default_command=True) @click.argument('country_name') def info(country_name): """Brief information about a country.""" @@ -86,4 +80,3 @@ def country_currency(code, country_name): def country_call_code(): """Get information about the calling code of a country""" pass - diff --git a/pynation/utils.py b/pynation/utils.py new file mode 100644 index 0000000..eee8ae7 --- /dev/null +++ b/pynation/utils.py @@ -0,0 +1,35 @@ + +from click import Group, UsageError + + +class DefaultCommand(Group): + + def command(self, *args, **kwargs): + default_command = kwargs.pop("default_command", False) + if default_command and not args: + kwargs["name"] = kwargs.get("name", "info") + + decorator = super(DefaultCommand, self).command(*args, **kwargs) + if default_command: + def new_decorator(f): + cmd = decorator(f) + self.default_command = cmd.name + cmd.hidden = True # Hide "->" from the command line + return cmd + return new_decorator + + return decorator + + def resolve_command(self, ctx, args): + try: + return super(DefaultCommand, self).resolve_command(ctx, args) + except UsageError: + # Since "country_name" is not a command name, we want to pass "->" + args.insert(0, self.default_command) + return super(DefaultCommand, self).resolve_command(ctx, args) + + +def return_country(data_source, column): + _country = data_source.get(column.title(), None) + if _country is not None: + return _country diff --git a/tests/test_cli.py b/tests/test_cli.py index daeb947..ccfbfdc 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,6 +1,7 @@ import pytest -from pynation.cli import cli1 as cli, return_country +from pynation.cli import cli1 as cli +from pynation.utils import return_country from pynation.data import country_calling_code, country_data, currency_data from click.testing import CliRunner @@ -15,15 +16,15 @@ class TestCli: runner = CliRunner() - def test_info_correct_country(self): - result = self.runner.invoke(cli, ['info', 'Nigeria']) + def test_correct_country(self): + result = self.runner.invoke(cli, ["Somalia"]) assert result.exit_code == 0 assert "Information about" in result.output assert "Currency Name" in result.output - assert 'NG' in result.output + assert "SO" in result.output def test_info_wrong_country(self): - result = self.runner.invoke(cli, ['info', 'eeieidjjdl']) + result = self.runner.invoke(cli, ['eeieidjjdl']) assert 'Country does not exist' in result.output def test_short_two_digit(self):