File: example

package info (click to toggle)
python-milc 1.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 788 kB
  • sloc: python: 1,868; sh: 55; makefile: 3
file content (80 lines) | stat: -rwxr-xr-x 2,586 bytes parent folder | download
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
#!/usr/bin/env python3
"""Example MILC program that shows off many features.

PYTHON_ARGCOMPLETE_OK
"""
import os

from milc import cli

cli.milc_options(name='example', author='Milc Milcenson', version='1.9.1')

# This needs to be imported after we use cli.milc_options()
import milc.subcommand.config  # noqa


def EnvironCompleter(**kwargs):
    """Example argument completer.
    """
    return os.environ


@cli.argument('-n', '--name', help='Name to greet', default='World')
@cli.entrypoint('Greet a user.')
def main(cli):
    cli.log.info('No subcommand specified!')
    cli.print_usage()


@cli.argument('-n', '--dashed-name', help='Name to greet', default='World')
@cli.subcommand('Subcommand with dash in the name.')
def dashed_hello(cli):
    cli.echo('Hello, dashed-subcommand %s!', cli.config.dashed_hello.dashed_name)


@cli.argument('--print-usage', action='store_true', help='Show a usage summary')
@cli.argument('--print-help', action='store_true', help='Alias for --help')
@cli.argument('--comma', help='comma in output', action='store_boolean', default=True)
@cli.argument('--count', help='Number of times to say hello', type=int, default=1)
@cli.subcommand('Say hello.')
def hello(cli):
    """Say hello.
    """
    # print_usage and print_help are here to help us test.
    if cli.config.hello.print_usage:
        cli.print_usage()
        return True

    if cli.config.hello.print_help:
        cli.print_help()
        return True

    comma = ',' if cli.config.hello.comma else ''
    for i in range(cli.config.hello.count):
        cli.echo('{fg_green}Hello%s %s!', comma, cli.config.general.name)


@cli.argument('-e', '--env', completer=EnvironCompleter, help='Environment Variable')
@cli.argument('-f', '--flag', help='Write it in a flag', action='store_true')
@cli.subcommand('Say goodbye.')
def goodbye(cli):
    if cli.config.goodbye.flag:
        cli.log.debug('Drawing a flag.')
        colors = ('{bg_red}', '{bg_lightred_ex}', '{bg_lightyellow_ex}', '{bg_green}', '{bg_blue}', '{bg_magenta}')
        string = 'Goodbye, %s!' % cli.config.general.name
        for i, letter in enumerate(string):
            color = colors[i % len(colors)]
            cli.echo(color + letter + ' '*39)
    else:
        cli.log.warning('Parting is such sweet sorrow.')
        cli.echo('{fg_blue}Goodbye, %s!', cli.config.general.name)


@cli.subcommand('Show the configured config file and directory.')
def config_file(cli):
    cli.echo(f'cli.config_file={cli.config_file}')
    cli.echo(f'cli.config_dir={cli.config_dir}')


if __name__ == '__main__':
    cli()