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

Defining Arguments

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

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.60.tar.gz (5.3 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.60-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pyargument-0.60.tar.gz
Algorithm Hash digest
SHA256 9d58883d41c2e77619d20e6c788b8fe1a5e9868483c21b12af550d5ab050f9d8
MD5 c7c44353928c3a4230568db9a72729bf
BLAKE2b-256 d230602d06eb6085bfb307d7da581be62c2faefb7262d883e67d610ca96e6788

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyargument-0.60-py3-none-any.whl
  • Upload date:
  • Size: 6.0 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.60-py3-none-any.whl
Algorithm Hash digest
SHA256 a12de68173c05780c554c1becb09e8b206b08e0f815919e309a7eabfb55d8241
MD5 0468a4d72fe1ea5607a48275130b3b04
BLAKE2b-256 b43dfd350fe868b4ed8a4ee1864f2977cb2e121ab572702ad10fdf112daf3b4f

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