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
nvars
support for capturing multiple input values - support for the
action
directive (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
dict
object
YAML file
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
from argsy import Argsy
argsy_parser = Argsy(config_file_name="my_cli_args.yml")
parsed_args = argsy_parser.parse_args(sys.argv[1:])
YAML String
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:])
Python Dictionary
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:])