diff options
author | Armin Ronacher <armin.ronacher@active-4.com> | 2014-05-10 02:46:08 +0200 |
---|---|---|
committer | Armin Ronacher <armin.ronacher@active-4.com> | 2014-05-10 02:46:08 +0200 |
commit | ae52b1aa6e3c7e76b94a1027c3b1d3ad0fa2ad18 (patch) | |
tree | 0d231ada4f3b6a79e4f9debf18642a0c22544b4e /examples/naval | |
parent | a6125e11d164356dae997517fa5c99c0800b7c7c (diff) | |
download | click-ae52b1aa6e3c7e76b94a1027c3b1d3ad0fa2ad18.tar.gz |
Ported naval example from docopt and expanded it
Diffstat (limited to 'examples/naval')
-rw-r--r-- | examples/naval/naval.py | 70 | ||||
-rw-r--r-- | examples/naval/setup.py | 15 |
2 files changed, 85 insertions, 0 deletions
diff --git a/examples/naval/naval.py b/examples/naval/naval.py new file mode 100644 index 0000000..2ce0d80 --- /dev/null +++ b/examples/naval/naval.py @@ -0,0 +1,70 @@ +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.""" + + +@ship.command('new') +@click.argument('name') +def ship_new(name): + """Creates a new ship.""" + click.echo('Created ship %s' % name) + + +@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.') +def ship_move(ship, x, y, speed): + """Moves SHIP to the new location X,Y.""" + click.echo('Moving ship %s to %s,%s with speed %s' % (ship, x, y, speed)) + + +@ship.command('shoot') +@click.argument('ship') +@click.argument('x', type=float) +@click.argument('y', type=float) +def ship_shoot(ship, x, y): + """Makes SHIP fire to X,Y.""" + click.echo('Ship %s fires to %s,%s' % (ship, x, y)) + + +@cli.group('mine') +def mine(ship): + """Manages mines.""" + + +@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.') +def mine_set(x, y, ty): + """Sets a mine at a specific coordinate.""" + click.echo('Set %s mine at %s,%s' % (ty, x, y)) + + +@mine.command('remove') +@click.argument('x', type=float) +@click.argument('y', type=float) +def mine_remove(x, y): + """Removes a mine at a specific coordinate.""" + click.echo('Removed mine at %s,%s' % (x, y)) diff --git a/examples/naval/setup.py b/examples/naval/setup.py new file mode 100644 index 0000000..048fc67 --- /dev/null +++ b/examples/naval/setup.py @@ -0,0 +1,15 @@ +from setuptools import setup + +setup( + name='naval', + version='2.0', + py_modules=['naval'], + include_package_data=True, + install_requires=[ + 'Click', + ], + entry_points=''' + [console_scripts] + naval=naval:cli + ''', +) |