A module for building command-line interface from configuration.
Project description
config-argument-parser
A module for building command-line interface from configuration.
Motivation
Configuration files are highly readable and useful for specifying options, but sometimes they are not convenient as command-line interface. However, it requires writing a lot of code to produce a CLI. This module automates the building process, by utilizing the Python standard libraries configparser
and argparse
. It has features below.
- The comments are parsed as help messages. (Most libraries do not preserve the comments.)
- Consistent format between configuration and script provides ease of use.
- Only a few lines are needed to build a CLI.
Usage
Create an example script example.py
:
import args
parser = args.ConfigArgumentParser()
parser.read("config.ini")
parser.add_arguments(shorts="snb") # or parser.add_arguments()
parser.parse_args()
print("Configs:", parser.defaults)
print("Args:", parser.args)
Create a configuration file config.ini
in the same directory:
[DEFAULT]
# Help message before argument. Optional.
a_string = 'abc'
a_number = 1.23 # inline comments are omitted
# Help can span multiple lines.
# This is another line.
a_boolean = False
Show help, python example.py -h
:
usage: example.py [-h] [-s A_STRING] [-n A_NUMBER] [-b]
optional arguments:
-h, --help show this help message and exit
-s A_STRING, --a_string A_STRING
Help message before argument. Optional. (default: abc)
-n A_NUMBER, --a_number A_NUMBER
-b, --a_boolean Help can span multiple lines, this is another line.
(default: False)
Regular run, python example.py
:
Configs: {'a_string': 'abc', 'a_number': 1.23, 'a_boolean': False}
Args: {'a_string': 'abc', 'a_number': 1.23, 'a_boolean': True}
Run with options, such as python example.py -b -n 1
:
Configs: {'a_string': 'abc', 'a_number': 1.23, 'a_boolean': False}
Args: {'a_string': 'abc', 'a_number': 1.0, 'a_boolean': True}
Installation
Install from PyPI:
pip install config-argument-parser
Alternatively, after git clone
and cd
into this repo, install in development mode:
python -m pip install --upgrade pip
pip install -e .[dev]
pre-commit install
Uninstall:
pip uninstall config-argument-parser
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 Distribution
Built Distribution
Hashes for config-argument-parser-0.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9eb032c3319698cd3262f7c87b307a8b3e748d165b596af3dc1bae01432a6c96 |
|
MD5 | b2b29f7985034d7a11f77a4aa7dddf40 |
|
BLAKE2b-256 | fc96aac0d3d7ad9da482deb4513fe2946263fbaac50413ccab05b8c856c00d93 |
Hashes for config_argument_parser-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3cffa06bc804c94647723efa39010f3df8d54326b4557c4e3558c95c10a777df |
|
MD5 | 6eaa657a1997715ccaa00041479285dc |
|
BLAKE2b-256 | d6a364960607b88d8b30692710b00d1910578c05ee4649052c7c4a19d2de57b9 |