Skip to main content

unfrustrating python CLI

Project description

cli2: unfrustrating python CLI
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Sometimes I just want to execute a python callback and pass args/kwargs on the
CLI, and not have to define any custom CLI entry point of any sort, nor change
any code, typically when automating stuff, cli2 unfrustrates me::

cli2 yourmodule.yourcallback somearg somekwarg=foo

Sometimes I just want to define a new command and expose all callables in a
module and I can't just do it with a one-liner. cli2 unfrustrates me::

console_script = cli2.ConsoleScript(__doc__).add_module('mymodule')
# then i add console_script entrypoint as such: mycmd = mycmd.console_script

I also like when readonly commands are in green, writing commands in yellow and
destructive commands in red, I find the commands list in the help output more
readable, and directive for new users of the CLI::

@cli2.config(color=cli2.RED)
def challenge(dir):
'''The challenge command dares you to run it.'''
os.exec('rm -rf ' + dir)

Of course then there's all this code I need to have coverage for and I'm
`still
<https://pypi-hypernode.com/project/django-dbdiff/>`_ so lazy that I still
`don't write most of my test code myself
<https://pypi-hypernode.com/project/django-responsediff/>`_, so I throwed an autotest
function in cli2 that I can use as such::

@pytest.mark.parametrize('name,command', [
('cli2', ''),
('help', 'help'),
('help_debug', 'help debug'),
# ... bunch of other commands
('debug', 'debug cli2.run to see=how -it --parses=me'),
])
def test_cli2(name, command):
cli2.autotest(
f'tests/{name}.txt',
'cli2 ' + command,
)

This got me to 86% coverage in 3 minutes. How so ?

Well, it will execute the command and compare with the contents in the test
text file with GNU or busybox diff and fail if there's a diff. If the file
doesn't exist then it will create it and fail because it created the fixture. I
can just rm -rf my tests dir and run pytest again, and use git diff to see what
has changed.

You should be able tho pip install cli2 and start using the cli2 command, or
cli2.ConsoleScript to make your own commands.

.. raw:: html

<a href="https://asciinema.org/a/221136" target="_blank"><img src="https://asciinema.org/a/221136.svg" /></a>

Projects using cli2 so far:

- `djcli
<https://yourlabs.io/oss/djcli>`_,

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

cli2-0.1.1.dev6.tar.gz (7.2 kB view details)

Uploaded Source

File details

Details for the file cli2-0.1.1.dev6.tar.gz.

File metadata

  • Download URL: cli2-0.1.1.dev6.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.6.6

File hashes

Hashes for cli2-0.1.1.dev6.tar.gz
Algorithm Hash digest
SHA256 176acd8b4606d50269fd961b6088bc2e7dc5ae2d00490f9cff6f584fae1e7602
MD5 ecd501addffc5b23cf94e325c5782375
BLAKE2b-256 999f6bbbb24be55e02692e00d9df46875dbec2a650e114c707832383dbaed7f6

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