Package: clinner
ClinnerVersion:1.12.3Status:Production/StableAuthor:José Antonio Perdiguero LópezClinner is a library that provides some useful tools to create command line interfaces for your applicationCheckClinner docs.FeaturesCandefine commandsin multiple way:List of shell commands such as["docker build", "docker push"].Python functions.Python async functions.Clinner provides a set ofcommands ready to uselike:Black.Flake8.Isort.Nosetest.Prospector.Pytest.Sphinx.Tox.Hooks forinjecting variablesoradd global argumentsto your script.Quick startInstall this package using pip:pipinstallclinnerCreate a commandfromclinner.commandimportcommand@commanddeffoo(*args,**kwargs):returnTrueCreate a main file:fromclinner.run.mainimportMainif__name__=='__main__':sys.exit(Main().run())CommandsCommands are declared using a decorator to register given functions. Commands are functions with the follow parameters:func: Function that will be called when command would be executed.command_type: Type of the command, could be abashorpythoncommand.args: Parser arguments for this command.parser_opts: Command subparser's keywords, such as description.This decorator allows to be used as a common decorator without arguments, where default type (python) will be used:@commanddeffoobar(*args,**kwargs):passOr specifying the type:@command(command_type=CommandType.PYTHON)deffoobar(*args,**kwargs):passBut also is possible to provide command line arguments, as expected by argparse.ArgumentParser.add_argument:@command(args=((('-f','--foo'),{'help':'Foo argument that does nothing'}),# Command argument(('--bar',),{'action':'store_true','help':'Bar argument stored as True'})),# Another argumentparser_opts={'title':'foobar_command','help':'Help for foobar_command'})# Parser parametersdeffoobar(*args,**kwargs):passAll commands will be registered in a command register that can be accessed throughcommand.register. Each entry in
this register is a dictionary with the fields declared at the beginning of this section.Shell commandExample of runningls -lashell command.@command(command_type=CommandType.SHELL)deflsla(*args,**kwargs):return[shlex.split("ls -la")]Python functionRun a python function.@commanddeffoo(*args,**kwargs):return"foo"Python async functionRun a python async function.@commandasyncdefbar(*args,**kwargs):awaitasyncio.sleep(1)return"bar"MainA main class is defined to ease the creation of command line applications. This class follows the process:Create a parser usingargparse.ArgumentParserfor the application:a) Calling alladd_arguments(parser)methods from all super classes, e.g:clinner.mixins.HealthCheckMixin.b) Addding a subparser for each command with their specific arguments.Parse arguments using the argument parser created previously.Inject variables into environment calling all super classes methods whose name starts withinject_.ExamplesSome Clinner examples.Simple MainExample of a simple main with two defined commandsfooandbar.#!/usr/bin/env pythonimportshleximportsysfromclinner.commandimportcommand,TypeasCommandTypefromclinner.run.mainimportMain@command(command_type=CommandType.SHELL,args=(('-i','--input'),('-o','--output')),parser_opts={'help':'Foo command'})deffoo(*args,**kwargs):"""List of foo commands"""ls_cmd=shlex.split('ls')wc_cmd=shlex.split('wc')wc_cmd+=[kwargs['input'],kwargs['output']]return[ls_cmd,wc_cmd]@command(command_type=CommandType.PYTHON,parser_opts={'help':'Bar command'})defbar(*args,**kwargs):"""Do a bar."""returnTrueif__name__=='__main__':sys.exit(Main().run())Builder MainExample of main module with build utilities such as unit tests, lint, sphinx doc, tox and dist packaging:#!/usr/bin/env pythonimportsysfromclinner.runimportMainclassBuild(Main):commands=('clinner.run.commands.black.black','clinner.run.commands.flake8.flake8','clinner.run.commands.isort.isort','clinner.run.commands.pytest.pytest','clinner.run.commands.sphinx.sphinx','clinner.run.commands.tox.tox',)if__name__=='__main__':sys.exit(Build().run())CheckClinner docsto see more advanced examples.