A tiny library for building CLIs
Project description
A tiny library for building CLIs in Python.
Copy teenycli.py to your PYTHONPATH and use it anywhere.
A brief tour
Command-line argument parsing:
from teenycli import ArgP
def main_sub(args):
print(args.left - args.right)
def main_div(args):
if args.floor_division:
print(args.left // args.right)
else:
print(args.left / args.right)
argp = ArgP()
argp_sub = argp.subcmd("sub", main_sub)
argp_sub.add("left")
argp_sub.add("--right", required=True)
argp_div = argp.subcmd("div", main_div)
argp_div.add("left")
argp_div.add("right")
argp_div.add("--floor-division")
argp.dispatch()
Colors:
from teenycli import red, green, print
print(red("This text is red."))
print(green("This text is green."))
Error messages:
from teenycli import warn, error
warn("a warning")
error("an error")
Confirmation:
from teenycli import confirm
confirmed = confirm("Do you wish to continue?")
Installation
You can install TeenyCLI with pip install teenycli. But the main point of TeenyCLI is that it is a single Python file that you can put somewhere on your PYTHONPATH and then use in one-off scripts without needing to set up a virtual environment or install anything. Or, you can copy teenycli.py into your own project and modify and extend it as you like.
Why not Click?
Click is a popular library for building command-line applications.
Reasons you might prefer TeenyCLI to Click:
- You don't want to bother with dependency management.
- You want to copy TeenyCLI into your own project as a small and easy-to-understand starting point.
- You prefer an
argparse-style interface to Click's function decorators. - You like TeenyCLI's minimal interface and documentation.
Reasons you might prefer Click to TeenyCLI:
- You need Windows support. TeenyCLI has not been tested on Windows.
ArgPshould work, but ANSI colors probably won't. - You are writing production code and want to depend on a project that is more mature and battle-tested.
- You prefer Click's function decorators to an
argparse-style interface. - You need one of the many features that Click supports and TeenyCLI doesn't.
API reference
The ArgP class
ArgP.__init__(version=None, **kwargs)- If
versionis notNone, then a--versionflag will be added that prints the version. - All other
kwargsare passed on toargparse.ArgumentParser.
- If
ArgP.add(*names, *, n = None, required = None, **kwargs) -> ArgP- Add an argument or flag to be parsed.
ncontrols how many values will be consumed by the argument. It should be set to one ofArgP.ZERO,ArgP.ONE, orArgP.MANY. IfNone, it defaults toArgP.ZEROfor flags andArgP.ONEfor positionals.requiredcontrols whether the argument must be present.- The default value of a
ArgP.MANYargument is the empty list, notNoneas inargparse. - All other
kwargsare passed on toargparse.ArgumentParser.add_argument. - Returns the same
ArgPinstance so that calls can be chained.
ArgP.subcmd(name, handler, *, required=True) -> ArgP- Register a subcommand.
handleris a function that takes in a singleargsparameter, with the argument values as fields on the object (i.e.,args.my_flag, notargs["my_flag"]).- Returns a new
ArgPinstance for the subcommand. - Nested subcommands are supported.
- The
requiredparameter applies to all subcommands registered on the parent parser, i.e. either the parent parser requires that some subcommand be present, or allows there to be no subcommand.
ArgP.dispatch(handler=None, *, argv=None)- Parse arguments and dispatch to the handler function.
- If you registered subcommands with
ArgP.subcmd, this method will dispatch to the corresponding subcommand handler. Otherwise, you need to pass in your main handler here. dispatchreturns whatever your handler function returned.
ArgP.parse(argv=None)- If you prefer to do dispatch yourself,
parsewill return the parsed arguments without dispatching to a handler.
- If you prefer to do dispatch yourself,
User I/O
print(message: str, **kwargs) -> None- Wraps built-in
printto strip ANSI color codes if (a) the output stream is not a terminal, or (b) theNO_COLORenvironment variable is set to any value.
- Wraps built-in
warn(message: str) -> None:- Prints a message to standard error, prefixed by
Warning:in yellow text.
- Prints a message to standard error, prefixed by
error(message: str) -> None- Prints a message to standard error, prefixed by
Error:in red text.
- Prints a message to standard error, prefixed by
bail(message: str, *, code = 2) -> NoReturn- Prints an error message, then exits the program.
confirm(message: str) -> bool- Prompt the user with
messageand returnTrueif they respond "y" or "yes" orFalseif they respond "n" or "no". If the user gives some other response, they will be prompted again until they give a valid response.
- Prompt the user with
confirm_or_bail(message: str) -> None- Wraps
confirmto exit the program if it returnsFalse.
- Wraps
Colors
Colors should be used with teenycli.print, which will intelligently strip out colors when appropriate.
red(s: str) -> stryellow(s: str) -> strcyan(s: str) -> strgreen(s: str) -> str
Miscellaneous
run(cmd, *, shell = False) -> str- Run the command and return standard output as a string.
- Standard output is decoded to text using the system's default encoding.
- If the command exits with a non-zero status,
TeenyCliErroris raised. cmdis passed on tosubprocess.run; it can be a list of strings (recommended) or a single string to be parsed by the shell withshell=True.
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
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 teenycli-0.1.3.tar.gz.
File metadata
- Download URL: teenycli-0.1.3.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.11.5 Darwin/23.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
efddba000062b04e5e04b33426c126923d1b6dd8694aa39ead9bec91c06e561c
|
|
| MD5 |
44d0df5daa2fef9d8d361ee590bd722c
|
|
| BLAKE2b-256 |
504275c3288cabffcff1d6c15ba895e4d6b67ed2e662b50acda74b4975cdf9f1
|
File details
Details for the file teenycli-0.1.3-py3-none-any.whl.
File metadata
- Download URL: teenycli-0.1.3-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.11.5 Darwin/23.6.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
24a38860650887df4772e78526865b6c1d2d9174faae396256992b125add8cf7
|
|
| MD5 |
d48e32e7b366a4fb07883e053c0319e1
|
|
| BLAKE2b-256 |
0201921ce65d06394add59883ddf720f593e68c50ff830a12a66a7a4b51dafc5
|