mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-20 13:08:31 +02:00
add test and argument handler for runtime override of configurations.
This commit is contained in:
parent
f80f033d5d
commit
110e2b9022
2 changed files with 25 additions and 0 deletions
20
jrnl/args.py
20
jrnl/args.py
|
@ -2,8 +2,10 @@
|
||||||
# License: https://www.gnu.org/licenses/gpl-3.0.html
|
# License: https://www.gnu.org/licenses/gpl-3.0.html
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
|
from jrnl import config
|
||||||
import re
|
import re
|
||||||
import textwrap
|
import textwrap
|
||||||
|
import json
|
||||||
|
|
||||||
from .commands import postconfig_decrypt
|
from .commands import postconfig_decrypt
|
||||||
from .commands import postconfig_encrypt
|
from .commands import postconfig_encrypt
|
||||||
|
@ -314,6 +316,24 @@ def parse_args(args=[]):
|
||||||
help=argparse.SUPPRESS,
|
help=argparse.SUPPRESS,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
overrides = parser.add_argument_group("Config file overrides",textwrap.dedent('These are one-off overrides of the config file options'))
|
||||||
|
overrides.add_argument(
|
||||||
|
"--override",
|
||||||
|
dest="config_override",
|
||||||
|
action="store",
|
||||||
|
type=json.loads,
|
||||||
|
nargs="?",
|
||||||
|
default={},
|
||||||
|
metavar="CONFIG_KV_PAIR",
|
||||||
|
help="""
|
||||||
|
Override configured key-value pairs with CONFIG_KV_PAIR for this command invocation only.
|
||||||
|
|
||||||
|
For example, to use a different editor for this jrnl entry, call:
|
||||||
|
jrnl --override '{"editor": "nano"}'
|
||||||
|
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
||||||
# Handle '-123' as a shortcut for '-n 123'
|
# Handle '-123' as a shortcut for '-n 123'
|
||||||
num = re.compile(r"^-(\d+)$")
|
num = re.compile(r"^-(\d+)$")
|
||||||
args = [num.sub(r"-n \1", arg) for arg in args]
|
args = [num.sub(r"-n \1", arg) for arg in args]
|
||||||
|
|
|
@ -35,6 +35,7 @@ def expected_args(**kwargs):
|
||||||
"strict": False,
|
"strict": False,
|
||||||
"tags": False,
|
"tags": False,
|
||||||
"text": [],
|
"text": [],
|
||||||
|
"config_override":{}
|
||||||
}
|
}
|
||||||
return {**default_args, **kwargs}
|
return {**default_args, **kwargs}
|
||||||
|
|
||||||
|
@ -204,6 +205,10 @@ def test_version_alone():
|
||||||
|
|
||||||
assert cli_as_dict("--version") == expected_args(preconfig_cmd=preconfig_version)
|
assert cli_as_dict("--version") == expected_args(preconfig_cmd=preconfig_version)
|
||||||
|
|
||||||
|
def test_editor_override():
|
||||||
|
from jrnl.commands import postconfig_override
|
||||||
|
|
||||||
|
assert cli_as_dict("--override '{\"editor\": \"nano\"}'") == expected_args(config_override={'editor':'nano'})
|
||||||
|
|
||||||
# @see https://github.com/jrnl-org/jrnl/issues/520
|
# @see https://github.com/jrnl-org/jrnl/issues/520
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
|
|
Loading…
Add table
Reference in a new issue