Re: [Haskell-cafe] Typed Configuration Files

2010-01-15 Thread Ketil Malde
Neil Mitchell ndmitch...@gmail.com writes:

 The CmdArgs manual might help:
 http://community.haskell.org/~ndm/darcs/cmdargs/cmdargs.htm

Yes, this is what I used :-)  Presenting examples is great, but gives me
the hubris to rip off the example that seems to fit most closely, and
modify it.  This makes for a quick start, but also for the odd
misunderstanding. 

 I'd describe cmdargs as referentially impure, but really concise.

Concise is nice - I've often been a bit annoyed that the otherwise nice
GetOpt stuff forces me to write a lot of boilerplate, enough of it that
my programs usually have a separate Options module.  For a lazy slob
like me, this means I often just hack something on top of the raw
GetArgs or write half-baked but simple solutions like my simpleargs
library.

 'def' is the default value, empty has a particular semantic meaning
 and serves to change the options. I should document this more
 carefully. Perhaps empty should be renamed 'optional', since that's
 what it does.

'whenMissing'?  'unspecified'?

 - As I wanted a single file argument, I tried to use 'args' in
  combination with a parameter of type FilePath.  Apparently 'args'
  wants [FilePath] and appends command line arguments to the default
  value. 

Spoilt by Haskell, I'd have expected a type error here.

 argPos 0 should do the trick.

Right.  Will subsequent arguments be ignored?

 CmdArgs is very much a 0.1 release. The documentation isn't polished,
 it does simple arguments nicely, but has flaws when you try and go
 more advanced. I want to spend some more time on it at some point.

That's great, I'm looking forward to it.

-k
-- 
If I haven't seen further, it is by standing in the footprints of giants
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Typed Configuration Files

2010-01-14 Thread Ketil Malde
Magnus Therning mag...@therning.org writes:

 Seriously, cmdargs is *brilliant*.  It's also magic (to me).

On this list, I'm uncertain whether brilliant is a warning or a
recommendation, but magic is clearly irresistible, so I had a go at
using cmdargs. 

And I agree, it is really nice in quickly and succintly getting command
parsing up and working, and in that it most Works As Expected (tm).
Some snags I ran into, which may (or may not) serve to improve
documentation, and which may (or may not) result in some gentle guidande
as to preferred solutions rising to the surface: 

- The examples use 'def' a lot, and I mistakenly thought 'empty' would
  supply default values. Not so, replace 'def' with the default value
  and off you go.  'def' seems to be the minimum value for that
  particular type.

- As I wanted a single file argument, I tried to use 'args' in
  combination with a parameter of type FilePath.  Apparently 'args'
  wants [FilePath] and appends command line arguments to the default
  value.  I used 'error no file bla bla' as the default value, and 
  appending to this didn't do much good, as you can imagine.  So: use
  [FilePath] and check the length manually.

- CmdArgs helpfully provides default --help, --version as well as
  --quite and --verbose.  For the two former, there's also a nice
  default implementation, but presumably the latter two are for use in
  the program proper.  Unfortunately, I don't know how to get at their
  values.

-k
-- 
If I haven't seen further, it is by standing in the footprints of giants
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Typed Configuration Files

2010-01-14 Thread Ketil Malde
Ketil Malde ke...@malde.org writes:

 - CmdArgs helpfully provides default --help, --version as well as
   --quite and --verbose.  For the two former, there's also a nice
   default implementation, but presumably the latter two are for use in
   the program proper.  Unfortunately, I don't know how to get at their
   values.

I couldn't find it in an example, but apparently it is done through
global functions 'isLoud', 'isNormal', and 'isQuiet'.

-k
-- 
If I haven't seen further, it is by standing in the footprints of giants
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Typed Configuration Files

2010-01-14 Thread Matthias Görgens
Hi Sebastian,

You might also want to look at how xmonad handles it's configuration.
Basically the configuration file is the main-file that produces the
executable and takes in the rest of xmonad as a library.  This works
out quite well, but you need a compiler to update the configuration.

Matthias.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Typed Configuration Files

2010-01-14 Thread Neil Mitchell
Hi

The CmdArgs manual might help:
http://community.haskell.org/~ndm/darcs/cmdargs/cmdargs.htm

 Seriously, cmdargs is *brilliant*.  It's also magic (to me).

 On this list, I'm uncertain whether brilliant is a warning or a
 recommendation, but magic is clearly irresistible, so I had a go at
 using cmdargs.

I'd describe cmdargs as referentially impure, but really concise.

 And I agree, it is really nice in quickly and succintly getting command
 parsing up and working, and in that it most Works As Expected (tm).
 Some snags I ran into, which may (or may not) serve to improve
 documentation, and which may (or may not) result in some gentle guidande
 as to preferred solutions rising to the surface:

 - The examples use 'def' a lot, and I mistakenly thought 'empty' would
  supply default values. Not so, replace 'def' with the default value
  and off you go.  'def' seems to be the minimum value for that
  particular type.

'def' is the default value, empty has a particular semantic meaning
and serves to change the options. I should document this more
carefully. Perhaps empty should be renamed 'optional', since that's
what it does.

 - As I wanted a single file argument, I tried to use 'args' in
  combination with a parameter of type FilePath.  Apparently 'args'
  wants [FilePath] and appends command line arguments to the default
  value.  I used 'error no file bla bla' as the default value, and
  appending to this didn't do much good, as you can imagine.  So: use
  [FilePath] and check the length manually.

argPos 0 should do the trick.

 - CmdArgs helpfully provides default --help, --version as well as
  --quite and --verbose.  For the two former, there's also a nice
  default implementation, but presumably the latter two are for use in
  the program proper.  Unfortunately, I don't know how to get at their
  values.

As you found later, isLoud etc do the job.

CmdArgs is very much a 0.1 release. The documentation isn't polished,
it does simple arguments nicely, but has flaws when you try and go
more advanced. I want to spend some more time on it at some point.

Thanks, Neil
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Typed Configuration Files

2010-01-10 Thread Sebastian Fischer

Is there something similar for parsing config files?


If you write one I most certainly will use it! ;)


You (we) can already start using the cmdargs package to parse config  
files.


Upon my feature request to add a function to the cmdargs package that  
allows to add default arguments, Neil pointed out that the function  
System.Environment.withArgs can be used to get the same effect without  
changes to the cmdargs package.


Here is a complete example:

{-# LANGUAGE DeriveDataTypeable #-}

import System.Environment
import System.Console.CmdArgs

data Conf = Conf { option :: Bool }
 deriving (Show,Data,Typeable)

myConf = mode $ Conf { option = enum False [True,False] }

main = print = getConfig my.conf My Program v0.0 myConf

getConfig configFileName welcomeMsg modeDesc =
  do originalArgs - getArgs
 argsFromFile - words `fmap` readFile configFileName
 withArgs (argsFromFile ++ originalArgs) (cmdArgs welcomeMsg  
[modeDesc])


If you save the String '--true' in the file 'my.conf', this program  
reads the config from the file and prints it:


# runhaskell typed-config.hs
Conf {option = True}

You can overwrite the default behaviour with command line arguments:

# runhaskell typed-config.hs --false
Conf {option = False}

After parsing a config file into command-line arguments, the parsing  
of the typed `Config` comes for free.


Sebastian

P.S.:

Instead of the `words` function one would use some smarter function  
that translates real config files into command-line arguments, but the  
fez-conf package (which provides such functionality) segfaults on my  
computer.


Depending on how one specifies the mode value, one may not be able to  
overwrite default options. For example, the usual translation of the  
boolean field above is a single flag --option that can be present or  
absent. I did not find a way to unset a set flag other than declaring  
it as an enum flag. This could be improved if flags without arguments  
would support optional arguments like '--option=yes/no' or similar.  
(Btw. the documentation of enum seems wrong, the given example does  
not typecheck).


--
Underestimating the novelty of the future is a time-honored tradition.
(D.G.)



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Typed Configuration Files

2010-01-08 Thread Sebastian Fischer

Dear Café,

Neil Mitchell's cmdargs package [1] is pretty neat. It can be used to  
parse command-line arguments into a user-defined data structure.


Is there something similar for parsing config files?

There are a number of config file parsers on Hackage. But even the  
most sophisticated one I found, ConfigFile by John Goerzen [2], only  
yields primitive data like strings, booleans, and numbers. Did I  
overlook something?


I'd like to write something like

do fc - configFile ...
   ac - cmdArgs ...
   let conf = fc `mappend` ac

where the type of `conf` is a user defined monoid.

I found that the fez-conf package [3] provides a function  
`parseToArgs` which creates a list similar to the one returned by  
`System.getArgs` from a config file. Neil, if you would add a function  
to your cmdargs package that allows to specify the argument list, then  
one could reuse your machinery to create typed config data from config  
files too, right? Maybe along the lines of


do args - parseToArgs $ readFile /my/conf
   conf - cmdArgsWithDefault args My Program [myMode]

The new function `cmdArgsWithDefault` could require the return type to  
be a monoid in order to allow users to specify themselves how to deal  
with multiple options of the same kind.


Would that be reasonable or is there a better alternative?

Cheers,
Sebastian

[1] http://hackage.haskell.org/package/cmdargs
[2] http://hackage.haskell.org/package/ConfigFile
[3] http://hackage.haskell.org/package/fez-conf


--
Underestimating the novelty of the future is a time-honored tradition.
(D.G.)



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Typed Configuration Files

2010-01-08 Thread Magnus Therning
On Fri, Jan 8, 2010 at 12:53 PM, Sebastian Fischer
s...@informatik.uni-kiel.de wrote:
 Dear Café,

 Neil Mitchell's cmdargs package [1] is pretty neat. It can be used to parse
 command-line arguments into a user-defined data structure.

 Is there something similar for parsing config files?

If you write one I most certainly will use it! ;)

Seriously, cmdargs is *brilliant*.  It's also magic (to me).
Implementing a config parser in a similar way would likely dispell the
magic... if I only could find the time.

/M

-- 
Magnus Therning(OpenPGP: 0xAB4DFBA4)
magnus@therning.org  Jabber: magnus@therning.org
http://therning.org/magnus identi.ca|twitter: magthe
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Typed Configuration Files

2010-01-08 Thread Nicolas Pouillard
Excerpts from Magnus Therning's message of Fri Jan 08 16:25:31 +0100 2010:
 On Fri, Jan 8, 2010 at 12:53 PM, Sebastian Fischer
 s...@informatik.uni-kiel.de wrote:
  Dear Café,
 
  Neil Mitchell's cmdargs package [1] is pretty neat. It can be used to parse
  command-line arguments into a user-defined data structure.
 
  Is there something similar for parsing config files?
 
 If you write one I most certainly will use it! ;)
 
 Seriously, cmdargs is *brilliant*.  It's also magic (to me).

Not only to you in fact it is black magic since it uses unsafePerformIO :(

-- 
Nicolas Pouillard
http://nicolaspouillard.fr
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Typed Configuration Files

2010-01-08 Thread Neil Mitchell
Hi,

 Seriously, cmdargs is *brilliant*.  It's also magic (to me).

 Not only to you in fact it is black magic since it uses unsafePerformIO :(

The problem isn't that it's black magic or that it uses
unsafePerformIO - the problem is that it's horribly impure, so doesn't
obey referential transparency. Unfortunately this in unavoidable with
the way I wanted to write command line arguments...

Thanks, Neil
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Typed Configuration Files

2010-01-08 Thread Bulat Ziganshin
Hello Sebastian,

Friday, January 8, 2010, 3:53:53 PM, you wrote:

 Neil Mitchell's cmdargs package [1] is pretty neat. It can be used to
 parse command-line arguments into a user-defined data structure.

 Is there something similar for parsing config files?

Lua language may be used to describe arbitrarily complex data
structure and there is HsLua: http://www.haskell.org/haskellwiki/HsLua


-- 
Best regards,
 Bulatmailto:bulat.zigans...@gmail.com

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe