Giter Club home page Giter Club logo

python-nutcli's Introduction

nutcli - cli in a nutshell

The Python nutcli package allows you to easily create robust command-based command line interface that can combine the best of the two worlds of python and shell.

It also provides several useful classes and decorators that can help you improve the user experience by producing nicely formatted output and allowing dry runs for operations with side effects.

Links

Installation

pip3 install nutcli

Example instead of thousands words

Bellow is the very basic example to get the idea of what this package does and how its used.

class HelloActor(Actor):
    def setup_parser(self, parser):
        parser.add_argument('--hello', action='store', type=str)

    def __call__(self, hello):
        self.info(hello)
        return 0

class Program:
    def main(self, argv):
        # Create argument parser.
        parser = argparse.ArgumentParser()

        CommandParser('Example Commands')([
            Command('hello', 'Print hello message', HelloActor()),
        ]).setup_parser(parser)

        # Create the runner object.
        runner = Runner('my-cli', parser).setup_parser()

        # Parse arguments - the runner internally process its own arguments
        # that were setup by previous call to `setup_parser()`.
        args = runner.parse_args(argv)

        # You can handle your own global arguments here.

        # Now we setup the default logger - it produces output to stdout
        # and stderr.
        runner.default_logger()

        # And finally, we execute the requested command and return its exit
        # code.
        return runner.execute(args)


if __name__ == "__main__":
    program = Program()
    sys.exit(program.main(sys.argv[1:]))
$ python3 ./main.py hello --hello world
[my-cli] world
$ python3 ./main.py --help
usage: main.py [-h] [--log-execution] [--dry-run] [--no-colors] COMMANDS ...

optional arguments:
-h, --help       show this help message and exit
--log-execution  Log execution of operations that supports it.
--dry-run        Do not execute operations with side effects. Only log what
                    would be done.
--no-colors      Do not execute operations with side effects. Only log what
                    would be done.

Example Commands:
COMMANDS
    hello          Print hello message

python-nutcli's People

Contributors

pbrezina avatar

Stargazers

 avatar

Watchers

 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.