Skip to main content

The library for easily writing feature-rich Python scripts

Project description

Arrrgs PyPI version Quality assurance

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

Custom command absence handler

Use the root_command decorator to set up a no-command handler. The same rules apply to this function as to normal command handlers except that it cannot have its own arguments.

from arrrgs import run, root_command

@root_command()
def print_hello():
    """Prints hello message to current user"""
    print("Hello, user")

if __name__ == "__main__":
    run()

Arguments

To add arguments for command you need to pass their description to the decorator arguments. If you need global arguments, pass them to global_args function. The available parameters of arg are the same as for add_argument in argparse.

from arrrgs import command, arg, run, global_args

global_args(
    arg("--rage", "-r", action='store_true', help="Rage mod")
)

@command(
    arg("name", help="User name")
)
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")

Custom command name

A situation may arise where you have to name a command after a built-in function or type, e.g. list. To specify a command name other than the function name, use the name parameter.

@command(name="list")
def list_numbers():
    """Prints list of numbers"""
    print("1, 2, 3")

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-2.0.0.tar.gz (24.6 kB view details)

Uploaded Source

Built Distribution

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

arrrgs-2.0.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file arrrgs-2.0.0.tar.gz.

File metadata

  • Download URL: arrrgs-2.0.0.tar.gz
  • Upload date:
  • Size: 24.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.6

File hashes

Hashes for arrrgs-2.0.0.tar.gz
Algorithm Hash digest
SHA256 ad7da8cb89d8da5949b5099ad0178697208739ea257e95381e6abfa4edfb10c7
MD5 1691f60df7c2debc9041d5b78bc6b480
BLAKE2b-256 98af5568521bcbf81fe508e8585107b9e7ac8cbe025e7ea7782a1de25fd9a0f7

See more details on using hashes here.

File details

Details for the file arrrgs-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: arrrgs-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.6

File hashes

Hashes for arrrgs-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f233877a29a20b08488f4e9f3449e0b1a11599345bb348812c68fc1819f2720c
MD5 bf4bc0bc0c3f7e343a2eb580b2ece305
BLAKE2b-256 534cf82f3f5be7108b7fdbd04d4211c66bbce2bccfd007b0cb2cc813a5309d25

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