80 lines
2.5 KiB
Python
Executable file
80 lines
2.5 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
"""Example MILC program that shows off many features.
|
|
|
|
PYTHON_ARGCOMPLETE_OK
|
|
"""
|
|
import os
|
|
|
|
from milc import set_metadata
|
|
|
|
set_metadata(name='example', author='Milc Milcenson', version='1.6.6')
|
|
|
|
from milc import cli
|
|
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()
|