Skip to main content

A lightweight Python command-line argument parser.

Project description

PyArgument

PyArgument is a lightweight Python command-line argument parser that allows defining multiple aliases for arguments, optional values, and default values. It is designed to be simple, flexible, and easy to use.


Features

  • Add arguments with multiple aliases (e.g., --help, -h)
  • Supports optional argument values (optarg=True)
  • Handles default values
  • Tracks which arguments were provided
  • Provides attribute access for parsed arguments
  • List all arguments, existing arguments, or aliases

Installation

Install locally

pip install .

Install via PIP

pip install pyargument

Usage

Example 1: Basic usage

from pyargument import PyArgument
import json
import os

def main():
    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("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 your_script.py --yes --dir /tmp --file example.txt

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.

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.

Example Output

$ python your_script.py --yes -d /tmp --file example.txt
Provided args: ['yes', 'dir', 'file']
yes: True
dir: /tmp
file: example.txt
pwd: /home/user

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.1.0.tar.gz (4.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.1.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyargument-0.1.0.tar.gz
  • Upload date:
  • Size: 4.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.1.0.tar.gz
Algorithm Hash digest
SHA256 a6c825c48735ad7c6a3adf58aafd8210f12f23e51513067f54e45160540f59b5
MD5 614139eade9e171e88fc202b42055c62
BLAKE2b-256 f2c7de8db40950c9f2c6a39558ae6145581b5d9a27284d3b03d1dc3a2d3ba254

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyargument-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 12e65a4abdc7884297391d10e028cbdd73a4d6e8cdca90ee91548e783775cdd4
MD5 2e1056867dcfc15403f77806e592b721
BLAKE2b-256 376cd3f2c7ad9e642c1368da3af8de80713fb3f749bcea0366acb6c7cb880380

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