Skip to content

Commit

Permalink
Initial 'stub' hello commands
Browse files Browse the repository at this point in the history
  • Loading branch information
pfnsec committed Jun 3, 2020
1 parent a706878 commit 5136109
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 0 deletions.
41 changes: 41 additions & 0 deletions batik/cli.py
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()
1 change: 1 addition & 0 deletions batik/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .hello import *
13 changes: 13 additions & 0 deletions batik/commands/base.py
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!')
14 changes: 14 additions & 0 deletions batik/commands/hello.py
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))
16 changes: 16 additions & 0 deletions tests/commands/test_hello.py
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)
22 changes: 22 additions & 0 deletions tests/test_cli.py
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)

0 comments on commit 5136109

Please sign in to comment.