File: default_command.py

package info (click to toggle)
python-cloup 3.0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 936 kB
  • sloc: python: 5,371; makefile: 120
file content (42 lines) | stat: -rw-r--r-- 1,015 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
This example:
- requires click-default-group
- was written in response to a StackOverflow question, not because I think it's
  a good idea to have groups with default command; I tend to not recommend them.
"""
import click

import cloup
from click_default_group import DefaultGroup


class GroupWithDefaultCommand(cloup.Group, DefaultGroup):
    # (Optional) Mark the default command with "*". If you don't need this, you
    # can just write `pass`.
    def format_subcommand_name(
        self, ctx: click.Context, name: str, cmd: click.Command
    ) -> str:
        if name == self.default_cmd_name:
            name = name + "*"
        return super().format_subcommand_name(ctx, name, cmd)


@cloup.group(cls=GroupWithDefaultCommand, default='alice')
def cli():
    pass


@cli.command()
@cloup.option("--foo")
def alice(**kwargs):
    print("Called alice with", kwargs)


@cli.command()
@cloup.option("--bar")
def bob(**kwargs):
    print("Called bob with", kwargs)


if __name__ == '__main__':
    cli()