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

Create a virtual environment using

uv sync

Install the pre-commit hooks using

pre-commit install

Now you have an editable installation, ready to develop.

Testing

After installing all the dependencies, run the test suite using

uv run 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.

Linting and Formatting

The Python code in this repository is linted and formatted using ruff with a line length of 120 characters.

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.2.0.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

argparse_shell-0.2.0-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

File details

Details for the file argparse_shell-0.2.0.tar.gz.

File metadata

  • Download URL: argparse_shell-0.2.0.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.22

File hashes

Hashes for argparse_shell-0.2.0.tar.gz
Algorithm Hash digest
SHA256 04b19e51a14fdfb0041361c845c921a62ebcd75dca3e5644a30c0d059e71fad3
MD5 a16ab5aa1a7b6053eeb26df0a26d470e
BLAKE2b-256 e39b1aac9a90b8de32a4dec5106e9c03ba516ad3cae6d3450e3afe9233fde149

See more details on using hashes here.

File details

Details for the file argparse_shell-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for argparse_shell-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b2bd72ccd18711e6dff57092f4ca5bcd2ff0ed52bc8310d6a50326fa07b60631
MD5 5a59ce15fecd1d35f4470cc809a1b52a
BLAKE2b-256 1445efe6591118dc147fc0e39c0371d601880543cddf87f4a7c28cc81e0305e6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page