python-click/examples/naval/naval.py

73 lines
1.7 KiB
Python
Raw Normal View History

2014-10-16 20:40:34 +02:00
import click
@click.group()
@click.version_option()
def cli():
"""Naval Fate.
This is the docopt example adopted to Click but with some actual
commands implemented and not just the empty parsing which really
is not all that interesting.
"""
@cli.group()
def ship():
"""Manages ships."""
2020-07-21 08:23:42 +02:00
@ship.command("new")
@click.argument("name")
2014-10-16 20:40:34 +02:00
def ship_new(name):
"""Creates a new ship."""
2021-10-10 03:31:57 +02:00
click.echo(f"Created ship {name}")
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
@ship.command("move")
@click.argument("ship")
@click.argument("x", type=float)
@click.argument("y", type=float)
@click.option("--speed", metavar="KN", default=10, help="Speed in knots.")
2014-10-16 20:40:34 +02:00
def ship_move(ship, x, y, speed):
"""Moves SHIP to the new location X,Y."""
2021-10-10 03:31:57 +02:00
click.echo(f"Moving ship {ship} to {x},{y} with speed {speed}")
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
@ship.command("shoot")
@click.argument("ship")
@click.argument("x", type=float)
@click.argument("y", type=float)
2014-10-16 20:40:34 +02:00
def ship_shoot(ship, x, y):
"""Makes SHIP fire to X,Y."""
2021-10-10 03:31:57 +02:00
click.echo(f"Ship {ship} fires to {x},{y}")
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
@cli.group("mine")
2014-10-16 20:40:34 +02:00
def mine():
"""Manages mines."""
2020-07-21 08:23:42 +02:00
@mine.command("set")
@click.argument("x", type=float)
@click.argument("y", type=float)
@click.option(
"ty",
"--moored",
flag_value="moored",
default=True,
help="Moored (anchored) mine. Default.",
)
@click.option("ty", "--drifting", flag_value="drifting", help="Drifting mine.")
2014-10-16 20:40:34 +02:00
def mine_set(x, y, ty):
"""Sets a mine at a specific coordinate."""
2021-10-10 03:31:57 +02:00
click.echo(f"Set {ty} mine at {x},{y}")
2014-10-16 20:40:34 +02:00
2020-07-21 08:23:42 +02:00
@mine.command("remove")
@click.argument("x", type=float)
@click.argument("y", type=float)
2014-10-16 20:40:34 +02:00
def mine_remove(x, y):
"""Removes a mine at a specific coordinate."""
2021-10-10 03:31:57 +02:00
click.echo(f"Removed mine at {x},{y}")