From 23cc43dc15c24d7632fac851e231a63a57516b35 Mon Sep 17 00:00:00 2001 From: Kamil Rakoczy Date: Tue, 27 Oct 2020 16:08:29 +0100 Subject: [PATCH] Use python-fire for command-line parsing Signed-off-by: Kamil Rakoczy --- bin/tuttest | 47 +++++++++++++++++++++++++++-------------------- setup.py | 2 +- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/bin/tuttest b/bin/tuttest index 521a4e0..b93c15c 100755 --- a/bin/tuttest +++ b/bin/tuttest @@ -1,28 +1,29 @@ #!/usr/bin/env python3 from tuttest import get_snippets - -if __name__ == "__main__": - import sys - import argparse - - parser = argparse.ArgumentParser(description='A tutorial tester script. Extract the code blocks from tutorial and see if, when followed, the tutorial actually works.') - - parser.add_argument('filename', metavar='filename', type=str, help='filename with tutorial') - parser.add_argument('commands', metavar='commands', nargs='?', type=str, help='optional names to give to the extracted snippets, provided as list') - - parser.add_argument('--prefix-lines-with', metavar='prefix', type=str, help='string to prefix each command with') - parser.add_argument('--single-command', action='store_true', help='executes all snippets in single command') - - args = parser.parse_args() +import fire + +def tuttest(filename, commands = None, single_command = None, prefix_lines_with = None): + ''' + Dishes up the appropriate amount of pizza. + Args: + filename : string + filename with tutorial + commands : string + optional names to give to the extracted snippets, provided as list + single_command : boolean + executes all snippets in single command + prefix_lines_with : string + string to prefix each command with + ''' + snippets = get_snippets(filename) code = [] - snippets = get_snippets(args.filename) - if not args.commands: + if not commands: for s in snippets: code.append(snippets[s].text.strip()) else: - commands = args.commands.split(',') + commands = commands.split(',') for c in commands: if c in snippets: @@ -39,13 +40,13 @@ if __name__ == "__main__": # no match, add ad hoc code as separate line code.append(c.strip()) - if args.single_command: + if single_command: code = [';'.join(code)] - if args.prefix_lines_with: + if prefix_lines_with: prefixed_code = [] for snippet in code: - prefixed_snippet = args.prefix_lines_with + " \'" + prefixed_snippet = prefix_lines_with + " \'" for line in snippet.splitlines(): if len(line.strip()): # skip empty lines prefixed_snippet += line + ";" @@ -54,3 +55,9 @@ if __name__ == "__main__": code = prefixed_code print('\n\n'.join(code)) + +if __name__ == "__main__": + import sys + + fire.Fire(tuttest) + diff --git a/setup.py b/setup.py index 2d7fead..73cdf87 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ author='Antmicro', author_email='mgielda@antmicro.com', install_requires=[ - 'docutils', 'pygments' + 'docutils', 'pygments', 'fire' ], license='MIT', scripts=['bin/tuttest'],