[Haskell-cafe] Terminal-like Application Design

Dougal Stanton dougal at dougalstanton.net
Fri Oct 17 08:46:56 EDT 2008


2008/10/17 Magnus Therning <magnus at therning.org>:
>
> I wanted to throw in another idea, something I didn't come up with
> myself but used in omnicodec[1].  Now I don't remember where I picked
> up the idea:

This method is described in
<http://www.haskell.org/haskellwiki/High-level_option_handling_with_GetOpt>.

I have used it in the past and eventually end up with huge Config data
structures and not much separation of unrelated parts of the
computation. Of course, this is probably just me ;-)

Alternatively (and this is where I have had more success) you can take
several passes over the command arguments, using different parsers.
Each parser just ignores what it doesn't recognise:

parseArgs names = do
    args <- getArgs
    let info = parseWith infoOptions []
    let options = parseWith argOptions defaultOptArgs
    let query = parseWith queryOptions defaultQuery
    return (info, options, query)
 where parseWith os z = case getOpt Permute os args of
                            (o,_,_) -> foldr id z o

Note that this method tends to ignore user error at the command line,
but I'm sure a hybrid could be constructed that was more chatty but
still quite clean at the back end.

See <http://www.dougalstanton.net/code/buses/> for this code in its
wider context.

Cheers,


D

-- 
Dougal Stanton
dougal at dougalstanton.net // http://www.dougalstanton.net


More information about the Haskell-Cafe mailing list