| 1 |
import click |
| 2 |
|
| 3 |
|
| 4 |
@click.group() |
| 5 |
@click.version_option() |
| 6 |
def cli(): |
| 7 |
"""Naval Fate. |
| 8 |
|
| 9 |
This is the docopt example adopted to Click but with some actual |
| 10 |
commands implemented and not just the empty parsing which really |
| 11 |
is not all that interesting. |
| 12 |
""" |
| 13 |
|
| 14 |
|
| 15 |
@cli.group() |
| 16 |
def ship(): |
| 17 |
"""Manages ships.""" |
| 18 |
|
| 19 |
|
| 20 |
@ship.command("new") |
| 21 |
@click.argument("name") |
| 22 |
def ship_new(name): |
| 23 |
"""Creates a new ship.""" |
| 24 |
click.echo(f"Created ship {name}") |
| 25 |
|
| 26 |
|
| 27 |
@ship.command("move") |
| 28 |
@click.argument("ship") |
| 29 |
@click.argument("x", type=float) |
| 30 |
@click.argument("y", type=float) |
| 31 |
@click.option("--speed", metavar="KN", default=10, help="Speed in knots.") |
| 32 |
def ship_move(ship, x, y, speed): |
| 33 |
"""Moves SHIP to the new location X,Y.""" |
| 34 |
click.echo(f"Moving ship {ship} to {x},{y} with speed {speed}") |
| 35 |
|
| 36 |
|
| 37 |
@ship.command("shoot") |
| 38 |
@click.argument("ship") |
| 39 |
@click.argument("x", type=float) |
| 40 |
@click.argument("y", type=float) |
| 41 |
def ship_shoot(ship, x, y): |
| 42 |
"""Makes SHIP fire to X,Y.""" |
| 43 |
click.echo(f"Ship {ship} fires to {x},{y}") |
| 44 |
|
| 45 |
|
| 46 |
@cli.group("mine") |
| 47 |
def mine(): |
| 48 |
"""Manages mines.""" |
| 49 |
|
| 50 |
|
| 51 |
@mine.command("set") |
| 52 |
@click.argument("x", type=float) |
| 53 |
@click.argument("y", type=float) |
| 54 |
@click.option( |
| 55 |
"ty", |
| 56 |
"--moored", |
| 57 |
flag_value="moored", |
| 58 |
default=True, |
| 59 |
help="Moored (anchored) mine. Default.", |
| 60 |
) |
| 61 |
@click.option("ty", "--drifting", flag_value="drifting", help="Drifting mine.") |
| 62 |
def mine_set(x, y, ty): |
| 63 |
"""Sets a mine at a specific coordinate.""" |
| 64 |
click.echo(f"Set {ty} mine at {x},{y}") |
| 65 |
|
| 66 |
|
| 67 |
@mine.command("remove") |
| 68 |
@click.argument("x", type=float) |
| 69 |
@click.argument("y", type=float) |
| 70 |
def mine_remove(x, y): |
| 71 |
"""Removes a mine at a specific coordinate.""" |
| 72 |
click.echo(f"Removed mine at {x},{y}") |