[mod] main:args:template_path
This commit is contained in:
parent
207a2b2a89
commit
75dcafc58c
|
|
@ -72,9 +72,7 @@ def handle_value(
|
||||||
)
|
)
|
||||||
return (
|
return (
|
||||||
file_read(value[len(options["indicator"]):])
|
file_read(value[len(options["indicator"]):])
|
||||||
if
|
if value.startswith(options["indicator"]) else
|
||||||
value.startswith(options["indicator"])
|
|
||||||
else
|
|
||||||
value
|
value
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -91,9 +89,7 @@ def data_from_file(
|
||||||
(
|
(
|
||||||
lambda content: (
|
lambda content: (
|
||||||
_json.loads(content)
|
_json.loads(content)
|
||||||
if
|
if path.endswith(".json") else
|
||||||
path.endswith(".json")
|
|
||||||
else
|
|
||||||
_yaml.safe_load(content)
|
_yaml.safe_load(content)
|
||||||
)
|
)
|
||||||
) (file_read(path))
|
) (file_read(path))
|
||||||
|
|
@ -101,7 +97,7 @@ def data_from_file(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def data_from_arguments(
|
def data_from_adhoc_arguments(
|
||||||
arguments
|
arguments
|
||||||
):
|
):
|
||||||
return dict(
|
return dict(
|
||||||
|
|
@ -125,7 +121,16 @@ def main(
|
||||||
argument_parser = _argparse.ArgumentParser(
|
argument_parser = _argparse.ArgumentParser(
|
||||||
prog = "coin",
|
prog = "coin",
|
||||||
formatter_class = _argparse.ArgumentDefaultsHelpFormatter,
|
formatter_class = _argparse.ArgumentDefaultsHelpFormatter,
|
||||||
description = "transforms an input string (stdin) by processing its placeholders; example: »echo '{{flowers}} are {{color}}' | coin -a flowers:roses -a color:red«",
|
description = "transforms a template string to its refined version by substituting its placeholders with concrete values; example: »echo '{{flowers}} are {{color}}' | coin -a flowers:roses -a color:red«",
|
||||||
|
)
|
||||||
|
argument_parser.add_argument(
|
||||||
|
"-t",
|
||||||
|
"--template-path",
|
||||||
|
dest = "template_path",
|
||||||
|
type = str,
|
||||||
|
metavar = "<template-path>",
|
||||||
|
default = None,
|
||||||
|
help = "default: read from stdin",
|
||||||
)
|
)
|
||||||
argument_parser.add_argument(
|
argument_parser.add_argument(
|
||||||
"-d",
|
"-d",
|
||||||
|
|
@ -137,12 +142,11 @@ def main(
|
||||||
)
|
)
|
||||||
argument_parser.add_argument(
|
argument_parser.add_argument(
|
||||||
"-a",
|
"-a",
|
||||||
"--argument",
|
"--adhoc-argument",
|
||||||
type = str,
|
type = str,
|
||||||
dest = "arguments",
|
dest = "adhoc_arguments",
|
||||||
action = "append",
|
action = "append",
|
||||||
default = None,
|
metavar = "<adhoc-argument-key>:<adhoc-argument-value>",
|
||||||
metavar = "<key>:<value>",
|
|
||||||
help = "prefixing <value> with '@' causes the value to be interpreted as the path to a text file, whose content shall be used",
|
help = "prefixing <value> with '@' causes the value to be interpreted as the path to a text file, whose content shall be used",
|
||||||
)
|
)
|
||||||
argument_parser.add_argument(
|
argument_parser.add_argument(
|
||||||
|
|
@ -170,11 +174,15 @@ def main(
|
||||||
args = argument_parser.parse_args()
|
args = argument_parser.parse_args()
|
||||||
|
|
||||||
## exec
|
## exec
|
||||||
content_in = _sys.stdin.read()
|
content_in = (
|
||||||
|
_sys.stdin.read()
|
||||||
|
if (args.template_path is None) else
|
||||||
|
file_read(args.template_path)
|
||||||
|
)
|
||||||
data = (
|
data = (
|
||||||
data_from_file(args.data_path)
|
data_from_file(args.data_path)
|
||||||
|
|
|
|
||||||
data_from_arguments(args.arguments)
|
data_from_adhoc_arguments(args.adhoc_arguments)
|
||||||
)
|
)
|
||||||
content_out = string_coin(
|
content_out = string_coin(
|
||||||
content_in,
|
content_in,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue