[Haskell-cafe] Parsing arguments and reading configuration
Antoine Latter
aslatter at gmail.com
Fri Sep 19 20:35:33 EDT 2008
2008/9/19 Magnus Therning <magnus at therning.org>:
>
> First I thought I'd treat the configuration in a similar way, but then I
> noticed a slight ordering problem. The command line arguments should
> take priority over the contents of the configuration file, but the
> location of the configuration can be given as an argument. I could read
> the arguments twice, first to get the correct location of the config
> file, then load the config, and then read the arguments again to make
> sure they take priority. But that feels a little silly. Are there any
> more elegant solutions people are using?
I'm not sure how well it would hold up under maintenance, but you coud
have a config sum-type which is itself a monoid, and then create two
of them:
> data UserConfig = UserConfig
> { item1 :: Maybe Type1
> , item2 :: Maybe Type2
> , configFileLocation :: Maybe FilePath
> }
> instance Monoid UserConfig where
> {- not shown -}
> buildConfig :: IO UserConfig
> buildConfig = do
> cmdLineCfg <- buildConfigFromCmdLine
> fileCfg <- maybe (return mempty) buildConfigFromFile (configFileLocation cmdLineCfg)
>
> return $ fileCfg `mappend` cmdLineCfg
> -- mappend is assumed to be left-biased
> buildConfigFromCmdLine :: IO UserConfig
> buildConfigFromFile :: FilePath -> IO UserConfig
Does that make sense? or is it too complicated?
-Antoine
More information about the Haskell-Cafe
mailing list