Skip to content

Commit

Permalink
Command line rework
Browse files Browse the repository at this point in the history
This commit allows passing country name directly to pynation.
This means "pynation Somalia" works instead of "pynation info Somalia".
  • Loading branch information
mrbazzan committed Feb 27, 2022
1 parent 9c72aa7 commit 692293e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
13 changes: 3 additions & 10 deletions pynation/cli.py
Original file line number Diff line number Diff line change
@@ -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."""
Expand Down Expand Up @@ -86,4 +80,3 @@ def country_currency(code, country_name):
def country_call_code():
"""Get information about the calling code of a country"""
pass

35 changes: 35 additions & 0 deletions pynation/utils.py
Original file line number Diff line number Diff line change
@@ -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
11 changes: 6 additions & 5 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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):
Expand Down

0 comments on commit 692293e

Please sign in to comment.