Skip to main content

Create interactive shell programs from arbitrary objects using the argparse and cmd modules

Project description

argparse-shell

Create interactive shell programs from arbitrary objects using the argparse and cmd modules.

calculator-example-gif

Usage

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()

Development

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.

Testing

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.

Formatting

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

Linting is implemented using flake8. The configuration for flake8 is located in the section [flake8] of the setup.cfg.

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

argparse_shell-0.1.1.tar.gz (16.3 kB view hashes)

Uploaded Source

Built Distribution

argparse_shell-0.1.1-py3-none-any.whl (19.0 kB view hashes)

Uploaded Python 3

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