Add command-line option -S/--severity

Specifies the maximum severity of errors to handle.  For example,
specifying "-S warning" means that errors of severity "info" and
"style" are ignored.

Signed-off-by: Martin Schwenke <martin@meltin.net>
This commit is contained in:
Martin Schwenke 2018-08-12 21:28:48 +10:00 committed by Vidar Holen
parent 15aaacf715
commit b16da4b242
4 changed files with 33 additions and 5 deletions

View File

@ -56,6 +56,11 @@ not warn at all, as `ksh` supports decimals in arithmetic contexts.
standard output. Subsequent **-f** options are ignored, see **FORMATS** standard output. Subsequent **-f** options are ignored, see **FORMATS**
below for more information. below for more information.
**-S**\ *SEVERITY*,\ **--severity=***severity*
: Specify maximum severity of errors to consider. Valid values are *error*,
*warning*, *info* and *style*. The default is *style*.
**-s**\ *shell*,\ **--shell=***shell* **-s**\ *shell*,\ **--shell=***shell*
: Specify Bourne shell dialect. Valid values are *sh*, *bash*, *dash* and *ksh*. : Specify Bourne shell dialect. Valid values are *sh*, *bash*, *dash* and *ksh*.

View File

@ -67,7 +67,8 @@ instance Monoid Status where
data Options = Options { data Options = Options {
checkSpec :: CheckSpec, checkSpec :: CheckSpec,
externalSources :: Bool, externalSources :: Bool,
formatterOptions :: FormatterOptions formatterOptions :: FormatterOptions,
maxSeverity :: Severity
} }
defaultOptions = Options { defaultOptions = Options {
@ -75,7 +76,8 @@ defaultOptions = Options {
externalSources = False, externalSources = False,
formatterOptions = FormatterOptions { formatterOptions = FormatterOptions {
foColorOption = ColorAuto foColorOption = ColorAuto
} },
maxSeverity = StyleC
} }
usageHeader = "Usage: shellcheck [OPTIONS...] FILES..." usageHeader = "Usage: shellcheck [OPTIONS...] FILES..."
@ -93,6 +95,9 @@ options = [
Option "s" ["shell"] Option "s" ["shell"]
(ReqArg (Flag "shell") "SHELLNAME") (ReqArg (Flag "shell") "SHELLNAME")
"Specify dialect (sh, bash, dash, ksh)", "Specify dialect (sh, bash, dash, ksh)",
Option "S" ["severity"]
(ReqArg (Flag "severity") "SEVERITY")
"Maximum severity of errors to consider (error, warning, info, style)",
Option "V" ["version"] Option "V" ["version"]
(NoArg $ Flag "version" "true") "Print version information", (NoArg $ Flag "version" "true") "Print version information",
Option "x" ["external-sources"] Option "x" ["external-sources"]
@ -223,6 +228,14 @@ parseColorOption colorOption =
"never" -> ColorNever "never" -> ColorNever
_ -> error $ "Bad value for --color `" ++ colorOption ++ "'" _ -> error $ "Bad value for --color `" ++ colorOption ++ "'"
parseSeverityOption severityOption =
case severityOption of
"error" -> ErrorC
"warning" -> WarningC
"info" -> InfoC
"style" -> StyleC
_ -> error $ "Bad value for --severity `" ++ severityOption ++ "'"
parseOption flag options = parseOption flag options =
case flag of case flag of
Flag "shell" str -> Flag "shell" str ->
@ -266,6 +279,13 @@ parseOption flag options =
} }
} }
Flag "severity" severity ->
return options {
checkSpec = (checkSpec options) {
csMaxSeverity = parseSeverityOption severity
}
}
_ -> return options _ -> return options
where where
die s = do die s = do

View File

@ -67,7 +67,8 @@ checkScript sys spec = do
return . nub . sortMessages . filter shouldInclude $ return . nub . sortMessages . filter shouldInclude $
(parseMessages ++ map translator analysisMessages) (parseMessages ++ map translator analysisMessages)
shouldInclude (PositionedComment _ _ (Comment _ code _)) = shouldInclude (PositionedComment _ _ (Comment severity code _)) =
severity <= csMaxSeverity spec &&
code `notElem` csExcludedWarnings spec code `notElem` csExcludedWarnings spec
sortMessages = sortBy (comparing order) sortMessages = sortBy (comparing order)

View File

@ -35,7 +35,8 @@ data CheckSpec = CheckSpec {
csScript :: String, csScript :: String,
csCheckSourced :: Bool, csCheckSourced :: Bool,
csExcludedWarnings :: [Integer], csExcludedWarnings :: [Integer],
csShellTypeOverride :: Maybe Shell csShellTypeOverride :: Maybe Shell,
csMaxSeverity :: Severity
} deriving (Show, Eq) } deriving (Show, Eq)
data CheckResult = CheckResult { data CheckResult = CheckResult {
@ -49,7 +50,8 @@ emptyCheckSpec = CheckSpec {
csScript = "", csScript = "",
csCheckSourced = False, csCheckSourced = False,
csExcludedWarnings = [], csExcludedWarnings = [],
csShellTypeOverride = Nothing csShellTypeOverride = Nothing,
csMaxSeverity = StyleC
} }
newParseSpec :: ParseSpec newParseSpec :: ParseSpec