-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
""" | ||
batik | ||
Usage: | ||
batik hello | ||
batik -h | --help | ||
batik --version | ||
Options: | ||
-h --help Show this screen. | ||
--version Show version. | ||
Examples: | ||
batik hello | ||
Help: | ||
For help using this tool, please open an issue on the Github repository: | ||
""" | ||
|
||
|
||
from inspect import getmembers, isclass | ||
|
||
from docopt import docopt | ||
|
||
from . import __version__ as VERSION | ||
|
||
|
||
def main(): | ||
"""Main CLI entrypoint.""" | ||
import batik.commands | ||
options = docopt(__doc__, version=VERSION) | ||
|
||
# Here we'll try to dynamically match the command the user is trying to run | ||
# with a pre-defined command class we've already created. | ||
for (k, v) in options.items(): | ||
if hasattr(batik.commands, k) and v: | ||
module = getattr(batik.commands, k) | ||
batik.commands = getmembers(module, isclass) | ||
command = [command[1] for command in batik.commands if command[0] != 'Base'][0] | ||
command = command(options) | ||
command.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .hello import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
"""The base command.""" | ||
|
||
|
||
class Base(object): | ||
"""A base command.""" | ||
|
||
def __init__(self, options, *args, **kwargs): | ||
self.options = options | ||
self.args = args | ||
self.kwargs = kwargs | ||
|
||
def run(self): | ||
raise NotImplementedError('You must implement the run() method yourself!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"""The hello command.""" | ||
|
||
|
||
from json import dumps | ||
|
||
from .base import Base | ||
|
||
|
||
class Hello(Base): | ||
"""Say hello, world!""" | ||
|
||
def run(self): | ||
print('Hello, world!') | ||
print('You supplied the following options:', dumps(self.options, indent=2, sort_keys=True)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
"""Tests for our `batik hello` subcommand.""" | ||
|
||
|
||
from subprocess import PIPE, Popen as popen | ||
from unittest import TestCase | ||
|
||
|
||
class TestHello(TestCase): | ||
def test_returns_multiple_lines(self): | ||
output = popen(['batik', 'hello'], stdout=PIPE).communicate()[0] | ||
lines = output.split('\n') | ||
self.assertTrue(len(lines) != 1) | ||
|
||
def test_returns_hello_world(self): | ||
output = popen(['batik', 'hello'], stdout=PIPE).communicate()[0] | ||
self.assertTrue('Hello, world!' in output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"""Tests for our main batik CLI module.""" | ||
|
||
|
||
from subprocess import PIPE, Popen as popen | ||
from unittest import TestCase | ||
|
||
from batik import __version__ as VERSION | ||
|
||
|
||
class TestHelp(TestCase): | ||
def test_returns_usage_information(self): | ||
output = popen(['batik', '-h'], stdout=PIPE).communicate()[0] | ||
self.assertTrue('Usage:' in output) | ||
|
||
output = popen(['batik', '--help'], stdout=PIPE).communicate()[0] | ||
self.assertTrue('Usage:' in output) | ||
|
||
|
||
class TestVersion(TestCase): | ||
def test_returns_version_information(self): | ||
output = popen(['batik', '--version'], stdout=PIPE).communicate()[0] | ||
self.assertEqual(output.strip(), VERSION) |