countrycodes-cli 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- countrycodes_cli/__init__.py +7 -0
- countrycodes_cli/cli.py +163 -0
- countrycodes_cli-0.1.0.dist-info/METADATA +87 -0
- countrycodes_cli-0.1.0.dist-info/RECORD +7 -0
- countrycodes_cli-0.1.0.dist-info/WHEEL +4 -0
- countrycodes_cli-0.1.0.dist-info/entry_points.txt +3 -0
- countrycodes_cli-0.1.0.dist-info/licenses/LICENSE +29 -0
countrycodes_cli/cli.py
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"""Command-line interface for country code lookups."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
from dataclasses import asdict
|
|
5
|
+
from typing import Annotated, Literal
|
|
6
|
+
|
|
7
|
+
import countrycodes as cc
|
|
8
|
+
import typer
|
|
9
|
+
from rich.console import Console
|
|
10
|
+
from rich.table import Table
|
|
11
|
+
|
|
12
|
+
app = typer.Typer(add_completion=False, no_args_is_help=True)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
@app.callback(invoke_without_command=True)
|
|
16
|
+
def main(
|
|
17
|
+
version: Annotated[
|
|
18
|
+
bool, typer.Option("--version", is_eager=True, help="Show version and exit.")
|
|
19
|
+
] = False,
|
|
20
|
+
) -> None:
|
|
21
|
+
if version:
|
|
22
|
+
typer.echo(cc.__version__)
|
|
23
|
+
raise typer.Exit()
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@app.command()
|
|
27
|
+
def lookup(
|
|
28
|
+
query: Annotated[str, typer.Argument(help="Country name, ISO code, or dial code.")],
|
|
29
|
+
output_format: Annotated[
|
|
30
|
+
Literal["text", "json"],
|
|
31
|
+
typer.Option("--format", help="Output format."),
|
|
32
|
+
] = "text",
|
|
33
|
+
limit: Annotated[
|
|
34
|
+
int,
|
|
35
|
+
typer.Option("--limit", min=1, help="Maximum number of matches to show."),
|
|
36
|
+
] = 5,
|
|
37
|
+
) -> None:
|
|
38
|
+
"""Look up a country by name, ISO code, or dial code."""
|
|
39
|
+
countries: tuple[cc.Country, ...] = (
|
|
40
|
+
tuple(
|
|
41
|
+
country
|
|
42
|
+
for country in cc.list_countries()
|
|
43
|
+
if country.dial_code == query.strip()
|
|
44
|
+
)
|
|
45
|
+
if query.strip().startswith("+")
|
|
46
|
+
else ()
|
|
47
|
+
)
|
|
48
|
+
is_dial_code_lookup = bool(countries)
|
|
49
|
+
has_more_matches = False
|
|
50
|
+
is_fuzzy_lookup = False
|
|
51
|
+
normalized_query = _normalize_query(query)
|
|
52
|
+
|
|
53
|
+
if is_dial_code_lookup:
|
|
54
|
+
has_more_matches = len(countries) > limit
|
|
55
|
+
countries = countries[:limit]
|
|
56
|
+
else:
|
|
57
|
+
matches = cc.search_countries(query, limit=limit + 1)
|
|
58
|
+
if not matches:
|
|
59
|
+
typer.secho(f"No country matches {query!r}", fg=typer.colors.RED, err=True)
|
|
60
|
+
raise typer.Exit(1)
|
|
61
|
+
has_more_matches = len(matches) > limit
|
|
62
|
+
countries = matches[:limit]
|
|
63
|
+
first_country = countries[0]
|
|
64
|
+
is_fuzzy_lookup = normalized_query not in {
|
|
65
|
+
_normalize_query(value)
|
|
66
|
+
for value in (
|
|
67
|
+
first_country.alpha2,
|
|
68
|
+
first_country.alpha3,
|
|
69
|
+
first_country.numeric,
|
|
70
|
+
first_country.dial_code,
|
|
71
|
+
first_country.display_name,
|
|
72
|
+
)
|
|
73
|
+
if value is not None
|
|
74
|
+
}
|
|
75
|
+
if is_fuzzy_lookup and all(
|
|
76
|
+
cc.country_name_similarity(query, country) == 0.0 for country in countries
|
|
77
|
+
):
|
|
78
|
+
typer.secho(f"No country matches {query!r}", fg=typer.colors.RED, err=True)
|
|
79
|
+
raise typer.Exit(1)
|
|
80
|
+
|
|
81
|
+
if not countries:
|
|
82
|
+
typer.secho(f"No country matches {query!r}", fg=typer.colors.RED, err=True)
|
|
83
|
+
raise typer.Exit(1)
|
|
84
|
+
|
|
85
|
+
if output_format == "json":
|
|
86
|
+
first_country = next(iter(countries), None)
|
|
87
|
+
result = (
|
|
88
|
+
[asdict(country) for country in countries]
|
|
89
|
+
if first_country is None or len(countries) > 1
|
|
90
|
+
else asdict(first_country)
|
|
91
|
+
)
|
|
92
|
+
typer.echo(json.dumps(result, ensure_ascii=False))
|
|
93
|
+
else:
|
|
94
|
+
table = Table(show_lines=False)
|
|
95
|
+
table.add_column("Alpha-2")
|
|
96
|
+
table.add_column("Alpha-3")
|
|
97
|
+
table.add_column("Numeric")
|
|
98
|
+
table.add_column("Dial Code")
|
|
99
|
+
table.add_column("Continent")
|
|
100
|
+
table.add_column("Country")
|
|
101
|
+
if is_fuzzy_lookup:
|
|
102
|
+
table.add_column("Similarity", justify="right", no_wrap=True)
|
|
103
|
+
for country in countries:
|
|
104
|
+
row = (
|
|
105
|
+
country.alpha2,
|
|
106
|
+
country.alpha3,
|
|
107
|
+
country.numeric,
|
|
108
|
+
country.dial_code or "[dim]NULL[/dim]",
|
|
109
|
+
country.continent,
|
|
110
|
+
country.display_name,
|
|
111
|
+
)
|
|
112
|
+
if is_fuzzy_lookup:
|
|
113
|
+
score = cc.country_name_similarity(query, country)
|
|
114
|
+
row += (f"{score:.1%}",)
|
|
115
|
+
table.add_row(*row)
|
|
116
|
+
Console().print(table)
|
|
117
|
+
if has_more_matches:
|
|
118
|
+
Console().print(f"[dim]Showing only the top {limit} matches.[/dim]")
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
@app.command(name="list")
|
|
122
|
+
def list_countries(
|
|
123
|
+
output_format: Annotated[
|
|
124
|
+
Literal["text", "json"],
|
|
125
|
+
typer.Option("--format", help="Output format."),
|
|
126
|
+
] = "text",
|
|
127
|
+
) -> None:
|
|
128
|
+
"""List all countries and territories."""
|
|
129
|
+
countries = cc.list_countries()
|
|
130
|
+
if output_format == "json":
|
|
131
|
+
typer.echo(
|
|
132
|
+
json.dumps(
|
|
133
|
+
[asdict(country) for country in countries],
|
|
134
|
+
ensure_ascii=False,
|
|
135
|
+
)
|
|
136
|
+
)
|
|
137
|
+
else:
|
|
138
|
+
table = Table(
|
|
139
|
+
"#",
|
|
140
|
+
"Alpha-2",
|
|
141
|
+
"Alpha-3",
|
|
142
|
+
"Numeric",
|
|
143
|
+
"Dial Code",
|
|
144
|
+
"Continent",
|
|
145
|
+
"Country",
|
|
146
|
+
show_lines=False,
|
|
147
|
+
)
|
|
148
|
+
for index, country in enumerate(countries, start=1):
|
|
149
|
+
table.add_row(
|
|
150
|
+
str(index),
|
|
151
|
+
country.alpha2,
|
|
152
|
+
country.alpha3,
|
|
153
|
+
country.numeric,
|
|
154
|
+
country.dial_code or "[dim]NULL[/dim]",
|
|
155
|
+
country.continent,
|
|
156
|
+
country.display_name,
|
|
157
|
+
)
|
|
158
|
+
Console().print(table)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def _normalize_query(value: str) -> str:
|
|
162
|
+
"""Return normalized text using the library's whitespace and case rules."""
|
|
163
|
+
return " ".join(value.casefold().split())
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: countrycodes-cli
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Command-line interface for the countrycodes library
|
|
5
|
+
License-Expression: BSD-3-Clause
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Requires-Dist: countrycodes==0.1.0
|
|
8
|
+
Requires-Dist: rich>=13.7.1
|
|
9
|
+
Requires-Dist: typer>=0.24.1
|
|
10
|
+
Requires-Python: >=3.11
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
<div align="center">
|
|
14
|
+
|
|
15
|
+
# countrycodes-cli
|
|
16
|
+
|
|
17
|
+
[](https://github.com/oyghen/countrycodes/tree/main/packages/countrycodes-cli)
|
|
18
|
+
[](https://pypi.org/project/countrycodes-cli/)
|
|
19
|
+
[](https://github.com/oyghen/countrycodes/blob/main/packages/countrycodes-cli/LICENSE)
|
|
20
|
+
[](https://github.com/oyghen/countrycodes/actions/workflows/ci.yml)
|
|
21
|
+
|
|
22
|
+
</div>
|
|
23
|
+
|
|
24
|
+
Command-line interface for the [`countrycodes`](https://pypi.org/project/countrycodes/)
|
|
25
|
+
ISO 3166-1 lookup library.
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
Install as a command-line tool with [uv](https://docs.astral.sh/uv/):
|
|
30
|
+
|
|
31
|
+
```console
|
|
32
|
+
uv tool install countrycodes-cli
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Or install into the active Python environment:
|
|
36
|
+
|
|
37
|
+
```console
|
|
38
|
+
python -m pip install countrycodes-cli
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Requires Python 3.11 or newer. The library dependency is installed automatically.
|
|
42
|
+
|
|
43
|
+
## Usage
|
|
44
|
+
|
|
45
|
+
Look up a country by code or name:
|
|
46
|
+
|
|
47
|
+
```console
|
|
48
|
+
countrycodes lookup US
|
|
49
|
+
countrycodes lookup "United Kingdom"
|
|
50
|
+
countrycodes lookup +1
|
|
51
|
+
countrycodes lookup Untied --limit 3
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Lookups are displayed as a table by default. Choose JSON output or list all
|
|
55
|
+
countries:
|
|
56
|
+
|
|
57
|
+
```console
|
|
58
|
+
countrycodes lookup GBR --format json
|
|
59
|
+
countrycodes list
|
|
60
|
+
countrycodes list --format json
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
The `--limit` option controls the maximum number of matches shown for a lookup
|
|
64
|
+
(default: 5), including countries sharing a dial code. When the text output is
|
|
65
|
+
truncated, the CLI notes that it is showing only the top matches. JSON lookup
|
|
66
|
+
output is a single record for one match or an array for multiple matches. The
|
|
67
|
+
default list is rendered as a table sorted by alpha-2 code, with a row number,
|
|
68
|
+
and JSON list output is an unnumbered array. Text tables show a country's
|
|
69
|
+
common name when available, falling back to its source name. Country records
|
|
70
|
+
include the international dial code when available. Fuzzy text lookup tables
|
|
71
|
+
include a combined word-level and full displayed-name similarity percentage
|
|
72
|
+
for each result. Fuzzy matching uses transposition-aware edit-distance scoring.
|
|
73
|
+
Country name data is retained as provided by its source; inclusion does not
|
|
74
|
+
imply endorsement of any political position.
|
|
75
|
+
|
|
76
|
+
See all options with:
|
|
77
|
+
|
|
78
|
+
```console
|
|
79
|
+
countrycodes --help
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## License
|
|
83
|
+
|
|
84
|
+
The CLI code is licensed under BSD 3-Clause. The separately distributed
|
|
85
|
+
`countrycodes` library includes ISO registry data under LGPL-2.1-or-later and
|
|
86
|
+
continent assignments from GeoNames under CC BY 4.0; installing the CLI
|
|
87
|
+
installs that library as a dependency.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
countrycodes_cli/__init__.py,sha256=NsDXp8qRqlBCiRDNa1zQq-Glh8zKE9NoGnhv2qECO6o,162
|
|
2
|
+
countrycodes_cli/cli.py,sha256=o4WUSXaxxSJMu0YSzeUqsEZPfP9NJwhzrG-6aEHmVN0,5200
|
|
3
|
+
countrycodes_cli-0.1.0.dist-info/licenses/LICENSE,sha256=fYGzxG2QzpqMwC8HUfxysU6lhn05faYpUPcn3bOHmvo,1532
|
|
4
|
+
countrycodes_cli-0.1.0.dist-info/WHEEL,sha256=3NO1oYsF7mANFUWVdyEQLM-zmecczWWEmmF5rEbXrWk,80
|
|
5
|
+
countrycodes_cli-0.1.0.dist-info/entry_points.txt,sha256=frYvIn09JurXf6BFHiuPlHSXzmZXczWDtLGA2bvypH8,59
|
|
6
|
+
countrycodes_cli-0.1.0.dist-info/METADATA,sha256=rlKmCeWFE9NXJisO-fuhUYisTpFgAbGGtnygco8W2Tk,2991
|
|
7
|
+
countrycodes_cli-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026, CountryCodes developers.
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
17
|
+
contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|