Changeset - acea8f3f6a6f
[Not reviewed]
feature-cli
0 1 0
Dennis Fink - 9 years ago 2015-10-24 01:25:08
dennis.fink@c3l.lu
Dynamically add subcommands
1 file changed with 8 insertions and 3 deletions:
0 comments (0 inline, 0 general)
ennstatus/cli/__init__.py
Show inline comments
 
import pathlib
 
import importlib
 
import operator
 

	
 
import click
 

	
 
from .commands import __all__ as commands_all
 

	
 

	
 
@click.group()
 
@click.option('-p', '--path', default='/srv/http/enn.lu',
 
              help='Path where the config files are',
 
              type=click.Path(exists=True,
 
                              file_okay=False,
 
                              writable=True,
 
                              readable=True)
 
              )
 
@click.pass_context
 
def cli(ctx, path):
 
    ctx.obj = {}
 
    path = pathlib.Path(path)
 
    ctx.obj['path'] = path
 
    ctx.obj['config_file'] = path / 'config.json'
 
    ctx.obj['data_dir'] = path / 'data'
 

	
 

	
 
from .commands import config
 
cli.add_command(config, 'config')
 
subcommands = importlib.import_module('ennstatus.cli.commands')
 
for command in commands_all:
 
    get = operator.attrgetter(command)
 
    cli.add_command(get(subcommands), command)
 

	
 
if __name__ == '__main__':
 
    cli()
0 comments (0 inline, 0 general)