aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/apps/UnsetEnv.hs
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2016-07-11 18:55:26 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2016-07-11 18:55:26 +0300
commitd53c986113cef78e2b504ecd5a4bba60999a55e1 (patch)
tree445b892abcfa272153dc39968838dbdc5d34d684 /apps/UnsetEnv.hs
parentREADME update (diff)
downloadwindows-env-d53c986113cef78e2b504ecd5a4bba60999a55e1.tar.gz
windows-env-d53c986113cef78e2b504ecd5a4bba60999a55e1.zip
become a proper stack project
Diffstat (limited to 'apps/UnsetEnv.hs')
-rw-r--r--apps/UnsetEnv.hs65
1 files changed, 65 insertions, 0 deletions
diff --git a/apps/UnsetEnv.hs b/apps/UnsetEnv.hs
new file mode 100644
index 0000000..cd43696
--- /dev/null
+++ b/apps/UnsetEnv.hs
@@ -0,0 +1,65 @@
+{-
+ - Copyright 2015 Egor Tensin <Egor.Tensin@gmail.com>
+ - This file is licensed under the terms of the MIT License.
+ - See LICENSE.txt for details.
+-}
+
+module Main ( main ) where
+
+import System.Console.GetOpt
+import System.Environment ( getArgs, getProgName )
+import System.Exit ( exitFailure, exitSuccess )
+import System.IO ( hPutStr, stderr )
+
+import qualified EnvUtils
+
+main :: IO ()
+main = do
+ rawArgs <- getArgs
+ case getOpt Permute optionDescription rawArgs of
+ (actions, args, []) -> do
+ options <- foldl (>>=) (return defaultOptions) actions
+ case args of
+ [name] -> unsetEnv name options
+ _ -> invalidNumberOfArguments
+ (_, _, errorMessages) -> exitWithUsageErrors errorMessages
+
+unsetEnv :: String -> Options -> IO ()
+unsetEnv name options = EnvUtils.wipeFromRegistryWithPrompt (env options) name
+
+data Options = Options { env :: EnvUtils.RegistryBasedEnvironment } deriving (Eq, Show)
+
+defaultOptions :: Options
+defaultOptions = Options { env = EnvUtils.CurrentUserEnvironment }
+
+buildHelpMessage :: IO String
+buildHelpMessage = do
+ header <- buildHeader
+ return $ usageInfo header optionDescription
+ where
+ buildHeader :: IO String
+ buildHeader = do
+ progName <- getProgName
+ return $ "Usage: " ++ progName ++ " [OPTIONS...] NAME\nOptions:"
+
+exitWithHelpMessage :: a -> IO b
+exitWithHelpMessage _ = do
+ helpMessage <- buildHelpMessage
+ putStr helpMessage
+ exitSuccess
+
+exitWithUsageErrors :: [String] -> IO a
+exitWithUsageErrors errorMessages = do
+ hPutStr stderr $ concatMap ("Usage error: " ++) errorMessages
+ helpMessage <- buildHelpMessage
+ hPutStr stderr helpMessage
+ exitFailure
+
+invalidNumberOfArguments :: IO a
+invalidNumberOfArguments = exitWithUsageErrors ["invalid number of arguments\n"]
+
+optionDescription :: [OptDescr (Options -> IO Options)]
+optionDescription = [
+ Option "g" ["global"] (NoArg $ \opts -> return opts { env = EnvUtils.AllUsersEnvironment }) "delete from the registry key for all users",
+ Option "h" ["help"] (NoArg exitWithHelpMessage) "show this message and exit"
+ ]