From ae4aea45300c922fa30397e0afff3afead6d144c Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Sat, 22 Aug 2015 13:15:10 -0700 Subject: [PATCH] Adds support for a SHELLCHECK_OPTS environment variable. --- ShellCheck/Regex.hs | 7 +++++++ shellcheck.1.md | 7 +++++++ shellcheck.hs | 12 +++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/ShellCheck/Regex.hs b/ShellCheck/Regex.hs index 8f89ff0..98e4528 100644 --- a/ShellCheck/Regex.hs +++ b/ShellCheck/Regex.hs @@ -71,3 +71,10 @@ subRegex re input replacement = f input (before, match, after) <- matchM re str :: Maybe (String, String, String) when (null match) $ error ("Internal error: substituted empty in " ++ str) return $ before ++ replacement ++ f after + +-- Split a string based on a regex. +splitOn :: String -> Regex -> [String] +splitOn input re = + case matchM re input :: Maybe (String, String, String) of + Just (before, match, after) -> before : after `splitOn` re + Nothing -> [input] diff --git a/shellcheck.1.md b/shellcheck.1.md index 619a30c..22ac258 100644 --- a/shellcheck.1.md +++ b/shellcheck.1.md @@ -151,6 +151,13 @@ Valid keys are: used to tell shellcheck where to look for a file whose name is determined at runtime, or to skip a source by telling it to use `/dev/null`. +# ENVIRONMENT VARIABLES +The environment variable `SHELLCHECK_OPTS` can be set with default flags: + + export SHELLCHECK_OPTS='--shell=bash --exclude=SC2016' + +Its value will be split on spaces and prepended to the command line on each +invocation. # AUTHOR ShellCheck is written and maintained by Vidar Holen. diff --git a/shellcheck.hs b/shellcheck.hs index 9370f4a..878e847 100644 --- a/shellcheck.hs +++ b/shellcheck.hs @@ -20,6 +20,7 @@ import ShellCheck.Data import ShellCheck.Checker import ShellCheck.Interface +import ShellCheck.Regex import ShellCheck.Formatter.Format import qualified ShellCheck.Formatter.CheckStyle @@ -123,8 +124,17 @@ getExclusions options = toStatus = liftM (either id id) . runExceptT +getEnvArgs = do + opts <- getEnv "SHELLCHECK_OPTS" `catch` cantWaitForLookupEnv + return . filter (not . null) $ opts `splitOn` mkRegex " +" + where + cantWaitForLookupEnv :: IOException -> IO String + cantWaitForLookupEnv = const $ return "" + main = do - args <- getArgs + params <- getArgs + envOpts <- getEnvArgs + let args = envOpts ++ params status <- toStatus $ do (flags, files) <- parseArguments args process flags files