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

  • Long and short flags (e.g., --help, -h)
  • Add arguments with multiple aliases
  • Supports optional argument with 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, "is exists:", parser.dir.exists, "metadata:", parser.dir.meta)
print("file:", parser.file.value)
parser.file.value = "new_value.txt"
print("file:", parser.file.value) # argument value changed!

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.

Provider Object

Each argument is a Provider (class) instance. It behaves like its value:

str(parser.input) int(parser.count) bool(parser.online)

But also exposes metadata:

parser.input.value parser.input.exists parser.input.required parser.input.optarg parser.input.names


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.

Inspecting Arguments

list_meta()

  • Returns a dictionary of all defined arguments 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
  • Object-oriented
  • 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.40.tar.gz (5.2 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.40-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pyargument-0.40.tar.gz
Algorithm Hash digest
SHA256 cbe1c500ea28b4e9fa2d778d94357dde561cd598bc88f97e0d2c4ac199ed5ebb
MD5 c778f5c7bd7ca22ede53eda9d8702bb0
BLAKE2b-256 4e4cde402b3a41163f4a77d9e306deec8ea49ce674ffc590295bc03d3ba3d7ea

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyargument-0.40-py3-none-any.whl
  • Upload date:
  • Size: 5.9 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.40-py3-none-any.whl
Algorithm Hash digest
SHA256 616adf309ba578947e18cf0b23ba169777fd0113ab4e14c4aeb4dd9a1ec47921
MD5 21a951d902fe1b39e08c1c78af583435
BLAKE2b-256 5e474a43e1d49ae55b266e5a3bb354cf9284883402ba31277f332bcc03746101

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