Skip to main content

A simple, clean alternative to argparse

Project description

ArgHandle

A simple, lightweight alternative to argparse. No confusion, no boilerplate, just clean argument handling for your CLI tools.

Built because argparse is overkill for most scripts. ArgHandle gives you exactly what you need: register args, match them, print help, done.

Installation

pip install arghandle

Quick Start

from arghandle import ArgHandle

cli = ArgHandle()
cli.ProgramName("mytool")

cli.RegisterArg(["--version", "-v"], HelpMsg="Prints the version and exit")
cli.RegisterArg(["build"], HelpMsg="Build the project")
cli.RegisterArg(["--output", "-o"], StrictIndex=2, StrictIndex_ExitOnError=True, HelpMsg="Output file")

cli.PrintOnNoArgs("No arguments provided. Use --help or -h for usage.", Exit=True)
cli.HandleHelp()

if cli.IsArgInActualArgs("--version") or cli.IsArgInActualArgs("-v"):
    raise SystemExit("mytool v1.1.0\n")

if cli.IsArgInActualArgs("build"):
    output = cli.SetVariableToIndex(2)
    print(f"Building: {output}")

Running mytool --help prints:

mytool --help/-h called:
  [--help, -h]: Prints this help message and exit
  [--version, -v]: Prints the version and exit
  [build]: Build the project
  [--output, -o]: Output file

API

ArgHandle()

Main class. Instantiate once at the start of your program.

cli = ArgHandle()

ProgramName(string: str)

Sets the program name shown in the help header.

cli.ProgramName("mytool")

RegisterArg(Flags, StrictIndex=None, StrictIndex_ExitOnError=False, *, HelpMsg)

Registers an argument to the help screen and optionally enforces a strict position in sys.argv.

  • Flags: list of flags/commands (e.g. ["--version", "-v"])
  • StrictIndex: if set, the arg must appear at this index in sys.argv
  • StrictIndex_ExitOnError: if True, exits with an error when the arg is at the wrong index; if False, returns StrictIndexBroken
  • HelpMsg: description shown in help output
cli.RegisterArg(["--version", "-v"], HelpMsg="Prints the version and exit")
cli.RegisterArg(["--output", "-o"], StrictIndex=2, StrictIndex_ExitOnError=True, HelpMsg="Output file")

Returns:

Return Type Meaning
Registered Successfully registered
NotRegistered Empty or invalid flags list
NoKwargs No HelpMsg provided
OverlimitKwargs More than one kwarg passed
StrictIndexBroken Arg not at required index and StrictIndex_ExitOnError=False

HandleHelp(Exit=True)

Checks if --help or -h is in args and prints the help screen. Exits by default.

cli.HandleHelp()

Output format:

mytool --help/-h called:
  [--help, -h]: Prints this help message and exit
  [--version, -v]: Prints the version and exit

PrintOnNoArgs(string, Exit=False)

Prints a message if no arguments are passed. Optionally exits.

cli.PrintOnNoArgs("No arguments provided.", Exit=True)

IsArgInActualArgs(String) -> bool

Returns True if the string is present anywhere in sys.argv[1:].

if cli.IsArgInActualArgs("build"):
    ...

IsArgMatch(String, AtIndex) -> bool

Returns True if the string matches the arg at a specific index in sys.argv[1:].

if cli.IsArgMatch("build", 0):
    ...

SetVariableToIndex(Index) -> str | IndexOutOfRange

Returns the value of sys.argv at the given index. Returns IndexOutOfRange if the index doesn't exist.

output = cli.SetVariableToIndex(2)
if isinstance(output, IndexOutOfRange):
    raise SystemExit("No value provided at index 2\n")
print(output)  # "myfile.c"

ArgCount() -> int

Returns the total number of arguments including the script name.

print(cli.ArgCount())

Return Types

All return type classes are importable directly:

from arghandle import (
    Registered,
    NotRegistered,
    NoKwargs,
    OverlimitKwargs,
    StrictIndexBroken,
    IndexOutOfRange
)

Real World Example

ArgHandle was built as part of AutoBuild, a Make-like build system with its own .abuild syntax. Check it out to see ArgHandle in action in a real project.

License

GPLv3, see LICENSE for details.

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

arghandle-1.2.1.tar.gz (43.5 kB view details)

Uploaded Source

Built Distribution

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

arghandle-1.2.1-py3-none-any.whl (30.8 kB view details)

Uploaded Python 3

File details

Details for the file arghandle-1.2.1.tar.gz.

File metadata

  • Download URL: arghandle-1.2.1.tar.gz
  • Upload date:
  • Size: 43.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for arghandle-1.2.1.tar.gz
Algorithm Hash digest
SHA256 10fa8a8a65d76cadc7f324daec666e45d7bdce0a0e472f5c77f36901dabd3d7c
MD5 192a785b7c86e0aa27ab34ef1e390e36
BLAKE2b-256 0446569b32b8bf2dbac1317881da8380b3027bd4e8096eff66e64bb486b3adec

See more details on using hashes here.

File details

Details for the file arghandle-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: arghandle-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 30.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for arghandle-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e22338a3d6bc474cae01909290419587d7a5ad7590a434af530f4b1f41c592f1
MD5 c0afddf5f93b03b14635939ea7b2eed2
BLAKE2b-256 0b9bf0a44e93f1d3ba0c4ec49d53e0b78171539b86e27d608d2cf63f536eaf8a

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