Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell commandline argument handling library

I've been using cmdargs for argument processing for a while, and it's great. However, I don't see a facility for long option names with a single hyphen, e.g. -option1 -option2 or the more difficult -optx which is equivalent to --opt=x. I need to maintain compatibility with an existing application, so these formats are both necessary.

System.Console.Getopt doesn't seem up to it either. Can anyone either provide a sample of how to do this with cmdargs, or suggest an alternative library which will support this?

like image 346
John L Avatar asked Apr 15 '11 15:04

John L


1 Answers

Probably the simplest thing you can do is to do a quick pre-processing pass yourself. For example, if you need to support exactly the three options -option1, -option2, and -optx, Something like this should work:

munge "-option1" = "--option1"
munge "-option2" = "--option2"
munge s | "-opt" `isPrefixOf` s = "--opt=" ++ drop 4 s
munge s = s

main = do
    realArgs <- getArgs
    withArgs (map munge realArgs) mainThatUsesCmdArgs

As you say, though, the help messages would then no longer mention the possibility of having these short forms. (Perhaps that's a good thing, though.)

If you want more, you will probably have to convince somebody to modify cmdargs.

like image 132
Daniel Wagner Avatar answered Sep 21 '22 15:09

Daniel Wagner