Command line argument parser for pythonic code
Project description
clappy
Command Line Argument Parser for Pythonic code. Simple and readable wrapper of argparse.
with clappy:
import clappy as cl
foo = cl.parse("--foo")
bar = cl.parse("--bar", is_flag=True)
without clappy:
from argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument("--foo")
parser.add_argument("--bar", action="store_true")
args = parser.parse_args()
foo = args.foo
bar = args.bar
Clappy is strong especially with subcommands.
with clappy:
import clappy as cl
if cl.subcommand("foo").invoked:
opt = cl.parse("--foo_opt")
elif cl.subcommand("bar").invoked:
opt = cl.parse("--bar_opt")
without clappy:
import argparse
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
subparser1 = subparsers.add_parser("foo")
subparser1.add_argument("--foo_opt")
subparser2 = subparsers.add_parser("bar")
subparser2.add_argument("--bar_opt")
args = parser.parse_args()
if hasattr(args, "foo_opt"):
opt = args.foo_opt
elif hasattr(args, "bar_opt"):
opt = args.bar_opt
Clappy becomes really helpful when you have multiple modules requiring command line arguments. Without clappy you must manage same parser and the result of parse across multiple modules.
Clappy frees you from such tiresome process by parsing independently.
Install
pip install clappy
How to use
It's a wrapper of argparse and same arguments are available.
Just call clappy.parse(*args, **kwargs) as if argparse.ArgumentParser().add_argument(*args, **kwargs). Reference is here.
Additionally, clappy.parse accepts one keyword argument named "is_flag". It's just an alias of action="store_true" in argparse.
An option with "is_flag" doesn't require argument, and it returns bool indicating if the option is given in command line or not.
Subcommand
To use subcommand, call clappy.subcommand().
The subcommand function accepts same arguments as subparsers.add_parser(). Available arguments are here.
It accepts only one positional argument like add_parser() method.
subcommand = clappy.subcommand(name, **kwargs) # 1
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(title="foo")
subparser = subparsers.add_parser(name, **kwargs) # 2
# 1 and 2 accept same arguments.
You can detect if the subcommand is invoked in both implicit or explicit ways. Following 2 examples are equivalent.
sc = clappy.subcommand("foo")
if sc.invoked:
# do smth
if clappy.subcommand("foo"):
# do smth
Auto help generation
If you wanna print usage of the script when it got runned with -h or --help option, there are 2 ways to generate help automatically.
-
Call clappy.create_help() for auto help generation after all arguments got parsed.
clappy.parse("--foo", help="help message here") clappy.parse("--bar", help="help message here") clappy.create_help()
Help option usually makes script print help and exit without executing all lines. Therefore, commandline parser must know when registering all arguments finishes.
clappy.create_help() explicitly shows the end of parse.
-
Call parser as context manager like following example.
with clappy.get_parser(): clappy.parse("--foo", help="help message here") clappy.parse("bar", help="help message here")
Parser automatically creates help after with block.
Construct parser with args
It is recommended to call getter of parser as context manager to construct parser with args you want.
with clappy.get_parser(*args, **kwargs):
clappy.parse("--foo", help="help message here")
clappy.parse("bar")
That's because it detects and logs unrecognized arguments after parsing to notify error. Moreover, it creates help automatically thanks to with statement.
Available arguments of get_parser(*args, **kwargs) is same as argparse.ArgumentParser(). Reference is here.
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.3-py3-none-any.whl.
File metadata
- Download URL: clappy-1.0.3-py3-none-any.whl
- Upload date:
- Size: 11.9 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 |
0e0fc75a307c4a2a8f6022a176802a24592f6e5a29674fa0e439c3d7421dda15
|
|
| MD5 |
fbbdaaee9bb859775aaf6383e7b45753
|
|
| BLAKE2b-256 |
e045217e611f4b238a7876459bca86729d253a67754e0fa50f866bfc825e05d2
|