Command line argument parser for pythonic code
Project description
clappy
Command Line Argument Parser for pythonic code.
Simple example with clappy:
import clappy as cl
log_level = cl.parse("--log_level", defaut="warning")
is_verbose = cl.parse("--verbose", "-v", is_flag=True)
# set logger with them
You can easily get each result independently.
Equivalent script without clappy:
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("--log_level", default="warning")
parser.add_argument("--verbose", "-v", action="store_true")
args = parser.parse_args()
log_level = args.log_level
is_verbose = args.verbose
# set logger with them
Script with clappy is more readable and writable.
Especially when you have multiple modules requiring commandline argument, you will have a hard time without clappy. You usually must manage same parser across multiple modules. After you register all arguments and parse result, you need to allocate each result to each module.
For example if you have a common library like logging, it can happen.
Clappy frees you from such tiresome process by independent parsing.
Install
pip install clappy
How to use
clappy is a wrapper of argparse. You can give arguments for clappy same as argparse. Reference of argparse is here.
Just call clappy.parse(*args, **kwargs) as if argparse.ArgumentParser().add_argument(*args, **kwargs). Same args are applicable for clappy.parse. Additionally, clappy accepts one keyword argument, "is_flag". It's just an alias of action="store_true". If you set "is_flag" True, you don't need to give argument after the option.
e.g. clappy.parse("--verbose", is_flag=True)
👍 --verbose
👎 --verbose True
Auto help generation
If you want to generate help automatically, call clappy.create_help(). It must be done after all arguments got parsed.
Subcommand
When you have main.py saying:
sub_parser1 = clappy.get_subcommand_parser("sub1")
sub_parser2 = clappy.get_subcommand_parser("sub2")
arg1 = sub_parser1.parse("--option_for_sub1")
arg2 = sub_parser2.parse("--option_for_sub2")
Construct parser with args
Initialize parser with clappy.initialize_parser(*args, **kwargs). These args are also common with argparse.ArgumentParser(*args, **kwargs).
Parse with args
Runs clappy.set_args_on_parse(*args, **kwargs). Same args with ArgumentParser().parse_args(*args, **kwargs)
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 Distributions
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 clappy-1.0.0-py3-none-any.whl.
File metadata
- Download URL: clappy-1.0.0-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60f8901bbb3a68308177325e8232b200e48f3b4df1d56016cf338685a1621aaf
|
|
| MD5 |
0d26ff546ba3b5a0e249f01269ec12fd
|
|
| BLAKE2b-256 |
f4a87b2c0c8ba38fe6fb099ecb2220e871a7eb05a02af88f17248fb04ea141dc
|