Skip to content

Commit

Permalink
Create CLI version flag
Browse files Browse the repository at this point in the history
  • Loading branch information
rougeth committed Oct 21, 2017
1 parent 1721fd7 commit 7a64307
Show file tree
Hide file tree
Showing 2 changed files with 39 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
24 changes: 24 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,26 @@ 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')

# Couldn't use == to compare command output with helper message
# because of a \n at the end :/
assert ctx.get_help() in result.output
assert result.exit_code == 0

0 comments on commit 7a64307

Please sign in to comment.