Package: argdantic
argdanticTyped command line interfaces withargparseandpydantic.Featuresargdanticprovides a thin boilerplate layer to provide a modern CLI experience, including:Typed arguments:arguments require full typing by default, enforcing clarity and help your editor provide better support (linting, hinting).Nested models:exploitpydanticmodels to scale from simple primitives to complex nested configurations with little effort.Nested commands:combine commands and build complex hierarchies to build complex interfaces.Validation by default:thanks topydantic, field validation is provided by default, with the desired complexity.Multiple sources:arguments can be provided from multiple sources, including environment variables, JSON, TOML and YAML files.QuickstartInstallationInstallingargdanticcan be done from source, or simply usingpip.
The only required dependency is, of course,pydantic, while the remaining can be selected depending on your needs:recommended choice: install everythingthis includes orjson, pyyaml, tomli, python-dotenvuser@pc:~$pipinstallargdantic[all]env, json, toml or yaml dependenciesuser@pc:~$pipinstallargdantic[env|json|toml|yaml]minimum requirement, only pydantic includeduser@pc:~$pipinstallargdanticA Simple ExampleCreating a CLI withargdanticcan be as simple as:fromargdanticimportArgParser# 1. create a CLI instanceparser=ArgParser()# 2. decorate the function to be called@parser.command()defbuy(name:str,quantity:int,price:float):print(f"Bought{quantity}{name}at ${price:.2f}.")# 3. Use your CLI by simply calling itif__name__=="__main__":parser()Then, in a terminal, thehelpcommand can provide the usual information:$pythoncli.py--help> usage: buy [-h] --name TEXT --quantity INT --price FLOAT>> optional arguments:>   -h, --help      show this help message and exit>   --name TEXT>   --quantity INT>   --price FLOATThis gives us the required arguments for the execution:$pythoncli.py--nameapples--quantity10--price3.4> Bought 10 apples at $3.40.Using ModelsPlain arguments andpydanticmodels can be mixed together:fromargdanticimportArgParserfrompydanticimportBaseModelparser=ArgParser()classItem(BaseModel):name:strprice:float@parser.command()defbuy(item:Item,quantity:int):print(f"Bought{quantity}X{item.name}at ${item.price:.2f}.")if__name__=="__main__":parser()This will produce the following help:usage: cli.py [-h] --item.name TEXT --item.price FLOAT --quantity INToptional arguments:-h, --help          show this help message and exit--item.name TEXT--item.price FLOAT--quantity INTArguments From Different Sourcesargdanticsupports several inputs:.envfiles, environment variables, and secrets thanks topydantic.JSON files, using either the standardjsonlibrary, ororjsonif available.YAML files, using thepyyamllibrary.TOML files, using the lightweighttomlilibrary.Sources can be imported and added to each command independently, as such:fromargdanticimportArgParserfromargdantic.sourcesimportEnvSettingsSource,JsonSettingsSourceparser=ArgParser()@parser.command(sources=[EnvSettingsSource(env_file=".env",env_file_encoding="utf-8"),JsonSettingsSource(path="settings.json"),])defsell(item:str,quantity:int,value:float):print(f"Selling:{item}x{quantity},{value:.2f}$")if__name__=="__main__":parser()This is just a brief introduction to the library, more examples and details can be found in thedocumentation.ContributingContributions are welcome! You can open a new issue to report bugs, or suggest new features. If you're brave enough, pull requests are also welcome.