Skip to main content

A library for creating hierarchical command line arg menus.

Project description

cmdmenu

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.

Installation

Run pip install cmdmenu

Usage

See /examples for full exampes.

Adding commands

Use the cmdmenu.add_command function for adding commands.

import argparse
import cmdmenu

def echo(message):
    print(message)

def mirror_echo(message):
    print(message[::-1])

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

cmdmenu.add_command(subparsers, echo)
cmdmenu.parse_and_run_with(parser)

Run like this.

$ python main.py echo Hello
Hello
$ python main.py mirror_echo Hello
olleH

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"):
    print(message)
$ python main.py --help
usage: An example application [-h] {echo,mirror_echo} ...

positional arguments:
  {echo,mirror_echo}
    echo              Echo to terminal
    mirror_echo       Echo reversed

optional arguments:
  -h, --help          show this help message and exit
$ python main.py 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!")
    else:
        print("Hello, {}".format(name))
$ python main.py hello
Hello, World!
$ python main.py 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}):
    print(sum(numbers))
$ python main.py add_numbers 1 2 3
6

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 my_commands.py, 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)
    cmdmenu.parse_and_run_with(parser)

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:

main.py
fakegit
    __init__.py -> defines add, rm
    remote.py -> defines add rename
    my_addon/
	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 main.py add
python main.py rm
python main.py remote add
python main.py remote rename
python main.py my_addon features foo
python main.py my_addon features bar
python main.py 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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file cmdmenu-0.1.1.tar.gz.

File metadata

  • Download URL: cmdmenu-0.1.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.6

File hashes

Hashes for cmdmenu-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2447405c39cf9d633ea1fd2bab76235d286feeae5895ea6eea70042d4b84161d
MD5 0282d92f9edf94d77ef180bf89313e2a
BLAKE2b-256 5c3ae9ac97e10bd45160e3bb2d1833c5cdd6c2d7d201b25c23ebb97f1bb56157

See more details on using hashes here.

File details

Details for the file cmdmenu-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: cmdmenu-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.6

File hashes

Hashes for cmdmenu-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d0a5aeaf4a5a734f4c2d6fe816e8fe021193d4dc67fa748f4aad277819852636
MD5 b42db1a9d6dcfd6977a2c29159cdd398
BLAKE2b-256 c70c5565f45a75183c6536526ca9e0a99bb994aa3369d6120fdd7b69ca705583

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