cement.ext.ext_argcomplete

The Argcomplete Extension provides the necessary hooks to utilize the Argcomplete Library, and perform auto-completion of command line arguments/options/sub-parsers/etc.

Requirements

  • Argcomplete (pip install argcomplete)
  • Argparse

This extension currently only works when using cement.ext.ext_argparse.ArgparseArgumentHandler (default) and cement.ext.ext_argparse.ArgparseController (new in Cement 2.8). It will not work with cement.core.controller.CementBaseController.

Configuration

This extension does not honor any application configuration settings.

Usage

myapp.py

#!/usr/bin/env python

from cement.core.foundation import CementApp
from cement.ext.ext_argparse import ArgparseController, expose


class BaseController(ArgparseController):
    class Meta:
        label = 'base'
        arguments = [
            (['-f', '--foo'], dict(help='base foo option', dest='foo'))
        ]

    @expose(hide=True)
    def default(self):
        print('Inside BaseController.default')

    @expose()
    def command1(self):
        print('Inside BaseController.command1')


class MyApp(CementApp):
    class Meta:
        label = 'myapp'
        extensions = ['argcomplete']
        handlers = [BaseController]

with MyApp() as app:
    app.run()

Note the #! line, which allows us to call our script directly (specifically for this example). The Argcomplete library requires the end-user to modify their environment in order to perform auto-completion. For this example, we are using a non-global option for demonstration purposes only. In the real world you will need to setup Argcomplete for your actual application entry-point name (i.e. myapp if installed as /usr/bin/myapp, etc).

$ eval "$(register-python-argcomplete myapp.py)"

$ ./myapp.py [tab][tab]

--debug                              -h
-o                                   --help
--quiet                              command1
                                     default

See the Argcomplete Documentation on how to properly integrate it’s usage into your application deployment. This extension simply enables Argcomplete to do it’s thing on application startup.