Write type-safe and elegant CLIs with a clear separation of concerns.
Project description
typed_argparse
💡 write type-safe and elegant CLIs with a clear separation of concerns.
Motivation
Want to add type annotations to a code base that makes use of argparse
without refactoring all you CLIs?
typed_argparse
allows to do that with minimal changes:
- Add a type
Args(TypedArgs)
that inherits fromTypedArgs
and fill it with type annotations. - Wrap the result of e.g. your
parse_args
function withArgs
. - That's it, enjoy IDE auto-completion and strong type safety 😀.
Features
- Argument parsing based on type annotation (including runtime validation, and support for many common types)
- Clear separation of concern between argument parsing and business logic.
- Support for super-low-latency shell auto-completions.
- Great for writing sub-command CLIs.
- Very lightweight.
- No dependencies.
- Fully typed, no extra type stubs required.
- Offers both a high-level and a low-level API.
The high-level API generally requires less code to write, is fully based on type annotations, and is the preferred way for writing new CLIs.
The low-level API is mainly a low-effort migration path for incorporating type-safety into existing CLIs based on
argparse
.
Install
$ pip install typed-argparse
The only requirement is a modern Python (3.6+).
Basic Usage
import typed_argparse as tap
# 1. Argument definition
class Args(tap.TypedArgs):
my_arg: str = tap.arg(help="some help")
number_a: int = tap.arg(default=42, help="some help")
number_b: Optional[int] = tap.arg(help="some help")
verbose: bool = tap.arg(help="some help")
names: List[str] = tap.arg(help="some help")
# 2. Business logic
def runner(args: Args):
print(f"Running my app with args:\n{args}")
# 3. Bind & run argument definition + business logic
def main() -> None:
tap.Parser(Args).bind(runner).run()
Documentation
See full documentation.
Changes
See change log.
License
This project is licensed under the terms of the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
typed-argparse-0.2.6.tar.gz
(18.6 kB
view hashes)
Built Distribution
Close
Hashes for typed_argparse-0.2.6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac91b7eeca39eb4b2f845167c6ce13d5ebf8fc909c623d18ba2e0e8d3af883b7 |
|
MD5 | 8d5b31f7ca3bd724450821692c48e5df |
|
BLAKE2b-256 | b402a70caa825362c74b73c29f4cb78f24a8163a87f6676880f05f49063be093 |