[Haskell-cafe] commandline parsing?

Marc Weber marco-oweber at gmx.de
Sat Jul 29 08:53:00 EDT 2006

I've  been using pesco_cmdline  for a while now. But I did notice that
it  doesn't fit my needs.. And it took me quite a while to get to know
why I was getting strange typeable errors when specifying the wrong
default  value or reading the wrong type.. (these errors occur at
runtime thus they don't use haskells  strength)

So I got another crazy  idea. Why not see arguments as tokens and use a
parser such as parsec?

All you would have to do is creating some kind of syntax tree.

Consider an example application : mmewcde (my mega executable which can
do everything).

mmewcde edit <file>
mmewcde mv <file> <file>
mmewcde mv --target-dir <dest> <folder> <files>>
mmewcde cp <file> <file>
mmewcde cp --target-dir <dest> <folder> <files>>
mmewcde callsox <infile> <options> <outfile>
Why not something like tar?
mmewcde tar <x|c|f|v>*  ..?

Of cause I'll have to  write the funtions
edit :: String -> IO ()
mv :: String ->  String -> IO ()
mvDest :: String -> [String] -> IO ()
which have to be called

Now I'd like to do this:

optionParser = many $ oneOf [  edit, mv, mvDest, cp, cpDest,  callsox, tar]
optionParserWithHelp = optionParser <|> printHelp 

edit = do
	string "edit"
	file <- existingFilename
	launchEditor file

mv = [..]
mvDest = do
	string mv
	string "--target-dir"
	td <- existingDirectory
	files <- many existingFilename
	map (movefile td) files


printHelp = do
	oneOf $ map string ["-h","--help","--usage"]
	prettyPrint optionParser

main = do
	args <- getArgs
	parseAndExecute optionParserWithHelp

prettyPrint might look like this (this will be unncommon ;)

mmewcde --help:
		edit <exitingfile> or
		mv  <exitingfile> <file>

Would this be nice? Does this already exist somehow?

perhaps many can even be made nongreedy so that you can specify more
than one command at once
mmewcde mv  --target-dir d f1 f2    cp --target-dir d2 f3 f4 ...

Does this make sense?
I think this would lead to self documenting well mantainable code.


More information about the Haskell-Cafe mailing list