Skip to content

Commit

Permalink
(#24) pokedex#21: "click rewrite"
Browse files Browse the repository at this point in the history
  • Loading branch information
withtwoemms committed Feb 27, 2024
1 parent 267e1b4 commit de54305
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
38 changes: 18 additions & 20 deletions pokedex/api/__main__.py
@@ -1,33 +1,31 @@
import sys
from argparse import ArgumentParser
import click

from pokedex.api.client import get_pokemon_by_move, get_pokemon_by_type
from pokedex.db.client import persist_requests
from pokedex.db.models import Report


def go(args=sys.argv):
parser = ArgumentParser(prog="get-pokemon", description="Catch 'em all")
@click.group()
def cli():
pass

subparsers = parser.add_subparsers(help="search dimension for fetching pokemon")

parser_by = subparsers.add_parser("by")
parser_by.add_argument("--type", type=str, help="e.g. water, grass, fire")
parser_by.add_argument("--move", type=str, help='e.g. "water-gun", "razor-leaf", ember')
@cli.command(no_args_is_help=True)
@click.option("--type", type=click.STRING, default=False, help="e.g. water, grass, fire")
@click.option("--move", type=click.STRING, default=False, help="e.g. water-gun, razor-leaf, ember")
def by(type: str, move: str):
"""
Accepts values by which to fetch Pokemon
any_args_given = len(sys.argv) > 1

if not any_args_given:
parser.print_help(sys.stderr)
sys.exit(1)

args = parser.parse_args()

if args.type:
results = dict(persist_requests(get_pokemon_by_type(args.type)))
TODO: dismiss mutual exclusivity for given options--
i.e. `--type` and `--move` should be usable at the same time
"""
if type != "False":
results = dict(persist_requests(get_pokemon_by_type(type)))
print(Report(persisted=results))
return

if args.move:
move = str(args.move).replace(" ", "-")
if move != "False":
results = dict(persist_requests(get_pokemon_by_move(move)))
print(Report(persisted=results))
return
2 changes: 1 addition & 1 deletion pyproject.toml
Expand Up @@ -23,7 +23,7 @@ tests = "scripts:tests"
coverage = "scripts:coverage"
lint = "scripts:lint"
check = "scripts:check"
get-pokemon = "pokedex.api.__main__:go"
get-pokemon = "pokedex.api.__main__:cli"

[build-system]
requires = ["poetry-core==1.5.0"]
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
@@ -1,2 +1,3 @@
click==8.1.3
poetry==1.5.1
typing_extensions==4.6.2

0 comments on commit de54305

Please sign in to comment.