From 44271a56c497d486ddefae58723723f01fe149b8 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Mon, 3 May 2021 18:55:12 -0700 Subject: [PATCH] Add argparse, betterhelp to bytestring. --- voussoirkit/bytestring.py | 40 +++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/voussoirkit/bytestring.py b/voussoirkit/bytestring.py index 541f82a..fc028d6 100644 --- a/voussoirkit/bytestring.py +++ b/voussoirkit/bytestring.py @@ -1,10 +1,29 @@ ''' +bytestring +========== + This module provides integer constants for power-of-two byte size units, and -functions for converting between ints and human-readable strings e.g. "1.2 GiB". +functions for converting between ints and human-readable strings. E.g.: +bytestring.bytestring(5000000) -> '4.768 MiB' +bytestring.parsebytes('8.5gb') -> 9126805504 + +Commandline usage: + +> bytestring number1 [number2 number3...] + +number: + An integer. Uses pipeable to support !c clipboard, !i stdin, which should + be one number per line. + +Examples: +> bytestring 12345 89989 +> some_process | bytestring !i ''' +import argparse import re import sys +from voussoirkit import betterhelp from voussoirkit import pipeable BYTE = 1 @@ -150,10 +169,23 @@ def parsebytes(string): return int(number * multiplier) +def bytestring_argparse(args): + numbers = pipeable.input_many(args.numbers) + for number in numbers: + try: + number = int(number) + except ValueError: + pipeable.stderr(f'bytestring: Each number should be an integer, not {number}.') + return 1 + pipeable.stdout(bytestring(number)) + def main(argv): - for line in pipeable.go(argv, strip=True, skip_blank=True): - n = int(line) - pipeable.output(bytestring(n)) + parser = argparse.ArgumentParser(description=__doc__) + + parser.add_argument('numbers', nargs='+') + parser.set_defaults(func=bytestring_argparse) + + return betterhelp.single_main(argv, parser, __doc__) if __name__ == '__main__': raise SystemExit(main(sys.argv[1:]))