Skip to main content

A lightweight argparse wrapper for building CLI tools.

Project description

Fastarg

Fastarg is a command line argument parser utility that is built on Python's standard argparse library.

Installation

pip3 install fastarg

Getting started

main.py

import fastarg

app = fastarg.Fastarg(description="hello world")

@app.command()
def hello_world(name: str):
    """hello world"""
    print("hello " + name)

if __name__ == "__main__":
    app.run()

Run command:

$ python3 main.py hello_world foo

Show help text:

$ python3 main.py -h

Subcommands

Create arbitrarily large trees of subcommands.

import fastarg

app = fastarg.Fastarg(description="productivity app", prog="todo")
app2 = fastarg.Fastarg(description="to do", help="manage todos")
app3 = fastarg.Fastarg(description="user", help="manage users")
app4 = fastarg.Fastarg(description="address", help="manage addresses")

@app.command()
def hello_world(name: str):
    """hello world"""
    print("hello " + name)

@app2.command()
def create_todo(title: str, completed: bool = False):
    """create a todo"""
    print(f"create todo: {title} - {completed}")

@app2.command()
def update_todo(
    id: int = fastarg.Argument(help="the primary key of todo"), 
    completed: bool = fastarg.Option(False, help="completed status")
    ):
    """update a todo"""
    print(f"update todo: {id} - {completed}")

@app3.command()
def create_user(email: str, password: str, gold: float):
    """create a user"""
    print(f"creating {email}/{password} with {gold} gold")

@app3.command()
def delete_user(email: str):
    """delete a user"""
    print(f"deleting user {email}")

@app4.command()
def create_address(
    user_id: int, 
    address: str, 
    city: str = fastarg.Option("", help="city (e.g. Seattle)"), 
    state: str = fastarg.Option("", help="state (e.g. WA)"), 
    zip: str = fastarg.Option("", help="zip")
    ):
    """create address for user"""
    print(f"creating address for user {user_id}")
    print(f"{address} {city} {state} {zip}")


app.add_fastarg(app2, name="todo")
app.add_fastarg(app3, name="user")
app3.add_fastarg(app4, name="address")

if __name__ == "__main__":
    app.run()

Example usage

Run hello world:

$ python3 main.py hello_world foo

Show help text for todo subcommand:

$ python3 main.py todo -h

Show help text for a subcommand:

$ python3 main.py todo update_todo -h

Run create_todo subcommand:

$ python3 main.py todo create_todo "drink water"

Run create_todo with optional completed flag:

$ python3 main.py todo create_todo "drink water" --completed
$ python3 main.py todo create_todo "drink water" --no-completed

Run the nested create_address command:

$ python3 main.py user address create_address 123 "456 main st" --city bellevue --state wa --zip 98004

Arguments

Positional arguments are required. For example, the create_todo command takes a positional argument of title.

Options

Options are optional. For example, the create_address command takes an optional argument of city, state, and zip.

Subcommands in multiple files

main.py

import fastarg
import commands.todo as todo
import commands.user as user

app = fastarg.Fastarg(description="productivity app", prog="todo")

@app.command()
def hello_world(name: str):
    """hello world"""
    print("hello " + name)

app.add_fastarg(todo.app, name="todo")
app.add_fastarg(user.app, name="user")

if __name__ == "__main__":
    app.run()

commands/todo.py

import fastarg

app = fastarg.Fastarg(description="to do", help="manage todos")

@app.command()
def create_todo(title: str, completed: bool = False):
    """create a todo"""
    print(f"create todo: {title} - {completed}")

@app.command()
def update_todo(
    id: int = fastarg.Argument(help="the primary key of todo"), 
    completed: bool = fastarg.Option(False, help="completed status")
    ):
    """update a todo"""
    print(f"update todo: {id} - {completed}")

commands/user.py

import fastarg
import commands.address as address

app = fastarg.Fastarg(description="user", help="manage users")

@app.command()
def create_user(email: str, password: str, gold: float):
    """create a user"""
    print(f"creating {email}/{password} with {gold} gold")

@app.command()
def delete_user(email: str):
    """delete a user"""
    print(f"deleting user {email}")

app.add_fastarg(address.app, name="address")

Examples

https://github.com/travisluong/fastarg/tree/main/examples

Author

Travis Luong

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

fastarg-0.1.2.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

fastarg-0.1.2-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file fastarg-0.1.2.tar.gz.

File metadata

  • Download URL: fastarg-0.1.2.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.6

File hashes

Hashes for fastarg-0.1.2.tar.gz
Algorithm Hash digest
SHA256 d12c65bc665715a450d1bcc676d86d1786c7694424353f9428a6cdc907269433
MD5 767c037e2ba86241b9e07f66ea38a7ce
BLAKE2b-256 3731a54c184d57d0d9d39e712928e57e02cfeec671b308be3d7d7fab94443b59

See more details on using hashes here.

File details

Details for the file fastarg-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: fastarg-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/33.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.63.0 importlib-metadata/4.11.2 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.6

File hashes

Hashes for fastarg-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4bc3b1ee30c852c6984eebf63169f8a5947adf7bb325a6217fa70103aee7453b
MD5 f4fd6b16962be163ccf63de6acc0d192
BLAKE2b-256 142d990d4a53c493660425a3d30c8e0c5f5639ad90b550415a4c2af3e5715140

See more details on using hashes here.

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