Skip to content

Commit

Permalink
Merge pull request #110 from rougeth/cli-version
Browse files Browse the repository at this point in the history
Create CLI flag to show Bottery version
  • Loading branch information
rougeth authored Oct 21, 2017
2 parents 1721fd7 + 6a0f334 commit 1fa3644
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
17 changes: 15 additions & 2 deletions bottery/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,23 @@ def wrapper(*args, **kwargs):
return wrapper


@click.group()
def cli():
@click.group(invoke_without_command=True)
@click.option('--version', '-v', is_flag=True, default=False)
@click.pass_context
def cli(ctx, version):
"""Bottery"""

# If no subcommand was given and the version flag is true, shows
# Bottery version
if not ctx.invoked_subcommand and version:
click.echo(bottery.__version__)
ctx.exit()

# If no subcommand but neither the version flag, shows help message
elif not ctx.invoked_subcommand:
click.echo(ctx.get_help())
ctx.exit()


@cli.command('startproject')
@click.argument('name')
Expand Down
22 changes: 22 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import click
from click.testing import CliRunner

import bottery
from bottery.cli import cli, debug_option, run


Expand Down Expand Up @@ -78,3 +79,24 @@ def test_startproject_invalid_project_name():
result = runner.invoke(cli, ['startproject', project_name])

assert result.exit_code == 2


def test_version_option():
runner = CliRunner()
result = runner.invoke(cli, ['--version'])

assert bottery.__version__ in result.output
assert result.exit_code == 0


def test_no_options_shows_help_message():
"""
Test if CLI shows the help message when no option or command is
given.
"""
runner = CliRunner()
result = runner.invoke(cli)
ctx = click.Context(cli, info_name='cli')

assert ctx.get_help() == result.output.strip()
assert result.exit_code == 0

0 comments on commit 1fa3644

Please sign in to comment.