Mercurial > dottes
comparison 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 |
comparison
equal
deleted
inserted
replaced
732:c81a1ed21877 | 733:841f7c19cb55 |
---|---|
15 # convention of '<header>:+' *also* being a continuation. Note that a | 15 # convention of '<header>:+' *also* being a continuation. Note that a |
16 # continuation is a distinct line in the field value; the value has a line | 16 # continuation is a distinct line in the field value; the value has a line |
17 # break between it and the previous line. | 17 # break between it and the previous line. |
18 # | 18 # |
19 | 19 |
20 import optparse | 20 import argparse |
21 import pathlib | 21 import pathlib |
22 import re | 22 import re |
23 import subprocess | 23 import subprocess |
24 import sys | 24 import sys |
25 | 25 |
237 title = getFieldDisplayText(lines, dir, "T", starts=starts, latex=latex) | 237 title = getFieldDisplayText(lines, dir, "T", starts=starts, latex=latex) |
238 subtitle = getFieldDisplayText(lines, dir, "T", n=2, starts=starts, latex=latex) | 238 subtitle = getFieldDisplayText(lines, dir, "T", n=2, starts=starts, latex=latex) |
239 return title if len(subtitle) == 0 else title + " (" + subtitle + ")" | 239 return title if len(subtitle) == 0 else title + " (" + subtitle + ")" |
240 | 240 |
241 if __name__ == "__main__": | 241 if __name__ == "__main__": |
242 def process(f, dir, options): | 242 def process(f, dir, args): |
243 lines = f.readlines() | 243 lines = f.readlines() |
244 if options.display: | 244 if args.display: |
245 if options.field.upper() == "FT": | 245 if args.field.upper() == "FT": |
246 line = getFullTitle(lines, dir, options.starts, options.latex) | 246 line = getFullTitle(lines, dir, args.starts, args.latex) |
247 else: | 247 else: |
248 line = getFieldDisplayText(lines, dir, options.field, options.index, options.starts, options.latex) | 248 line = getFieldDisplayText(lines, dir, args.field, args.index, args.starts, args.latex) |
249 else: | 249 else: |
250 if options.field.upper() == "FT": | 250 if args.field.upper() == "FT": |
251 options.field = "T" | 251 args.field = "T" |
252 line = getFieldText(lines, options.field, options.index, options.starts) | 252 line = getFieldText(lines, args.field, args.index, args.starts) |
253 if line: | 253 if line: |
254 print(line) | 254 print(line) |
255 return True | 255 return True |
256 else: | 256 else: |
257 return False | 257 return False |
258 | 258 |
259 # execute only if run as a script | 259 # execute only if run as a script |
260 parser = optparse.OptionParser(usage="usage: %prog [options] [filename]\n\n" | 260 parser = argparse.ArgumentParser(description="Extract field data from ABC file.") |
261 " Extract field data from ABC file.") | 261 parser.add_argument("-f", "--field", dest="field", default="T", |
262 parser.add_option("-f", "--field", dest="field", default="T", | 262 help=("extract the given field [default: %(default)s]. " |
263 help="extract the field FIELD", metavar="FIELD") | 263 "Field FT is special; it returns the full title " |
264 parser.add_option("-l", "--latex", dest="latex", | 264 "- the title followed by subtitle in () if " |
265 action="store_true", default=False, | 265 "present - for display text, or just the title " |
266 help="convert special characters for LaTeX") | 266 "for non-display text.")) |
267 parser.add_option("-d", "--display", dest="display", | 267 parser.add_argument("-l", "--latex", dest="latex", |
268 action="store_true", default=False, | 268 action="store_true", default=False, |
269 help="convert to display text") | 269 help="convert special characters for LaTeX (default HTML)") |
270 parser.add_option("-n", "--index", dest="index", | 270 parser.add_argument("-d", "--display", dest="display", |
271 action="store", type="int", default=1, | 271 action="store_true", default=False, |
272 help="report INDEXth value [default: %default]", | 272 help=("convert to display text. Convert accents to " |
273 metavar="INDEX") | 273 "LaTeX or HTML, in titles convert 'Tune, The' to " |
274 parser.add_option("-s", "--starts", dest="starts", | 274 "'The Tune', convert keys to full key name, " |
275 action="store", type="string", default=None, | 275 "and expand Markdown in notes and history.")) |
276 help="report only if line starts CONTENT and remove CONTENT", | 276 parser.add_argument("-n", "--index", dest="index", |
277 metavar="CONTENT") | 277 action="store", type=int, default=1, |
278 (options, args) = parser.parse_args() | 278 help="report INDEXth value [default: %(default)s]") |
279 parser.add_argument("-s", "--starts", dest="starts", | |
280 action="store", default=None, | |
281 help=("report only if line starts with CONTENT " | |
282 "and remove CONTENT"), | |
283 metavar="CONTENT") | |
284 parser.add_argument('input', type=argparse.FileType('r'), | |
285 help='input ABC file') | |
286 args = parser.parse_args() | |
279 | 287 |
280 res = False | 288 res = False |
281 if len(args) > 0: | 289 if args.input: |
282 for arg in args: | 290 path = pathlib.Path(args.input.name) |
283 path = pathlib.Path(arg) | 291 with path.open() as f: |
284 with path.open() as f: | 292 res = process(f, path.parent, args) |
285 res = res or process(f, path.parent, options) | 293 else: |
286 else: | 294 res = process(sys.stdin, ".", args) |
287 res = process(sys.stdin, ".", options) | |
288 sys.exit(int(not res)) | 295 sys.exit(int(not res)) |