From b16da4b242ffd79d177b4de0c9e779c452710d54 Mon Sep 17 00:00:00 2001 From: Martin Schwenke Date: Sun, 12 Aug 2018 21:28:48 +1000 Subject: [PATCH] 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 --- shellcheck.1.md | 5 +++++ shellcheck.hs | 24 ++++++++++++++++++++++-- src/ShellCheck/Checker.hs | 3 ++- src/ShellCheck/Interface.hs | 6 ++++-- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/shellcheck.1.md b/shellcheck.1.md index 1e15a81..abf2ff3 100644 --- a/shellcheck.1.md +++ b/shellcheck.1.md @@ -56,6 +56,11 @@ not warn at all, as `ksh` supports decimals in arithmetic contexts. standard output. Subsequent **-f** options are ignored, see **FORMATS** 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* : Specify Bourne shell dialect. Valid values are *sh*, *bash*, *dash* and *ksh*. diff --git a/shellcheck.hs b/shellcheck.hs index b361fe1..52866ff 100644 --- a/shellcheck.hs +++ b/shellcheck.hs @@ -67,7 +67,8 @@ instance Monoid Status where data Options = Options { checkSpec :: CheckSpec, externalSources :: Bool, - formatterOptions :: FormatterOptions + formatterOptions :: FormatterOptions, + maxSeverity :: Severity } defaultOptions = Options { @@ -75,7 +76,8 @@ defaultOptions = Options { externalSources = False, formatterOptions = FormatterOptions { foColorOption = ColorAuto - } + }, + maxSeverity = StyleC } usageHeader = "Usage: shellcheck [OPTIONS...] FILES..." @@ -93,6 +95,9 @@ options = [ Option "s" ["shell"] (ReqArg (Flag "shell") "SHELLNAME") "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"] (NoArg $ Flag "version" "true") "Print version information", Option "x" ["external-sources"] @@ -223,6 +228,14 @@ parseColorOption colorOption = "never" -> ColorNever _ -> 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 = case flag of Flag "shell" str -> @@ -266,6 +279,13 @@ parseOption flag options = } } + Flag "severity" severity -> + return options { + checkSpec = (checkSpec options) { + csMaxSeverity = parseSeverityOption severity + } + } + _ -> return options where die s = do diff --git a/src/ShellCheck/Checker.hs b/src/ShellCheck/Checker.hs index 1ecf03e..23f60ba 100644 --- a/src/ShellCheck/Checker.hs +++ b/src/ShellCheck/Checker.hs @@ -67,7 +67,8 @@ checkScript sys spec = do return . nub . sortMessages . filter shouldInclude $ (parseMessages ++ map translator analysisMessages) - shouldInclude (PositionedComment _ _ (Comment _ code _)) = + shouldInclude (PositionedComment _ _ (Comment severity code _)) = + severity <= csMaxSeverity spec && code `notElem` csExcludedWarnings spec sortMessages = sortBy (comparing order) diff --git a/src/ShellCheck/Interface.hs b/src/ShellCheck/Interface.hs index 0d5d7da..5e475b0 100644 --- a/src/ShellCheck/Interface.hs +++ b/src/ShellCheck/Interface.hs @@ -35,7 +35,8 @@ data CheckSpec = CheckSpec { csScript :: String, csCheckSourced :: Bool, csExcludedWarnings :: [Integer], - csShellTypeOverride :: Maybe Shell + csShellTypeOverride :: Maybe Shell, + csMaxSeverity :: Severity } deriving (Show, Eq) data CheckResult = CheckResult { @@ -49,7 +50,8 @@ emptyCheckSpec = CheckSpec { csScript = "", csCheckSourced = False, csExcludedWarnings = [], - csShellTypeOverride = Nothing + csShellTypeOverride = Nothing, + csMaxSeverity = StyleC } newParseSpec :: ParseSpec