diff --git a/abcfirstline.py b/abcfirstline.py index f83a2c8..04586c2 100755 --- a/abcfirstline.py +++ b/abcfirstline.py @@ -4,6 +4,8 @@ # to print the first line of the music only. # +import argparse +import pathlib import sys def process(inf): @@ -21,21 +23,20 @@ def process(inf): if len(start) > 1 and start[1] == ":" and (start[0].isalpha() or start[0] == '+'): if start[0] in ["M", "K", "L"]: print(line) - # Output line. If it is a continuation, output at most one - # continuation. + # Output line. If the line ends with a continuation, carry + # on outputting lines, otherwise stop. else: print(line) - if continued or line[-1] != "\\": + if line[-1] != "\\": break - else: - continued = True -if len(sys.argv) > 1: - for arg in sys.argv[1:]: - try: - inf = open(arg, "r") - process(inf) - finally: - inf.close() -else: - process(sys.stdin) +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Write minimal version of ABC input file with just the first line of music.") + parser.add_argument('input', type=argparse.FileType('r'), + help='input ABC file') + args = parser.parse_args() + + path = pathlib.Path(args.input.name) + with path.open() as f: + process(f) + sys.exit(0)