Convert abcfield.py from OptionParser to ArgumentParser.

abctemplate.py uses ArgumentParser, so this reduced dependencies.
This commit is contained in:
Jim Hague 2017-10-12 11:59:28 +01:00
parent fd98182250
commit 5622d5d239
1 changed files with 41 additions and 34 deletions

View File

@ -17,7 +17,7 @@
# break between it and the previous line. # break between it and the previous line.
# #
import optparse import argparse
import pathlib import pathlib
import re import re
import subprocess import subprocess
@ -239,17 +239,17 @@ def getFullTitle(lines, dir, starts = None, latex = False):
return title if len(subtitle) == 0 else title + " (" + subtitle + ")" return title if len(subtitle) == 0 else title + " (" + subtitle + ")"
if __name__ == "__main__": if __name__ == "__main__":
def process(f, dir, options): def process(f, dir, args):
lines = f.readlines() lines = f.readlines()
if options.display: if args.display:
if options.field.upper() == "FT": if args.field.upper() == "FT":
line = getFullTitle(lines, dir, options.starts, options.latex) line = getFullTitle(lines, dir, args.starts, args.latex)
else: 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: else:
if options.field.upper() == "FT": if args.field.upper() == "FT":
options.field = "T" args.field = "T"
line = getFieldText(lines, options.field, options.index, options.starts) line = getFieldText(lines, args.field, args.index, args.starts)
if line: if line:
print(line) print(line)
return True return True
@ -257,32 +257,39 @@ if __name__ == "__main__":
return False return False
# execute only if run as a script # execute only if run as a script
parser = optparse.OptionParser(usage="usage: %prog [options] [filename]\n\n" parser = argparse.ArgumentParser(description="Extract field data from ABC file.")
" Extract field data from ABC file.") parser.add_argument("-f", "--field", dest="field", default="T",
parser.add_option("-f", "--field", dest="field", default="T", help=("extract the given field [default: %(default)s]. "
help="extract the field FIELD", metavar="FIELD") "Field FT is special; it returns the full title "
parser.add_option("-l", "--latex", dest="latex", "- the title followed by subtitle in () if "
action="store_true", default=False, "present - for display text, or just the title "
help="convert special characters for LaTeX") "for non-display text."))
parser.add_option("-d", "--display", dest="display", parser.add_argument("-l", "--latex", dest="latex",
action="store_true", default=False, action="store_true", default=False,
help="convert to display text") help="convert special characters for LaTeX (default HTML)")
parser.add_option("-n", "--index", dest="index", parser.add_argument("-d", "--display", dest="display",
action="store", type="int", default=1, action="store_true", default=False,
help="report INDEXth value [default: %default]", help=("convert to display text. Convert accents to "
metavar="INDEX") "LaTeX or HTML, in titles convert 'Tune, The' to "
parser.add_option("-s", "--starts", dest="starts", "'The Tune', convert keys to full key name, "
action="store", type="string", default=None, "and expand Markdown in notes and history."))
help="report only if line starts CONTENT and remove CONTENT", parser.add_argument("-n", "--index", dest="index",
metavar="CONTENT") action="store", type=int, default=1,
(options, args) = parser.parse_args() 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 res = False
if len(args) > 0: if args.input:
for arg in args: path = pathlib.Path(args.input.name)
path = pathlib.Path(arg) with path.open() as f:
with path.open() as f: res = process(f, path.parent, args)
res = res or process(f, path.parent, options)
else: else:
res = process(sys.stdin, ".", options) res = process(sys.stdin, ".", args)
sys.exit(int(not res)) sys.exit(int(not res))