annotate fontaine/cli.py @ 10:2cea36073188

Move core CLI tool code into the package.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 04 Jan 2017 08:46:27 -0800
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
2cea36073188 Move core CLI tool code into the package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import sys
2cea36073188 Move core CLI tool code into the package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import argparse
2cea36073188 Move core CLI tool code into the package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3
2cea36073188 Move core CLI tool code into the package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4
2cea36073188 Move core CLI tool code into the package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 def main():
2cea36073188 Move core CLI tool code into the package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6 parser = argparse.ArgumentParser(
2cea36073188 Move core CLI tool code into the package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 description='Fontaine command line utility')
2cea36073188 Move core CLI tool code into the package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 parser.add_argument('script')
2cea36073188 Move core CLI tool code into the package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 parser.add_argument('out_file', nargs='?')
2cea36073188 Move core CLI tool code into the package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 args = parser.parse_args()
2cea36073188 Move core CLI tool code into the package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11
2cea36073188 Move core CLI tool code into the package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 from fontaine.parser import FontaineParser
2cea36073188 Move core CLI tool code into the package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13 p = FontaineParser()
2cea36073188 Move core CLI tool code into the package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 doc = p.parse(args.script)
2cea36073188 Move core CLI tool code into the package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15
2cea36073188 Move core CLI tool code into the package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 if not args.out_file:
2cea36073188 Move core CLI tool code into the package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17 from fontaine.console import ConsoleDocumentRenderer
2cea36073188 Move core CLI tool code into the package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 rdr = ConsoleDocumentRenderer()
2cea36073188 Move core CLI tool code into the package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 rdr.render_doc(doc, sys.stdout)
2cea36073188 Move core CLI tool code into the package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 else:
2cea36073188 Move core CLI tool code into the package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 from fontaine.html import HtmlDocumentRenderer
2cea36073188 Move core CLI tool code into the package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 rdr = HtmlDocumentRenderer()
2cea36073188 Move core CLI tool code into the package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23 with open(args.out_file, 'w') as fp:
2cea36073188 Move core CLI tool code into the package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 rdr.render_doc(doc, fp)