File: example_group.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 (108 lines) | stat: -rw-r--r-- 3,539 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# flake8: noqa E128
import cloup
from cloup import Section, argument, option, option_group


def make_example_group(align_sections):
    def f(**kwargs):
        print(**kwargs)

    git_clone = cloup.command(
        'clone', help='Clone a repository into a new directory')(f)
    git_hidden1 = cloup.command(
        'hidden1', hidden=True)(f)
    git_init = cloup.command(
        'init', help='Create an empty Git repository or reinitialize an existing one')(f)

    git_rm = cloup.command(
        'rm', help='Remove files from the working tree and from the index')(f)
    git_sparse_checkout = cloup.command(
        'sparse-checkout', help='Initialize and modify the sparse-checkout')(f)
    git_mv = cloup.command(
        'mv', help='Move or rename a file, a directory, or a symlink')(f)

    @cloup.group(
        'git',
        align_sections=align_sections,
        align_option_groups=align_sections,
        context_settings={'terminal_width': 80},
    )
    @option_group(
        "Useful options",
        option("-C", type=cloup.Path(), help="Configuration file."),
        option("-p", "--paginate", is_flag=True, help="Paginate output."),
    )
    def git():
        return 0

    # We'll add commands/sections in all possible ways
    first_section = git.section(
        'Start a working area (see also: git help tutorial)', git_init, git_hidden1)
    first_section.add_command(git_clone)

    git.add_section(Section(
        'Work on the current change (see also: git help everyday)',
        [git_rm, git_sparse_checkout, git_mv],
        is_sorted=True
    ))

    git.add_command(cloup.command('fake-3', hidden=True)(f))
    git.add_command(cloup.command('fake-2', help='Fake command #2')(f))
    git.add_command(cloup.command('fake-1', help='Fake command #1')(f))

    git.expected_help = (EXPECTED_ALIGNED_HELP if align_sections
                         else EXPECTED_NON_ALIGNED_HELP)
    return git


EXPECTED_ALIGNED_HELP = """
Usage: git [OPTIONS] COMMAND [ARGS]...

Useful options:
  -C PATH         Configuration file.
  -p, --paginate  Paginate output.

Other options:
  --help          Show this message and exit.

Start a working area (see also: git help tutorial):
  init             Create an empty Git repository or reinitialize an existing...
  clone            Clone a repository into a new directory

Work on the current change (see also: git help everyday):
  mv               Move or rename a file, a directory, or a symlink
  rm               Remove files from the working tree and from the index
  sparse-checkout  Initialize and modify the sparse-checkout

Other commands:
  fake-1           Fake command #1
  fake-2           Fake command #2
""".strip()

EXPECTED_NON_ALIGNED_HELP = """
Usage: git [OPTIONS] COMMAND [ARGS]...

Useful options:
  -C PATH         Configuration file.
  -p, --paginate  Paginate output.

Other options:
  --help  Show this message and exit.

Start a working area (see also: git help tutorial):
  init   Create an empty Git repository or reinitialize an existing one
  clone  Clone a repository into a new directory

Work on the current change (see also: git help everyday):
  mv               Move or rename a file, a directory, or a symlink
  rm               Remove files from the working tree and from the index
  sparse-checkout  Initialize and modify the sparse-checkout

Other commands:
  fake-1  Fake command #1
  fake-2  Fake command #2
""".strip()


if __name__ == '__main__':
    make_example_group(align_sections=False)(['--help'], prog_name='git')