Skip to content

Asyncio-based automatic CLI creation tool using prompt-toolkit

License

Notifications You must be signed in to change notification settings

KimiNewt/aiocmd

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

aiocmd

Coroutine-based CLI generator using prompt_toolkit, similarly to the built-in cmd module.

How to install?

Simply use pip3 install aiocmd

How to use?

To use, inherit from the PromptToolkitCmd class and implement the do_<action> for each command.

Each command can receive arguments and optional (keyword) arguments. You then must run the run() coroutine to start the CLI.

For instance:

import asyncio

from aiocmd import aiocmd


class MyCLI(aiocmd.PromptToolkitCmd):

    def do_my_action(self):
        """This will appear in help text"""
        print("You ran my action!")
        
    def do_add(self, x, y):
        print(int(x) + int(y))

    async def do_sleep(self, sleep_time=1):
        await asyncio.sleep(int(sleep_time))
        
        
if __name__ == "__main__":
    asyncio.get_event_loop().run_until_complete(MyCLI().run())

Will create this CLI:

CLIImage

Extra features

You can implement a custom completion for each command by implementing _<action>_completions.

For example, to complete a single-digit number for the add action:

class MyCLI(aiocmd.PromptToolkitCmd):
    
    def _add_completions(self):
        return WordCompleter([str(i) for i in range(9)])

CLIImage

You can also set a custom prompt and aliases parameters for the class (example in docs).

About

Asyncio-based automatic CLI creation tool using prompt-toolkit

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages