Skip to content

Instantly share code, notes, and snippets.

@phadej
Created October 30, 2012 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phadej/3982151 to your computer and use it in GitHub Desktop.
Save phadej/3982151 to your computer and use it in GitHub Desktop.
Docopt.hs - parsing command line options <=> matching a regexp
module Docopt where
import System.Environment (getArgs)
import Regexp
data DocoptResult = DocoptCommand Bool
| DocoptPositionalArg (Maybe String)
| DocoptRepatableArg [String]
| DocoptFlag Bool
| DocoptRepeatableFlag Int
data DocoptOption = DocoptOption {
docoptOptionShort :: Maybe Char,
docoptOptionLong :: Maybe String
}
{-
Usage:
test1.py <foo> [-k] bar
test1.py <foo> [-l] baz
test1.py [-h|-v]
test1.py fubaz Z... to X...
test1.py [-v|-vv|-vvv] xyzzy ZYXXY
-}
main :: IO ()
main = getArgs >>= print
testParams1a = ["fooFile", "bar"]
testParams1b = ["fooFile", "-k", "bar"]
testParams2a = ["fooFile", "baz"]
testParams2b = ["fooFile", "-l", "baz"]
testParams3a = ["fubaz", "1", "2", "to", "3", "4"]
usage :: Regexp String
usage = first ~|~ second ~|~ third ~|~ forth ~|~ fifth
where first = concatMany [anyChar, optional (character "-k"), character "bar"]
second = concatMany [anyChar, optional (character "-l"), character "baz"]
third = character "-h" ~|~ character "-v"
forth = concatMany [character "fubaz", kleeneCross anyChar, character "to", kleeneCross anyChar]
fifth = concatMany [optional $ character "-v" ~|~ character "-vv" ~|~ character "-vvv", character "xyzzy", anyChar]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment