Skip to main content

Cheap CLI framework, gives rich commands for Django

Project description

# clilabs: the python CLI framework for getting things done

```
$ pip install clilabs
```

Try with your own command first, put this in example.py:

```
def main(*args, **kwargs):
print(f'Args: {args}')
print(f'Kwargs: {kwargs}')
```

Note that you can also use the example.py at the root of the repo for testing
purposes.

You invoke your own functions by defining the main function in a python module
that takes args and kwargs:

```
$ clilabs example.main somearg somekwarg=1
```

Note that you can either omit `:main` that is the default,
either target a callable:

```
$ clilabs example:SomeClass.some_attr.somestaticmethod
```

If an argument starts with `-`, it will be available with in the context object
you can import from clilabs. Change example.py to the following:

```
def main(*args, **kwargs):
print(f'Args: {args}')
print(f'Kwargs: {kwargs}')

from clilabs import context
print(f'Context args: {context.args}')
print(f'Context kwagrs: {context.kwargs}')
```

Now try:

```
$ clilabs your.module --noinput -c=2
```

The tradeoff is that you cannot use space to delimit key from value because
that would confuse the lexer which wouldn't be able to distinguish value from
un-named argument:

```
# results in context.args == ['c'] and args == ['somearg']
# NOT in context['c'] == 'somearg'
$ clilabs your.module -c somearg
```

When it finds only a `-` alone then it will read stdin for a value:

```
$ echo bar | clilabs example -
```

If you want to get the help of a command, sorry but this framework is too
stupid. However, you can try to read its docstring with the builtin
introspection command:

```
# docstring of function
$ clilabs clilabs.inspect:doc your.mod:main

# docstring of module
$ clilabs clilabs.inspect:doc clilabs.django
```

Pycli also bundles a nice bunch of functions for Django, for now it has a
django submodule that defines a bunch of CLI functions:

```
# find the content for this variable in your project manage.py
# because automated software should be available automatically
# changing INSTALLED_APPS is considered a manual operation
$ export DJANGO_SETTINGS_MODULE=your.settings

# you will probably run this from your directory parent to manage.py:
$ clilabs clilabs.django:create auth.user username=pony is_superuser=1

# i just checked and chpasswd command in django supports only interactive
$ clilabs clilabs.django:chpasswd /path/to/password username=pony

# supports stdin too with -
$ echo yourpassword | clilabs.django:chpasswd - username=pony

# find stuff:
$ clilabs clilabs.django:list auth.user username is_superuser
| pk | username | is_superuser |
| 1 | pony | True |
| 2 | ninja | False |

# and filter
$ clilabs clilabs.django:list auth.user username is_superuser=true
| pk | username | is_superuser |
| 1 | ninja | True |

# count supports filters too
$ clilabs clilabs.django:count is_superuser=true
1

# delete stuff, would be nicer with --noinput
$ clilabs clilabs.django:delete auth.user username=pony --noinput
1

# you can use other models
$ clilabs clilabs.django:create sites.site name='awesome site' domain=awesome.com
$ clilabs clilabs.django:list admin.logentry
$ clilabs clilabs.django:detail auth.groups pk=1

# and of course your own apps:
$ clilabs yourapp.cli:dosomething with_that_arg with_that_kwarg=1 --noinput --othercontextarg=1
```

## Why not use a management command ?

**Because automation software should be automatically made available in an
automated way.**

That means, having to add it to INSTALLED_APPS is: no because it's a manual
operation.

## Why is it not available as management command ?

Because we need to break out of the framework's inversion of control to make
such a rich user experience from the command line: with dynamic options.

## Why not use click ?

I really wonder how to use click to work out with this weird cli.

## Do you have crazy ideas for new django functions ?

Always, why not being able to run a forms on the command line given a dotted
import path, both in interactive, and non interactive mode ?

Or, why not provision a model from a file tree of YAML files to replace
loaddata ?

Last time I needed those features, I passed, but it's over now.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

clilabs-0.1.0.tar.gz (4.5 kB view details)

Uploaded Source

File details

Details for the file clilabs-0.1.0.tar.gz.

File metadata

  • Download URL: clilabs-0.1.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.6

File hashes

Hashes for clilabs-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2a283b585bf9d90f98c5a6c6f12efdf7c7a5502333310bed16d687621a9e0d97
MD5 0e6e1ff34448f88f5962ff15d8d87775
BLAKE2b-256 08aeb8b37ec01fc029bf8eaa905b91a769599d6b95c64c3912778eca10e930e7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page