Code Monkey home page Code Monkey logo

argparse-shell's Introduction

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.

argparse-shell's People

Contributors

jonasehrlich avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.