Create interactive shell programs from arbitrary objects using the argparse and cmd modules.
Use the ArgparseShell.from_object
factory method to quickly create an interactive command line interface
for an existing class. Afterwards the application can be run using the ArgparseShell.main
method.
See the following ./examples/calculator.py for a simple example:
#! /usr/bin/env python3
from argparse_shell import ArgparseShell
class Calculator:
"""A simple calculator example"""
def add(self, a: float, b: float) -> float:
"""Add two numbers
:param a: First number
:param b: Second number
:return: Sum of two numbers
"""
return a + b
def div(self, a: float, b: float) -> float:
"""
Divide numbers
:param a: First number
:param b: Second number
:return: Division of two numbers"""
return a / b
def mult(self, a: float, b: float) -> float:
"""Multiply two numbers
:param a: First number
:param b: Second number
:return: Product of two numbers
"""
return a * b
def sub(self, a: float, b: float) -> float:
"""Subtract two numbers
:param a: First number
:type a: float
:param b: Second number
:type b: float
:return: Subtraction of the two numbers
:rtype: float
"""
return a - b
if __name__ == "__main__":
calc = Calculator()
shell = ArgparseShell.from_object(calc, "calc")
shell.main()
Fork the repository from Github
Clone your version of the repository
git clone https://github.com/<your-username>/argparse-shell
Install the dependencies and dev dependencies using
pip install -e .[dev]
Install the pre-commit hooks using
$ pre-commit install
pre-commit installed at .git/hooks/pre-commit
Now you have an editable installation, ready to develop.
After installing all the dependencies, run the test suite using
pytest
The options for pytest are defined in the setup.cfg and include test coverage check.
The coverage currently has a fail-under
limit of 75 percent. This limit might get increased when more tests get added.
The Python code in this repository is formatted using black black with a line length
of 120 characters. The configuration for black is located in the section [tool.black]
section of pyproject.toml.
Linting is implemented using flake8. The configuration for flake8 is located in
the section [flake8]
of the setup.cfg.