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: match args, print help, done.

Installation

pip install arghandle

Quick Start

from arghandle import ArgHandle

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

cli.RegisterToHelp("version", ["--version", "-v"], HelpMsg="Prints the version and exit")
cli.RegisterToHelp("build", ["build"], HelpMsg="Build the project")

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.0.0\n")

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

Running mytool --help prints:

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

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")

RegisterToHelp(Argument, RepresentingCommands, *, HelpMsg)

Registers a command to the --help output.

  • Argument - internal name for the command
  • RepresentingCommands - list of flags/commands (e.g. ["--version", "-v"])
  • HelpMsg - description shown in help

Returns one of: Registered, NotRegistered, NoKwargs, OverlimitKwargs

cli.RegisterToHelp("version", ["--version", "-v"], HelpMsg="Prints the version and exit")

HandleHelp(Exit=True)

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

cli.HandleHelp()

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.

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

IsArgMatch(String, AtIndex) -> bool

Returns True if the string matches the arg at a specific index.

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

ArgCount() -> int

Returns the total number of arguments including the script name.

print(cli.ArgCount())

Return Types

RegisterToHelp returns one of these custom types to indicate the result:

Type Meaning
Registered Successfully registered
NotRegistered Empty argument or command list
NoKwargs No HelpMsg provided
OverlimitKwargs More than one kwarg passed

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.0.0.tar.gz (41.3 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.0.0-py3-none-any.whl (28.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for arghandle-1.0.0.tar.gz
Algorithm Hash digest
SHA256 55dc44228ab61dd405c91c3d2f259211626c842d656698059f0ea74ff1eacd17
MD5 cfe80c8194fd93eff2dc59d9e4b88f1f
BLAKE2b-256 a866eec5f0c2b9e347f9f1c9f3a78419068ac69450d040e648cf005f3a09ae5c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: arghandle-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 28.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.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bafe919f1a7e5eac74e58888032a1a6241811f672b629162d703315093bff69b
MD5 722c6e9708bc5c9cfec1ab34170a719a
BLAKE2b-256 0230d24a9f9da37fdf814c42ed43bb29bd3ceab390607694ce4a3ca74955204d

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