Convert abcfield.py from OptionParser to ArgumentParser.
abctemplate.py uses ArgumentParser, so this reduced dependencies.
This commit is contained in:
parent
fd98182250
commit
5622d5d239
67
abcfield.py
67
abcfield.py
|
@ -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 "
|
||||||
|
"present - for display text, or just the title "
|
||||||
|
"for non-display text."))
|
||||||
|
parser.add_argument("-l", "--latex", dest="latex",
|
||||||
action="store_true", default=False,
|
action="store_true", default=False,
|
||||||
help="convert special characters for LaTeX")
|
help="convert special characters for LaTeX (default HTML)")
|
||||||
parser.add_option("-d", "--display", dest="display",
|
parser.add_argument("-d", "--display", dest="display",
|
||||||
action="store_true", default=False,
|
action="store_true", default=False,
|
||||||
help="convert to display text")
|
help=("convert to display text. Convert accents to "
|
||||||
parser.add_option("-n", "--index", dest="index",
|
"LaTeX or HTML, in titles convert 'Tune, The' to "
|
||||||
action="store", type="int", default=1,
|
"'The Tune', convert keys to full key name, "
|
||||||
help="report INDEXth value [default: %default]",
|
"and expand Markdown in notes and history."))
|
||||||
metavar="INDEX")
|
parser.add_argument("-n", "--index", dest="index",
|
||||||
parser.add_option("-s", "--starts", dest="starts",
|
action="store", type=int, default=1,
|
||||||
action="store", type="string", default=None,
|
help="report INDEXth value [default: %(default)s]")
|
||||||
help="report only if line starts CONTENT and remove CONTENT",
|
parser.add_argument("-s", "--starts", dest="starts",
|
||||||
|
action="store", default=None,
|
||||||
|
help=("report only if line starts with CONTENT "
|
||||||
|
"and remove CONTENT"),
|
||||||
metavar="CONTENT")
|
metavar="CONTENT")
|
||||||
(options, args) = parser.parse_args()
|
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 = res or process(f, path.parent, options)
|
res = process(f, path.parent, args)
|
||||||
else:
|
else:
|
||||||
res = process(sys.stdin, ".", options)
|
res = process(sys.stdin, ".", args)
|
||||||
sys.exit(int(not res))
|
sys.exit(int(not res))
|
||||||
|
|
Loading…
Reference in New Issue