Skip to content

Commit 0be616b

Browse files
committed
add a command line tool for this project.
1 parent 1adcd59 commit 0be616b

3 files changed

Lines changed: 39 additions & 0 deletions

File tree

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def get_version():
4040
':python_version=="2.6"': ['ordereddict>=1.1'],
4141
'develop': ['regex', 'pytest', 'pytest-cov'],
4242
},
43+
entry_points={'console_scripts': ['textile=textile.__main__:main']},
4344
setup_requires=['pytest-runner'],
4445
tests_require=['pytest', 'pytest-cov'],
4546
include_package_data=True,

tests/test_cli.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import subprocess
2+
3+
def test_console_script():
4+
result = subprocess.check_output(['python', '-m', 'textile', 'README.textile'])
5+
with open('tests/fixtures/README.txt') as f:
6+
expect = ''.join(f.readlines())
7+
assert result == expect

textile/__main__.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import argparse
2+
import sys
3+
import textile
4+
5+
6+
def main():
7+
"""A CLI tool in the style of python's json.tool. In fact, this is mostly
8+
copied directly from that module. This allows us to create a stand-alone
9+
tool as well as invoking it via `python -m textile`."""
10+
prog = 'textile'
11+
description = ('A simple command line interface for textile module '
12+
'to convert textile input to HTML output. This script '
13+
'accepts input as a file or stdin and can write out to '
14+
'a file or stdout.')
15+
parser = argparse.ArgumentParser(prog=prog, description=description)
16+
parser.add_argument('infile', nargs='?', type=argparse.FileType(),
17+
help='a textile file to be converted')
18+
parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
19+
help='write the output of infile to outfile')
20+
options = parser.parse_args()
21+
22+
infile = options.infile or sys.stdin
23+
outfile = options.outfile or sys.stdout
24+
with infile:
25+
output = textile.textile(''.join(infile.readlines()))
26+
with outfile:
27+
outfile.write(output)
28+
29+
30+
if __name__ == '__main__': #pragma: no cover
31+
main()

0 commit comments

Comments
 (0)