From 4e5d32b05a1cbecb6184fd61f911ba4c5f34508b Mon Sep 17 00:00:00 2001 From: Vidar Holen Date: Mon, 3 Feb 2014 20:06:59 -0800 Subject: [PATCH] Added --version flag --- ShellCheck.cabal | 2 +- ShellCheck/Analytics.hs | 3 +++ ShellCheck/Data.hs | 2 ++ shellcheck.hs | 35 +++++++++++++++++++++++------------ 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/ShellCheck.cabal b/ShellCheck.cabal index 11132c7..c10f8da 100644 --- a/ShellCheck.cabal +++ b/ShellCheck.cabal @@ -1,5 +1,5 @@ Name: ShellCheck -Version: 0.3.0 +Version: 0.3.0 -- Must also be updated in ShellCheck/Data.hs Synopsis: Shell script analysis tool License: OtherLicense License-file: LICENSE diff --git a/ShellCheck/Analytics.hs b/ShellCheck/Analytics.hs index 9a6d483..dc14741 100644 --- a/ShellCheck/Analytics.hs +++ b/ShellCheck/Analytics.hs @@ -105,8 +105,11 @@ determineShell (T_Script _ shebang _) = fromMaybe Bash . shellForExecutable $ sh shellForExecutable "sh" = return Sh shellForExecutable "ash" = return Sh shellForExecutable "dash" = return Sh + shellForExecutable "ksh" = return Ksh +shellForExecutable "ksh88" = return Ksh shellForExecutable "ksh93" = return Ksh + shellForExecutable "zsh" = return Zsh shellForExecutable "bash" = return Bash shellForExecutable _ = Nothing diff --git a/ShellCheck/Data.hs b/ShellCheck/Data.hs index a071561..2d569eb 100644 --- a/ShellCheck/Data.hs +++ b/ShellCheck/Data.hs @@ -1,5 +1,7 @@ module ShellCheck.Data where +shellcheckVersion = "0.3.0" -- Must also be updated in ShellCheck.cabal + internalVariables = [ -- Generic "", "_", "rest", "REST", diff --git a/shellcheck.hs b/shellcheck.hs index 673970b..68c8b79 100644 --- a/shellcheck.hs +++ b/shellcheck.hs @@ -22,6 +22,7 @@ import Data.Maybe import GHC.Exts import GHC.IO.Device import Prelude hiding (catch) +import ShellCheck.Data import ShellCheck.Simple import ShellCheck.Analytics import System.Console.GetOpt @@ -41,7 +42,9 @@ options = [ Option ['e'] ["exclude"] (ReqArg (Flag "exclude") "CODE1,CODE2..") "exclude types of warnings", Option ['s'] ["shell"] - (ReqArg (Flag "shell") "SHELLNAME") "Specify dialect (bash,sh,ksh,zsh)" + (ReqArg (Flag "shell") "SHELLNAME") "Specify dialect (bash,sh,ksh,zsh)", + Option ['V'] ["version"] + (NoArg $ Flag "version" "true") "Print version information" ] printErr = hPutStrLn stderr @@ -61,14 +64,9 @@ instance JSON ShellCheckComment where parseArguments argv = case getOpt Permute options argv of - (opts, files, []) -> - if not $ null files - then - return $ Just (opts, files) - else do - printErr "No files specified.\n" - printErr $ usageInfo header options - exitWith syntaxFailure + (opts, files, []) -> do + verifyOptions opts files + return $ Just (opts, files) (_, _, errors) -> do printErr $ (concat errors) ++ "\n" ++ usageInfo header options @@ -268,7 +266,6 @@ main = do process Nothing = return False process (Just (options, files)) = do - verifyShellOption options let format = fromMaybe "tty" $ getOption options "format" in case Map.lookup format formats of Nothing -> do @@ -280,10 +277,24 @@ process (Just (options, files)) = do Just f -> do f options files -verifyShellOption options = - let shell = getOption options "shell" in +verifyOptions opts files = do + when (isJust $ getOption opts "version") printVersionAndExit + + let shell = getOption opts "shell" in if isNothing shell then return () else when (isNothing $ shell >>= shellForExecutable) $ do printErr $ "Unknown shell: " ++ (fromJust shell) exitWith supportFailure + + when (null files) $ do + printErr "No files specified.\n" + printErr $ usageInfo header options + exitWith syntaxFailure + +printVersionAndExit = do + putStrLn $ "ShellCheck - shell script analysis tool" + putStrLn $ "version: " ++ shellcheckVersion + putStrLn $ "license: GNU Affero General Public License, version 3" + putStrLn $ "website: http://www.shellcheck.net" + exitWith ExitSuccess -- GitLab