Tiny wrapper for Python's `argparse` package with YAML-based cli configuration.
Project description
argsy
Tiny wrapper for Python's argparse package with YAML-based cli configuration.
Overview
The argsy package aims to make CLI argument configuration and parsing declarative and simple, while still leveraging Python's argparse package to handle the parsing details and user help messaging.
Configuration supports declarative configuration of:
- top-level position args, options / flags
- a single tier of subcommands
- position args, options / flags for each subcommand
- support for setting default values for options / flags
- partial
nvarssupport for capturing multiple input values - support for the
actiondirective (for things like toggling booleans)
Getting Started
Install argsy
pip install argsy>=1.0.0
Define your program structure
CLI arguments are define as either:
- a YAML file or string, or
- a Python
dictobject
YAML file
#
# my_cli_args.yml
#
program:
name: my_program
descripton: "About what my_program does."
args:
option1:
cmd_type: option
flags: '-o|--option1'
help: "Short text about the option."
required: true
#
# my_program.py
#
from argsy import Argsy
argsy_parser = Argsy(config_file_name="my_cli_args.yml")
parsed_args = argsy_parser.parse_args(sys.argv[1:])
# User runs:
# my_program -o USER_PROVIDED_OPTION_VALUE
#
# Resulting 'parsed_args':
# {
# 'cmd': None,
# 'args': {
# 'option1': USER_PROVIDED_OPTION_VALUE
# }
# }
YAML String
#
# my_program.py
#
from argsy import Argsy
CLI_ARGS = """
program:
name: my_program
descripton: "About what my_program does."
args:
option1:
cmd_type: option
flags: '-o|--option1'
help: "Short text about the option."
required: true
"""
argsy_parser = Argsy(config_str=CLI_ARGS)
parsed_args = argsy_parser.parse_args(sys.argv[1:])
# User runs:
# my_program -o USER_PROVIDED_OPTION_VALUE
#
# Resulting 'parsed_args':
# {
# 'cmd': None,
# 'args': {
# 'option1': USER_PROVIDED_OPTION_VALUE
# }
# }
Python Dictionary
#
# my_program.py
#
from argsy import Argsy
CLI_ARGS = dict(
program=dict(
name='my_program',
description='About what my_program does.',
args=dict(
option1=dict(
cmd_type='option',
flags='-o|--option1',
help='Short text about the option.',
required=True
)
)
)
)
argsy_parser = Argsy(config_dict=CLI_ARGS)
parsed_args = argsy_parser.parse_args(sys.argv[1:])
# User runs:
# my_program -o USER_PROVIDED_OPTION_VALUE
#
# Resulting 'parsed_args':
# {
# 'cmd': None,
# 'args': {
# 'option1': USER_PROVIDED_OPTION_VALUE
# }
# }
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 argsy-1.0.3-py3-none-any.whl.
File metadata
- Download URL: argsy-1.0.3-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f836857d791ad974517a170ea7e6c7c5ff7b294aaee787d8dd5aa7d8820ec38e
|
|
| MD5 |
06eb671aac03b278cad158f53a7ea535
|
|
| BLAKE2b-256 |
11dd6761d1fb2ced7a594c7ef8aec3d34cd75d366310820da25ba405cc6c84f7
|