[Haskell-cafe] commandline parsing?
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
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
file <- existingFilename
mv = [..]
mvDest = do
td <- existingDirectory
files <- many existingFilename
map (movefile td) files
printHelp = do
oneOf $ map string ["-h","--help","--usage"]
main = do
args <- getArgs
prettyPrint might look like this (this will be unncommon ;)
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