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")
# add short options for the first few arguments in order
# default is parser.add_arguments()
parser.add_arguments(shorts="sfb")
parser.parse_args()
print("Configs:", parser.defaults)
print("Args: ", parser.args)
Create a configuration file config.ini
in the same directory:
[DEFAULT]
# Help message of the first argument. Help is optional.
a_string = 'abc'
a_float = 1.23 # inline comments are omitted
# Help can span multiple lines.
# This is another line.
a_boolean = False
an_integer = 0
Show help, python example.py -h
:
usage: example.py [-h] [-s A_STRING] [-f A_FLOAT] [-b]
[--an_integer AN_INTEGER]
optional arguments:
-h, --help show this help message and exit
-s A_STRING, --a_string A_STRING
Help message of the first argument. Help is optional.
(default: abc)
-f A_FLOAT, --a_float A_FLOAT
-b, --a_boolean Help can span multiple lines. This is another line.
(default: False)
--an_integer AN_INTEGER
Regular run, python example.py
:
Configs: {'a_string': 'abc', 'a_float': 1.23, 'a_boolean': False, 'an_integer': 0}
Args: {'a_string': 'abc', 'a_float': 1.23, 'a_boolean': False, 'an_integer': 0}
Run with options, such as python example.py -b -f 1
:
Configs: {'a_string': 'abc', 'a_float': 1.23, 'a_boolean': False, 'an_integer': 0}
Args: {'a_string': 'abc', 'a_float': 1.0, 'a_boolean': True, 'an_integer': 0}
Installation
Install from PyPI:
python -m pip install --upgrade pip
pip install config-argument-parser
Alternatively, install from source:
git clone https://github.com/yuanx749/config-argument-parser.git
cd config-argument-parser
then install in development mode:
git checkout main
python -m pip install --upgrade pip
pip install -e .
or:
git checkout dev
python -m pip install --upgrade pip
pip install -e .[dev]
pre-commit install
Uninstall:
pip uninstall config-argument-parser
Notes
This package uses Semantic Versioning.
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.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5c5507cfca3ff61994f2cb2df87df68425ed2dccc20eb9463c9f18ca865cf23d |
|
MD5 | 5d2404a3628b3b93d4f37744d811380e |
|
BLAKE2b-256 | 1a66bf58a50289817c510cf871573a3f1241d1e19ab9ecac8ab6c7f02b60c0b5 |
Hashes for config_argument_parser-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8cf1e13f8939e6fa9566ca59eed4bd78ff7d1c3fd650fee2de3d9ff324b0f7c0 |
|
MD5 | befdcf127571f91e59b9bee65d44e2e6 |
|
BLAKE2b-256 | b1d804a4ad238816ce4460a8233ce7401f5accb25f669881404edee36ebc16f1 |