aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/SetEnv.hs
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2015-05-06 06:27:15 +0300
committerEgor Tensin <Egor.Tensin@gmail.com>2015-05-06 06:27:15 +0300
commit71e1194fcb39606336b0569147dd0f794b6bfa78 (patch)
tree61154840f0bce9be819533ea4b298e83d1d55290 /SetEnv.hs
downloadwindows-env-71e1194fcb39606336b0569147dd0f794b6bfa78.tar.gz
windows-env-71e1194fcb39606336b0569147dd0f794b6bfa78.zip
initial commit
Diffstat (limited to 'SetEnv.hs')
-rw-r--r--SetEnv.hs66
1 files changed, 66 insertions, 0 deletions
diff --git a/SetEnv.hs b/SetEnv.hs
new file mode 100644
index 0000000..30f5b1e
--- /dev/null
+++ b/SetEnv.hs
@@ -0,0 +1,66 @@
+{-
+ - 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, value] -> setEnv name value options
+ _ -> invalidNumberOfArguments
+ (_, _, errorMessages) ->
+ exitWithUsageErrors errorMessages
+
+setEnv :: String -> String -> Options -> IO ()
+setEnv name value options = EnvUtils.saveToRegistryWithPrompt (env options) name value
+
+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 VALUE\nOptions:"
+
+exitWithHelpMessage :: Options -> IO a
+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 }) "save under the registry key for all users",
+ Option "h" ["help"] (NoArg exitWithHelpMessage) "show this message and exit"
+ ]