pyclilib
Python Library to create CLI tools using Subject-Verb-Object pattern
# [my-namespace] not yet implemented
my-cmd [my-namespace] my-action my-resource [pos_args] [--options]
Quickstart 1: CLI without Namespaces
pip install pyclilib
- Create a hello world CLI
import clilib
@clilib.decorator.resource
class MyResource():
@clilib.decorator.verb
def get(self):
print("Hello world")
clilib.init("mycli")
clilib.run("mycli")
- Run your program
$ python example.py get my-resource
Hello world
Quickstart 2: CLI with args
pip install pyclilib
- Create a hello world CLI with args.
import clilib
# Under the hood, clilib uses argparse.
# the @arg decorator uses its "add_argument" method
@clilib.decorator.resource
@clilib.decorator.arg("--environment", "-e", type=str, default="dev")
class MyResource():
@clilib.decorator.verb
@clilib.decorator.arg("name", type=str)
def get(self, args):
print(f"Get: {args.name}, {args.environment}")
@clilib.decorator.verb
def list(self, args):
print(f"List: {args.environment}")
clilib.init("mycli")
clilib.run("mycli")
- Run your program
$ python example.py get my-resource Foo -e stage
Get: Foo, stage
$ python example.py list my-resource
List: dev
Quickstart 3: CLI with Namespaces (not implemented yet)
pip install clilib
- Create the runner,
example.py
import clilib
clilib.init("mycli")
clilib.run("mycli")
- Namespaces are sub-packages of an expected local
namespace
package
$ mkdir -p namespace namespace/my-namespace
$ touch namespace/__init__.py namespace/my-namespace/__init__.py
# namespace/my-namespace/__init__.py
import clilib
@clilib.decorator.resource
class MyResource():
@clilib.decorator.verb
def get(self):
print("Hello World")
- Run your program
$ python example.py my-namespace get my-resource
Hello World
Notes
- You can either have
cli namespace action resource
ORcli action resource
in the future
Roadmap
- Global Config -- Potential
- Namespace
- Conditional Args
-
CLI without resource ex.
git init
TODO
- How to make log level configurable?
Since decorators run during "compile"(?) time, setting the level through
clilib.run("foo", logging.DEBUG)
will set the level after the decorators do their thing. Same story withclilib._log_level = logging.DEBUG