aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/apps/SetEnv.hs
diff options
context:
space:
mode:
Diffstat (limited to 'apps/SetEnv.hs')
-rw-r--r--apps/SetEnv.hs65
1 files changed, 34 insertions, 31 deletions
diff --git a/apps/SetEnv.hs b/apps/SetEnv.hs
index fda9726..f27be4e 100644
--- a/apps/SetEnv.hs
+++ b/apps/SetEnv.hs
@@ -4,63 +4,66 @@
- See LICENSE.txt for details.
-}
-module Main ( main ) where
+module Main (main) where
import System.Console.GetOpt
-import System.Environment ( getArgs, getProgName )
-import System.Exit ( exitFailure, exitSuccess )
-import System.IO ( hPutStr, stderr )
+import System.Environment (getArgs, getProgName)
+import System.Exit (exitFailure, exitSuccess)
+import System.IO (hPutStr, stderr)
import qualified Environment
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
+ 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 = Environment.saveToRegistryWithPrompt (env options) name value
-data Options = Options { env :: Environment.RegistryBasedEnvironment } deriving (Eq, Show)
+data Options = Options
+ { env :: Environment.RegistryBasedEnvironment
+ } deriving (Eq, Show)
defaultOptions :: Options
-defaultOptions = Options { env = Environment.CurrentUserEnvironment }
+defaultOptions = Options
+ { env = Environment.CurrentUserEnvironment
+ }
buildHelpMessage :: IO String
buildHelpMessage = do
- header <- buildHeader
- return $ usageInfo header optionDescription
- where
- buildHeader :: IO String
- buildHeader = do
+ header <- buildHeader
+ return $ usageInfo header optionDescription
+ where
+ buildHeader = do
progName <- getProgName
return $ "Usage: " ++ progName ++ " [OPTIONS...] NAME VALUE\nOptions:"
exitWithHelpMessage :: Options -> IO a
exitWithHelpMessage _ = do
- helpMessage <- buildHelpMessage
- putStr helpMessage
- exitSuccess
+ helpMessage <- buildHelpMessage
+ putStr helpMessage
+ exitSuccess
exitWithUsageErrors :: [String] -> IO a
exitWithUsageErrors errorMessages = do
- hPutStr stderr $ concatMap ("Usage error: " ++) errorMessages
- helpMessage <- buildHelpMessage
- hPutStr stderr helpMessage
- exitFailure
+ 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 = Environment.AllUsersEnvironment }) "save under the registry key for all users",
- Option "h" ["help"] (NoArg exitWithHelpMessage) "show this message and exit"
- ]
+optionDescription =
+ [ Option "g" ["global"] (NoArg $ \opts -> return opts { env = Environment.AllUsersEnvironment }) "save under the registry key for all users"
+ , Option "h" ["help"] (NoArg exitWithHelpMessage) "show this message and exit"
+ ]