diff --git a/bottery/cli.py b/bottery/cli.py index 64d5a8c..fc745d5 100644 --- a/bottery/cli.py +++ b/bottery/cli.py @@ -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') diff --git a/tests/test_cli.py b/tests/test_cli.py index fb91bef..1d663f0 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -6,6 +6,7 @@ import click from click.testing import CliRunner +import bottery from bottery.cli import cli, debug_option, run @@ -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