from argparse import ArgumentParser from igitar.base import Song from igitar.renderers.html import render as html_render, get_options as html_get_options from igitar.renderers.text import render as text_render, get_options as text_get_options def get_args(): """Parse the command line arguments""" parser = ArgumentParser() subparsers = parser.add_subparsers(title='commands', dest='command') subparsers.add_parser('list', aliases=['list-commands', 'l'], help='List the commands available') subparsers.add_parser('html', aliases=['html-params', 'h'], help='List the parameters available for HTML rendering') subparsers.add_parser('text', aliases=['text-params', 't'], help='List the parameters available for text rendering') parser_render = subparsers.add_parser('render', aliases=['r'], help='Render a ChordPro file to HTML or text') parser_render.add_argument('input', metavar='FILENAME', help='Input ChordPro file') parser_render.add_argument('-o', '--output', metavar='FILENAME', help='Output to a file') parser_render.add_argument('-f', '--format', metavar='FORMAT', choices=['html', 'text'], default='html', help='Output format') parser_render.add_argument('-p', '--param', metavar='KEY=VALUE', action='append', help='Parameter to send to renderer') return parser.parse_args() def main(): """The command line entrypoint""" args = get_args() if args.command in ['list', 'list-commands', 'l']: print('Commands') print('') print(' list List all available commands') print(' render Render a ChordPro file to HTML or text') print(' html List the parameters available for HTML rendering') print(' text List the parameters available for text rendering') elif args.command in ['html', 'html-params', 'h']: print('HTML parameters') print('') for param, details in html_get_options().items(): print(' {:<12} {}'.format(param, details['description'])) elif args.command in ['text', 'text-params', 't']: print('Text parameters') print('') for param, details in text_get_options().items(): print(' {:<12} {}'.format(param, details['description'])) elif args.command == 'render': render_params = {kv.split('=')[0]: kv.split('=')[-1] for kv in args.param} if args.param else {} # Prepare the params for key, value in render_params.items(): if value.lower() == "true": render_params[key] = True elif value.lower() == "false": render_params[key] = False elif value.isdigit(): render_params[key] = int(value) song = Song(args.input) if args.format == 'text': output = text_render(song, options=render_params) else: output = html_render(song, options=render_params) if args.output: with open(args.output, 'w') as output_file: output_file.write(output) else: print(output)