feat: operator arguments

This commit is contained in:
Johann Dreo 2026-04-05 11:57:14 +02:00
commit 20d0424930

View file

@ -1,5 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
import re
import sys import sys
import locale import locale
import logging import logging
@ -42,8 +43,8 @@ class format:
return items return items
class trim(Format): class trim(Format):
def __init__(self, max = 14): def __init__(self, max = "14"):
self.max = max self.max = int(max)
def trim(self, line): def trim(self, line):
if len(line) > self.max: if len(line) > self.max:
@ -158,6 +159,23 @@ def classes_of(namespace):
subs = {cls.__name__:cls for cls in itf.__subclasses__()} subs = {cls.__name__:cls for cls in itf.__subclasses__()}
return subs return subs
def operator(asked_op):
logger.debug(f"├ Parsed operators:")
for op in asked_op:
# We do not use a f(a) notation,
# because the shell would try to interpret
# any unescaped/unquoted parentheses.
# It is faster to use f:a for the user.
m = re.match("(\w+):(.+)", op)
if m:
name = m.group(1)
args = [a.strip() for a in m.group(2).split(',')]
else:
name = op
args = []
logger.debug(f"│ ├ {name}({','.join(args)})")
yield name,args
logger.debug("│ └OK")
def main(): def main():
errors = {"NO_ERROR":0, "UNKNOWN_ERROR":1, "NO_SETUP_NEEDED":2, "NO_APP_KEY":10} errors = {"NO_ERROR":0, "UNKNOWN_ERROR":1, "NO_SETUP_NEEDED":2, "NO_APP_KEY":10}
@ -169,31 +187,31 @@ def main():
streamers = classes_of(stream) streamers = classes_of(stream)
parser.add_argument("-s", "--stream", parser.add_argument("-s", "--stream",
choices = streamers.keys(), # choices = streamers.keys(),
# metavar = "STREAMER(S)", metavar = '{' + ','.join(streamers) + '}',
default = [], default = [],
action="append", action="append",
help="Where to get items.") help="Where to get items.")
consumers = classes_of(consume) consumers = classes_of(consume)
parser.add_argument("-c", "--consume", parser.add_argument("-c", "--consume",
choices = consumers.keys(), # choices = consumers.keys(),
# metavar = "CONSUMER", metavar = '{' + ','.join(consumers) + '}',
default = "lines", default = "lines",
help="How to get the content form items.") help="How to get the content form items.")
formaters = classes_of(format) formaters = classes_of(format)
parser.add_argument("-f", "--format", parser.add_argument("-f", "--format",
choices = formaters.keys(), # choices = formaters.keys(),
# metavar = "FORMATER(S)", metavar = '{' + ','.join(formaters) + '}',
default = [], default = [],
action="append", action="append",
help="How to format items.") help="How to format items.")
lifters = classes_of(lift) lifters = classes_of(lift)
parser.add_argument("-l", "--lift", parser.add_argument("-l", "--lift",
choices = lifters.keys(), # choices = lifters.keys(),
# metavar = "LIFTER(S)", metavar = '{' + ','.join(lifters) + '}',
default = [], default = [],
action="append", action="append",
help="How to lift items.") help="How to lift items.")
@ -210,6 +228,8 @@ def main():
logger.debug(f"├ formaters: {', '.join(formaters)}") logger.debug(f"├ formaters: {', '.join(formaters)}")
logger.debug(f"└ lifters: {', '.join(lifters)}") logger.debug(f"└ lifters: {', '.join(lifters)}")
# Sane defaults. Cannot be a default in argparse,
# because choices would be append to it.
if not asked.stream: if not asked.stream:
asked.stream = ["stdin"] asked.stream = ["stdin"]
@ -222,9 +242,9 @@ def main():
logger.debug("Chosen operators:") logger.debug("Chosen operators:")
forthlift = Forthlifter( forthlift = Forthlifter(
consumer = consumers[asked.consume](), consumer = consumers[asked.consume](),
streamers = [streamers[k]() for k in asked.stream], streamers = [streamers[op](*args) for op,args in operator(asked.stream)],
formatters = [formaters[k]() for k in asked.format], formatters = [formaters[op](*args) for op,args in operator(asked.format)],
lifters = [ lifters[k]() for k in asked.lift ], lifters = [ lifters[op](*args) for op,args in operator(asked.lift )],
) )
logger.debug("Run:") logger.debug("Run:")
forthlift() forthlift()