Typed Argument Parsing with Pydantic enhanced version
Project description
Help
See documentation for help.
Requirements
Requires Python 3.10+, and is compatible with the Pydantic library. Use Rich for better console output.
Installation
Installation with pip is simple:
$ pip install argparse-dantic
Example
from argparse_dantic import ArgumentParser, BaseModel, Field
class Arguments(BaseModel):
# Required Args
string: str = Field(description="a required string", aliases=["-s"])
integer: int = Field(description="a required integer", aliases=["-i"])
flag: bool = Field(description="a required flag", aliases=["-f"])
# Optional Args
second_flag: bool = Field(False, description="an optional flag")
third_flag: bool = Field(True, description="an optional flag")
def main() -> None:
# Create Parser and Parse Args
parser = ArgumentParser(
model=Arguments,
prog="Example Program",
description="Example Description",
version="0.0.1",
epilog="Example Epilog",
)
args = parser.parse_typed_args()
# Print Args
print(args)
if __name__ == "__main__":
main()
$ python3 example.py --help
usage: Example Program [-h] [-v] [-s STRING] [-i INTEGER] [-f | --flag | --no-flag]
[--second-flag] [--no-third-flag]
Example Description
required arguments:
-s STRING, --string STRING
a required string
-i INTEGER, --integer INTEGER
a required integer
-f, --flag, --no-flag
a required flag
optional arguments:
--second-flag an optional flag (default: False)
--no-third-flag an optional flag (default: True)
help:
-h, --help show this help message and exit
-v, --version show program's version number and exit
Example Epilog
$ python3 example.py --string hello -i 42 -f
string='hello' integer=42 flag=True second_flag=False third_flag=True
Advanced Example
from argparse_dantic import ArgumentParser, BaseModel, Field, ActionNameBind
class GlobalModel(BaseModel):
action_name: ActionNameBind # This is a special field that binds the action name to the model
verbose: bool = Field(False, description="verbose output", global_=True)
debug: bool = Field(False, description="debug output", global_=True)
class PubOptionsModel(BaseModel):
target: str = Field(description="build target", aliases=["-t"])
clean: bool = Field(False, description="clean build", aliases=["--c"])
class BuildCommandModel(GlobalModel, PubOptionsModel):
build_type: str = Field(description="build type", aliases=["-bt"])
class InstallCommandModel(GlobalModel, PubOptionsModel):
install_type: str = Field(description="install type", aliases=["-it"])
class BasicModel(GlobalModel):
build: BuildCommandModel = Field(aliases=["bd"], description="build command")
install: InstallCommandModel = Field(aliases=["ins"], description="install command")
def main() -> None:
# Create Parser and Parse Args
parser = ArgumentParser(
model=BasicModel,
prog="Example Program",
description="Example Description",
version="0.0.1",
epilog="Example Epilog",
)
args = parser.parse_typed_args()
# Print Args
print(args)
# Get Command Arguments Faster
command_arguments = getattr(args, args.action_name)
# Get Global Arguments Faster
verbose = args.global_data.get("verbose")
debug = args.global_data.get("debug")
# In child models you can also access global data
# verbose = command_arguments.global_data.get("verbose")
# debug = command_arguments.global_data.get("debug")
License
This project is licensed under the terms of the MIT license.
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
argparse_dantic-0.2.0.tar.gz
(63.8 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file argparse_dantic-0.2.0.tar.gz.
File metadata
- Download URL: argparse_dantic-0.2.0.tar.gz
- Upload date:
- Size: 63.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3219e328be87c82dd1a19d4f586270c20f3f7fcd4429b155735f28360efee833
|
|
| MD5 |
5bf87bcd713b7e946fdce8a5b9fd3055
|
|
| BLAKE2b-256 |
e9742217258e16eecae352761f983052abdcc7bcfbbbd1c89e1148f06adde16c
|
File details
Details for the file argparse_dantic-0.2.0-py3-none-any.whl.
File metadata
- Download URL: argparse_dantic-0.2.0-py3-none-any.whl
- Upload date:
- Size: 72.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
233def84997cc3eb4924db0924037ae2d8863379cd3f61d85b7e988cbc2f6503
|
|
| MD5 |
7043342aa7f0a464a9bf352fd0930478
|
|
| BLAKE2b-256 |
f3c660a79246d8b015a6bcd745a3941f61c5ad2919ae0f911470e6d54af50a6f
|