Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use python-fire for command-line parsing #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions bin/tuttest
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
#!/usr/bin/env python3

from tuttest import get_snippets
import fire

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')
def tuttest(filename: str, commands: str = None, single_command: bool = None, prefix_lines_with: str = None):
'''
Tuttest is a utility package that simplifies tutorial and example testing. It provides an interface for extracting code snippets embedded in RST and Markdown files.

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()
Args:
filename : filename with tutorial
commands : optional names to give to the extracted snippets,
provided as list
single_command : executes all snippets in a single command
prefix_lines_with : 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 +38,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 +53,6 @@ if __name__ == "__main__":
code = prefixed_code
print('\n\n'.join(code))

if __name__ == "__main__":
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