Skip to main content

A lightweight Python command-line argument parser.

Project description

PyArgument

PyArgument is a lightweight Python command-line argument parser with support for aliases, optional values, required arguments, and argument collection.
It is designed for simplicity and direct control over parsing behavior.


Features

  • Add arguments with multiple aliases (e.g., --help, -h)
  • Supports optional argument values (optarg=True)
  • Required arguments (required=True)
  • Handles default values
  • Tracks which arguments were provided
  • Attribute-based access (parser.input, parser.dir)
  • Collects parsed arguments into a list, using (parser.pyargs)

Installation

Install locally

pip install .

Install via PIP

pip install pyargument

Usage

Example 1: Basic usage

from pyargument import PyArgument
import json
import os

pwd = os.getcwd()
parser = PyArgument()
parser.add_arg("--online")
parser.add_arg("--input", "-in", optarg=True, required=True)
parser.add_arg("--dir", "--folder", optarg=True, default=pwd)
parser.add_arg("--file", optarg=True)
parser.add_arg("--pwd", "-p", "--cwd", required=True)

parser.parse_args()

print("Collected args:", parser.pyargs)
print("Arguments metadata:", json.dumps(parser.list_all(), indent=4))
print("Arguments provided:", parser.list_args())
print("Arguments alias:", json.dumps(parser.list_alias(), indent=4))
print("online:", parser.online)
print("input:", parser.input)
print("dir:", parser.dir)
print("file:", parser.file)
print("pwd:", parser.pwd)

Run from command line:

python -m pyargument --input data.txt --pwd /home/user --dir /tmp

Example 2: Aliases

parser.add_arg("--help", "-h", "-?")
parser.parse_args()
print(parser.help)  # True if any alias is used
  • All aliases (--help, -h, -?) map to the same internal argument.
  • Access via the main argument name (help).

Methods

add_arg(*names, optarg=False, default=None, required=True)

  • *names: All aliases for this argument.
  • optarg: If True, the argument can accept a value.
  • default: Default value if argument not provided.
  • required: Currently for future use; reserved.

Defining Arguments

add_arg(*names, optarg=False, default=None, required=False)

Parameters:

  • names: One or more argument names
  • optarg: Accepts a value if True
  • default: Default value if not provided
  • required: Program exits if missing

Example:

parser.add_arg("--input", "-in", optarg=True, required=True) parser.add_arg("--online")


Supported Argument Formats

--flag
--key value
--key=value
-key value

Examples:

python app.py --input file.txt --pwd /home/user
python app.py --input=file.txt -p /home/user


Accessing Parsed Values

Each argument is available as an attribute:

parser.input
parser.dir
parser.file
parser.pwd
parser.online

Boolean flags return True when present.


Required Arguments

If a required argument is missing, execution stops:

Required argument ('--input', '-in') not provided.

Exit code: 1


parse_args(*args)

  • Parses command-line arguments (sys.argv[1:]).
  • Updates attributes of the parser object.

list_all()

  • Returns a dictionary of all defined arguments and their metadata.

list_args()

  • Returns a list of arguments that were provided in the command line.

list_alias()

  • Returns a dictionary mapping each internal argument to all its aliases.

Raw Parsed Arguments (pyargs)

  • parser.pyargs

pyargs stores a simplified list of parsed arguments. Useful for forwarding arguments to another command.

Example output:

['input', 'file.txt', 'pwd', '/home/user']


Design Philosophy

  • Minimal
  • Explicit
  • Hackable
  • No magic
  • Easy to understand

License

MIT License


Repository

https://github.com/ankushbhagats/pyargument

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

pyargument-0.20.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

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

pyargument-0.20-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file pyargument-0.20.tar.gz.

File metadata

  • Download URL: pyargument-0.20.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for pyargument-0.20.tar.gz
Algorithm Hash digest
SHA256 f27e5ddf87a09357f97f3902cc720b0a8911c93c107ccc73d8939a745ada7cc9
MD5 766a23d238fbe2846f60d39037cd56b9
BLAKE2b-256 39a7a0444203104dc05775da7959a263e4e329be31a8480f0cb1b5894595b8cd

See more details on using hashes here.

File details

Details for the file pyargument-0.20-py3-none-any.whl.

File metadata

  • Download URL: pyargument-0.20-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for pyargument-0.20-py3-none-any.whl
Algorithm Hash digest
SHA256 8634c6274e9e70ecc82a1474aff384c007d726c2d602a4d4e2355a80b47540db
MD5 73bba4d40700f38bb9040fd8c02c2adf
BLAKE2b-256 ac5540f52bc232f63a68e4ceda95304f7ec36c8b937f38cc6ec392204fa9088c

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