Skip to content

Commit ca9218e

Browse files
authored
Merge pull request #99 from manu-chroma/master
schema_salad/main.py: --version now correctly prints version
2 parents 61c2203 + a63f75f commit ca9218e

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

schema_salad/main.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ def main(argsl=None): # type: (List[str]) -> int
6262
"--print-index", action="store_true", help="Print node index")
6363
exgroup.add_argument("--print-metadata",
6464
action="store_true", help="Print document metadata")
65-
exgroup.add_argument("--version", action="store_true",
66-
help="Print version")
6765

6866
exgroup = parser.add_mutually_exclusive_group()
6967
exgroup.add_argument("--strict", action="store_true", help="Strict validation (unrecognized or out of place fields are error)",
@@ -79,11 +77,18 @@ def main(argsl=None): # type: (List[str]) -> int
7977
exgroup.add_argument("--debug", action="store_true",
8078
help="Print even more logging")
8179

82-
parser.add_argument("schema", type=str)
80+
parser.add_argument("schema", type=str, nargs="?", default=None)
8381
parser.add_argument("document", type=str, nargs="?", default=None)
82+
parser.add_argument("--version", "-v", action="store_true",
83+
help="Print version", default=None)
84+
8485

8586
args = parser.parse_args(argsl)
8687

88+
if args.version is None and args.schema is None:
89+
print('%s: error: too few arguments' % sys.argv[0])
90+
return 1
91+
8792
if args.quiet:
8893
_logger.setLevel(logging.WARN)
8994
if args.debug:
@@ -92,10 +97,10 @@ def main(argsl=None): # type: (List[str]) -> int
9297
pkg = pkg_resources.require("schema_salad")
9398
if pkg:
9499
if args.version:
95-
print("%s %s" % (sys.argv[0], pkg[0].version))
100+
print("%s Current version: %s" % (sys.argv[0], pkg[0].version))
96101
return 0
97102
else:
98-
_logger.info("%s %s", sys.argv[0], pkg[0].version)
103+
_logger.info("%s Current version: %s", sys.argv[0], pkg[0].version)
99104

100105
# Get the metaschema to validate the schema
101106
metaschema_names, metaschema_doc, metaschema_loader = schema.get_metaschema()

schema_salad/tests/test_cli_args.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import unittest
2+
import sys
3+
4+
import schema_salad.main as cli_parser
5+
6+
""" for capturing print() output """
7+
from contextlib import contextmanager
8+
from StringIO import StringIO
9+
10+
@contextmanager
11+
def captured_output():
12+
new_out, new_err = StringIO(), StringIO()
13+
old_out, old_err = sys.stdout, sys.stderr
14+
try:
15+
sys.stdout, sys.stderr = new_out, new_err
16+
yield sys.stdout, sys.stderr
17+
finally:
18+
sys.stdout, sys.stderr = old_out, old_err
19+
20+
21+
""" test different sets of command line arguments"""
22+
class ParseCliArgs(unittest.TestCase):
23+
24+
def test_version(self):
25+
args = [["--version"], ["-v"]]
26+
for arg in args:
27+
with captured_output() as (out, err):
28+
cli_parser.main(arg)
29+
30+
response = out.getvalue().strip() # capture output and strip newline
31+
self.assertTrue("Current version" in response)
32+
33+
def test_empty_input(self):
34+
# running schema_salad tool wihtout any args
35+
args = []
36+
with captured_output() as (out, err):
37+
cli_parser.main(args)
38+
39+
response = out.getvalue().strip()
40+
self.assertTrue("error: too few arguments" in response)

0 commit comments

Comments
 (0)