Skip to content

Commit

Permalink
Use python-fire for command-line parsing
Browse files Browse the repository at this point in the history
Signed-off-by: Kamil Rakoczy <[email protected]>
  • Loading branch information
kamilrakoczy committed Oct 27, 2020
1 parent 12ba1cf commit 23cc43d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
47 changes: 27 additions & 20 deletions bin/tuttest
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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 + ";"
Expand All @@ -54,3 +55,9 @@ if __name__ == "__main__":
code = prefixed_code
print('\n\n'.join(code))


if __name__ == "__main__":
import sys

fire.Fire(tuttest)

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
author='Antmicro',
author_email='[email protected]',
install_requires=[
'docutils', 'pygments'
'docutils', 'pygments', 'fire'
],
license='MIT',
scripts=['bin/tuttest'],
Expand Down

0 comments on commit 23cc43d

Please sign in to comment.