Mercurial > dottes
diff abcfield.py @ 733:841f7c19cb55
Convert abcfield.py from OptionParser to ArgumentParser.
abctemplate.py uses ArgumentParser, so this reduced dependencies.
author | Jim Hague <jim.hague@acm.org> |
---|---|
date | Thu, 12 Oct 2017 11:59:28 +0100 |
parents | 9b562923ac71 |
children | e896cf93fe98 |
line wrap: on
line diff
--- a/abcfield.py Thu Oct 12 11:21:48 2017 +0100 +++ b/abcfield.py Thu Oct 12 11:59:28 2017 +0100 @@ -17,7 +17,7 @@ # break between it and the previous line. # -import optparse +import argparse import pathlib import re import subprocess @@ -239,17 +239,17 @@ return title if len(subtitle) == 0 else title + " (" + subtitle + ")" if __name__ == "__main__": - def process(f, dir, options): + def process(f, dir, args): lines = f.readlines() - if options.display: - if options.field.upper() == "FT": - line = getFullTitle(lines, dir, options.starts, options.latex) + if args.display: + if args.field.upper() == "FT": + line = getFullTitle(lines, dir, args.starts, args.latex) else: - line = getFieldDisplayText(lines, dir, options.field, options.index, options.starts, options.latex) + line = getFieldDisplayText(lines, dir, args.field, args.index, args.starts, args.latex) else: - if options.field.upper() == "FT": - options.field = "T" - line = getFieldText(lines, options.field, options.index, options.starts) + if args.field.upper() == "FT": + args.field = "T" + line = getFieldText(lines, args.field, args.index, args.starts) if line: print(line) return True @@ -257,32 +257,39 @@ return False # execute only if run as a script - parser = optparse.OptionParser(usage="usage: %prog [options] [filename]\n\n" - " Extract field data from ABC file.") - parser.add_option("-f", "--field", dest="field", default="T", - help="extract the field FIELD", metavar="FIELD") - parser.add_option("-l", "--latex", dest="latex", - action="store_true", default=False, - help="convert special characters for LaTeX") - parser.add_option("-d", "--display", dest="display", - action="store_true", default=False, - help="convert to display text") - parser.add_option("-n", "--index", dest="index", - action="store", type="int", default=1, - help="report INDEXth value [default: %default]", - metavar="INDEX") - parser.add_option("-s", "--starts", dest="starts", - action="store", type="string", default=None, - help="report only if line starts CONTENT and remove CONTENT", - metavar="CONTENT") - (options, args) = parser.parse_args() + parser = argparse.ArgumentParser(description="Extract field data from ABC file.") + parser.add_argument("-f", "--field", dest="field", default="T", + help=("extract the given field [default: %(default)s]. " + "Field FT is special; it returns the full title " + "- the title followed by subtitle in () if " + "present - for display text, or just the title " + "for non-display text.")) + parser.add_argument("-l", "--latex", dest="latex", + action="store_true", default=False, + help="convert special characters for LaTeX (default HTML)") + parser.add_argument("-d", "--display", dest="display", + action="store_true", default=False, + help=("convert to display text. Convert accents to " + "LaTeX or HTML, in titles convert 'Tune, The' to " + "'The Tune', convert keys to full key name, " + "and expand Markdown in notes and history.")) + parser.add_argument("-n", "--index", dest="index", + action="store", type=int, default=1, + help="report INDEXth value [default: %(default)s]") + parser.add_argument("-s", "--starts", dest="starts", + action="store", default=None, + help=("report only if line starts with CONTENT " + "and remove CONTENT"), + metavar="CONTENT") + parser.add_argument('input', type=argparse.FileType('r'), + help='input ABC file') + args = parser.parse_args() res = False - if len(args) > 0: - for arg in args: - path = pathlib.Path(arg) - with path.open() as f: - res = res or process(f, path.parent, options) + if args.input: + path = pathlib.Path(args.input.name) + with path.open() as f: + res = process(f, path.parent, args) else: - res = process(sys.stdin, ".", options) + res = process(sys.stdin, ".", args) sys.exit(int(not res))