Commit b5c85f8b authored by Amir MOHAMMADI's avatar Amir MOHAMMADI

Merge branch 'aliasing' into 'master'

Add prefix aliasing for bob commands

See merge request !81
parents 637581c8 9e2c1fc1
Pipeline #20770 failed with stage
in 92 minutes and 19 seconds
......@@ -261,3 +261,26 @@ class ResourceOption(click.Option):
value = getattr(value, keyword)
return value
class AliasedGroup(click.Group):
''' Class that handles prefix aliasing for commands
Basically just implements get_command that is used by click to choose the
comamnd based on the name.
Example
-------
To enable prefix aliasing of commands for a given group,
just set ``cls=AliasedGroup`` parameter in click.group decorator.
'''
def get_command(self, ctx, cmd_name):
rv = click.Group.get_command(self, ctx, cmd_name)
if rv is not None:
return rv
matches = [x for x in self.list_commands(ctx)
if x.startswith(cmd_name)]
if not matches:
return None
elif len(matches) == 1:
return click.Group.get_command(self, ctx, matches[0])
ctx.fail('Too many matches: %s' % ', '.join(sorted(matches)))
......@@ -2,7 +2,7 @@
"""
from .. import rc
from ..rc_config import _saverc, _rc_to_str, _get_rc_path
from .click_helper import verbosity_option
from .click_helper import verbosity_option, AliasedGroup
import logging
import click
......@@ -11,7 +11,7 @@ import click
logger = logging.getLogger(__name__)
@click.group()
@click.group(cls=AliasedGroup)
@verbosity_option()
def config():
"""The manager for bob's global configuration."""
......
......@@ -3,12 +3,13 @@
import pkg_resources
import click
from click_plugins import with_plugins
from .click_helper import AliasedGroup
from ..log import setup
logger = setup('bob')
@with_plugins(pkg_resources.iter_entry_points('bob.cli'))
@click.group()
@click.group(cls=AliasedGroup)
def main():
"""The main command line interface for bob. Look below for available
commands."""
......
......@@ -2,7 +2,7 @@ import click
from click.testing import CliRunner
from bob.extension.scripts.click_helper import (
verbosity_option, bool_option, list_float_option,
open_file_mode_option, ConfigCommand, ResourceOption)
open_file_mode_option, ConfigCommand, ResourceOption, AliasedGroup)
def test_verbosity_option():
......@@ -145,3 +145,29 @@ def test_commands_with_config_3():
result = runner.invoke(cli, ['basic_config', '-a', 3])
assert result.exit_code == 0, (result.exit_code, result.output)
assert result.output.strip() == '3', result.output
def test_prefix_aliasing():
@click.group(cls=AliasedGroup)
def cli():
pass
@cli.command()
def test():
click.echo("OK")
@cli.command()
def test_aaa():
click.echo("AAA")
runner = CliRunner()
result = runner.invoke(cli, ['te'], catch_exceptions=False)
assert result.exit_code != 0, (result.exit_code, result.output)
result = runner.invoke(cli, ['test'], catch_exceptions=False)
assert result.exit_code == 0, (result.exit_code, result.output)
assert 'OK' in result.output, (result.exit_code, result.output)
result = runner.invoke(cli, ['test_a'], catch_exceptions=False)
assert result.exit_code == 0, (result.exit_code, result.output)
assert 'AAA' in result.output, (result.exit_code, result.output)
......@@ -30,3 +30,4 @@ cpp:identifier NULL
py:class click.core.Command
py:class click.core.Option
py:class click.core.Group
......@@ -76,6 +76,7 @@ Scripts
bob.extension.scripts.click_helper.bool_option
bob.extension.scripts.click_helper.list_float_option
bob.extension.scripts.click_helper.open_file_mode_option
bob.extension.scripts.click_helper.AliasedGroup
Core Functionality
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment