2019-06-12 05:41:31 +00:00
|
|
|
'''
|
2020-12-08 04:25:52 +00:00
|
|
|
Repeat the input as many times as you want.
|
2019-06-12 05:41:31 +00:00
|
|
|
|
|
|
|
> repeat "hello" 8
|
2021-09-24 06:42:34 +00:00
|
|
|
|
|
|
|
> repeat "yowza" inf
|
|
|
|
|
2019-06-12 05:41:31 +00:00
|
|
|
> echo hi | repeat !i 4
|
|
|
|
'''
|
2020-12-08 04:25:52 +00:00
|
|
|
import argparse
|
2019-06-12 05:41:31 +00:00
|
|
|
import sys
|
|
|
|
|
2020-12-08 04:25:52 +00:00
|
|
|
from voussoirkit import pipeable
|
|
|
|
|
2021-09-24 06:42:34 +00:00
|
|
|
def repeat_inf(text):
|
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
pipeable.stdout(text)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def repeat_times(text, times):
|
|
|
|
try:
|
|
|
|
times = int(times)
|
|
|
|
except ValueError:
|
|
|
|
pipeable.stderr('times should be an integer >= 1.')
|
|
|
|
return 1
|
|
|
|
|
|
|
|
if times < 1:
|
|
|
|
pipeable.stderr('times should be >= 1.')
|
|
|
|
return 1
|
|
|
|
|
|
|
|
try:
|
|
|
|
for t in range(times):
|
|
|
|
pipeable.stdout(text)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
return 1
|
|
|
|
|
2020-12-08 04:25:52 +00:00
|
|
|
def repeat_argparse(args):
|
2021-08-17 21:06:07 +00:00
|
|
|
text = pipeable.input(args.text, split_lines=False)
|
2020-12-08 04:25:52 +00:00
|
|
|
if args.times == 'inf':
|
2021-09-24 06:42:34 +00:00
|
|
|
return repeat_inf(text)
|
2020-12-08 04:25:52 +00:00
|
|
|
else:
|
2021-09-24 06:42:34 +00:00
|
|
|
return repeat_times(text, args.times)
|
2020-12-08 04:25:52 +00:00
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
|
|
|
|
|
|
parser.add_argument('text')
|
|
|
|
parser.add_argument('times')
|
|
|
|
parser.set_defaults(func=repeat_argparse)
|
2019-06-12 05:41:31 +00:00
|
|
|
|
2020-12-08 04:25:52 +00:00
|
|
|
args = parser.parse_args(argv)
|
|
|
|
return args.func(args)
|
2019-06-12 05:41:31 +00:00
|
|
|
|
2020-12-08 04:25:52 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
raise SystemExit(main(sys.argv[1:]))
|