Created
October 30, 2012 18:40
-
-
Save phadej/3982151 to your computer and use it in GitHub Desktop.
Docopt.hs - parsing command line options <=> matching a regexp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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