Repository: oasis-open/cti-stix-validator

Issue:
handle options --verbose and --silent correctly
Related to #50 

The correct combination of these two should be as follows:

|--verbose  |  --silent | desired behavior |
| --- | --- | --- |
|absent (default is False) | absent (default is False) | all messages except those printed by info |
|absent (default is False) | present (True) | no messages printed
| present (True) | absent (default is False) | all messages, including info are printed
| present (True) |  present (True) | error |

Current behavior is:

|--verbose  |  --silent | current behavior |
| --- | --- | --- |
|absent (default is False) | absent (default is False) | all messages except those printed by info |
|absent (default is False) | present (ignored, so the default - False) | all messages except those printed by info |
| present (True) | absent (default is False) | all messages, including info are printed
| present (True) |  present (ignored, so the default - False) | all messages, including info are printed |

Discussion:

Patch:
diff --git a/stix2validator/scripts/stix2_validator.py b/stix2validator/scripts/stix2_validator.py
index 15bd7b0..8dda167 100644
--- a/stix2validator/scripts/stix2_validator.py
+++ b/stix2validator/scripts/stix2_validator.py
@@ -292,9 +292,6 @@ def main():
     options = ValidationOptions(args)
 
     try:
-        # Set the output level (e.g., quiet vs. verbose)
-        output.set_level(options.verbose)
-
         if not options.no_cache:
             init_requests_cache(options.refresh_cache)
 
diff --git a/stix2validator/util.py b/stix2validator/util.py
index 4da0be5..327931f 100644
--- a/stix2validator/util.py
+++ b/stix2validator/util.py
@@ -1,5 +1,7 @@
 from collections import Iterable
 
+from .output import error, set_level, set_silent
+
 
 class ValidationOptions(object):
     """Collection of validation options which can be set via command line or
@@ -72,6 +74,12 @@ class ValidationOptions(object):
             self.refresh_cache = refresh_cache
             self.clear_cache = clear_cache
 
+        # Set the output level (e.g., quiet vs. verbose)
+        if self.silent and self.verbose:
+            error('Error: Output can either be silent or verbose, but not both.')
+        set_level(self.verbose)
+        set_silent(self.silent)
+
         # Convert string of comma-separated checks to a list,
         # and convert check code numbers to names
         if self.disabled:


Test patch:
diff --git a/stix2validator/test/bundle_tests.py b/stix2validator/test/bundle_tests.py
index 8f417bd..52235ba 100644
--- a/stix2validator/test/bundle_tests.py
+++ b/stix2validator/test/bundle_tests.py
@@ -1,6 +1,8 @@
 import copy
 import json
 
+import pytest
+
 from . import ValidatorTest
 
 VALID_BUNDLE = u"""
@@ -51,3 +53,8 @@ class BundleTestCases(ValidatorTest):
 
         bundle['objects'][1]['modified'] = "2017-06-22T14:09:00.123Z"
         self.assertTrueWithOptions(bundle)
+
+    def test_silent_and_verbose(self):
+        bundle = json.loads(VALID_BUNDLE)
+        with pytest.raises(SystemExit):
+            self.assertFalseWithOptions(bundle, silent=True, verbose=True)