Skip to main content

A library for creating hierarchical command line arg menus.

Project description


cmdmenu is a simple library based on argparse for automatically creating command line interfaces consisting of levels of hierarchy (like git) and linking them to functions.


Run pip install cmdmenu


See /examples for full exampes.

Adding commands

Use the cmdmenu.add_command function for adding commands.

import argparse
import cmdmenu

def echo(message):

def mirror_echo(message):

parser = argparse.ArgumentParser("An example application")
subparsers = parser.add_subparsers()

cmdmenu.add_command(subparsers, echo)

Run like this.

$ python echo Hello
$ python mirror_echo Hello

You can add a help message and a description to your commands using the cmdmenu_function decorator. If you only specify one parameter, it will be used for both, otherwise, the first one is the short help message, the second one is the description.

You can also add a string annotation to function parameters to generate help message

@cmdmenu.cmdmenu_function("Echo to terminal", "Longer description of echo")
def echo(message: "Message to echo"):
$ python --help
usage: An example application [-h] {echo,mirror_echo} ...

positional arguments:
    echo              Echo to terminal
    mirror_echo       Echo reversed

optional arguments:
  -h, --help          show this help message and exit
$ python echo --help
usage: An example application echo [-h] message

Longer description of echo

positional arguments:
  message     Message to echo

optional arguments:
  -h, --help  show this help message and exit

Default values

cmdmenu respects parameter default values. If a parameter has a default value, a flag will be created for it. You can override the flag name using a dictionary annotation (see below) with the name parameter.

@cmdmenu.cmdmenu_function("Print a hello world message")
def hello(name=None):
    if name is None:
        print("Hello, World!")
        print("Hello, {}".format(name))
$ python hello
Hello, World!
$ python hello --name Ali
Hello, Ali!

More annotations

You can pass a dictionary as parameter annotation, the arguments are then passed to argparse. See add_command docstring for more details.

@cmdmenu.cmdmenu_function("Print sum of given numbers")
def add_numbers(numbers: {"help": "Numbers to sum up",
                  "nargs": "+", "type":int}):
$ python add_numbers 1 2 3

Adding modules

You can all functions marked by the cmdmenu_function in a module (and its submodules) using the add_module function.

You can save the functions from previous example in, and run the following

import argparse
import cmdmenu

import my_commands

if __name__ == "__main__":
    parser = argparse.ArgumentParser("An example application")
    subparsers = parser.add_subparsers()

    cmdmenu.add_module(subparsers, my_commands, toplevel=True)

This will create an identical program. The toplevel=True parameter indicates that the functions should be added directly without creating an editional level.

By default add_module adds all the submodules of given module, which contain a variable called CMDMENU_META.

Take the following structure as an example:
fakegit -> defines add, rm -> defines add rename
	features -> defines foo, bar
	items -> defines baz

By adding fakegit as a toplevel, you get the following commands, each with positional and keyword arguments as defined by the functions:

python add
python rm
python remote add
python remote rename
python my_addon features foo
python my_addon features bar
python items baz

Submodule menu behaviour and documentation can be configured with a CMDMENU_META dictionary. See add_module docstring for details.

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

cmdmenu-0.1.1.tar.gz (5.1 kB view hashes)

Uploaded source

Built Distribution

cmdmenu-0.1.1-py3-none-any.whl (5.1 kB view hashes)

Uploaded py3

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