Skip to main content

The library for easily writing feature-rich Python scripts

Project description

Arrrgs

Logo

The library for easily writing feature-rich Python scripts. Uses the built-in argparse module for parsing.

  • Simple API
  • Automatic async support
  • Small size

Installing

pip install arrrgs

Usage

Basic

To declare a command, write a function and add the command decorator to it. To start command processing, call the function run.

from arrrgs import command, run
from os import getlogin

@command()
def hello():
    """Prints hello message to current user"""
    print(f"Hello, {getlogin()}")

if __name__ == "__main__":
    run()

Arrrgs will process the command and show the user the result. The help message will be generated from the function documentation.

python examples/basic.py hello --help
# usage: basic.py hello [-h]

# Prints hello message to current user

# optional arguments:
#   -h, --help  show this help message and exit

Arguments

To add arguments you need to pass their description to the decorator arguments. The available parameters are the same as for add_argument in argparse.

from arrrgs import command, arg, run

@command(
    arg("name", help="User name"),
    arg("--rage", "-r", action='store_true', help="Rage mod"),
)
def hello(args):
    """Prints hello message to current user"""
    user_name = args.name
    if args.rage:
        user_name = user_name.upper()
    print(f"Hello, {user_name}")

if __name__ == "__main__":
    run()

Context

Sometimes all the teams in an application need a common entity that they interact with. Commands have a context for that. The context value is set when the function run is called.

from arrrgs import command, run

class User:
    def __init__(self, name):
        self._name = name

    def get_name(self):
        """Returns user name"""
        return self._name

@command()
def hello(_, context):
    """Prints hello message to current user"""
    print(f"Hello, {context.get_name()}")

if __name__ == "__main__":
    user = User("Mikhael")
    run(user)

Async

To execute the command in an asynchronous context, simply add the async keyword in the function declaration.

@command()
async def hello():
    """Prints hello message to current user"""
    print("Hello, async user")

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

arrrgs-0.0.2.tar.gz (3.0 kB view hashes)

Uploaded Source

Built Distribution

arrrgs-0.0.2-py3-none-any.whl (3.5 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