pythonreadlinepython-cmd

How to check if completion is available in a python cmd.Cmd program


Standard cmd module provide nice tools to build a CLI with auto completion by default if readline support is available.

However I wonder how can I know from inside the program whether this support is effectively available or not and though whether the completion is enabled or not.


Solution

  • I checked the sources of the cmd module and it imports readline where it need it in a try-except block to catch an ImportError. Then the best way to find if completion is enable is just to use the following:

    try:
       import readline
       completion_available = True
    except ImportError:
       completion_available = False