File: GetOpt.hs

package info (click to toggle)
haskell-cmdargs 0.10.14-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 348 kB
  • ctags: 1
  • sloc: haskell: 2,972; makefile: 3
file content (42 lines) | stat: -rw-r--r-- 1,689 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{-# LANGUAGE DeriveDataTypeable #-}

module System.Console.CmdArgs.Test.GetOpt where

import Data.Data
import System.Console.CmdArgs.GetOpt
import qualified System.Console.CmdArgs.Explicit as Explicit
import System.Console.CmdArgs.Test.Util


data Flag = Verbose | Version | Name String | Output String | Arg String deriving (Show,Data,Typeable)

options :: [OptDescr Flag]
options =
   [Option ['v']     ["verbose"]           (NoArg Verbose)      "verbosely list files",
    Option ['V','?'] ["version","release"] (NoArg Version)      "show version info",
    Option ['o']     ["output"]            (OptArg out "FILE")  "use FILE for dump",
    Option ['n']     ["name"]              (ReqArg Name "USER") "only dump USER's files"]

out :: Maybe String -> Flag
out Nothing  = Output "stdout"
out (Just o) = Output o

tester :: [String] -> (String,String)
tester cmdline = case getOpt Permute options cmdline of
                        (o,n,[]  ) -> let x = "options=" ++ show o ++ "  args=" ++ show n in (x,x)
                        (_,_,errs) -> ("failed", unlines errs ++ usageInfo header options)
   where header = "Usage: foobar [OPTION...] files..."

mode = (convert "GetOpt compatibility demo" options){Explicit.modeNames=["getopt"]}

demo = [newDemo print mode]


test = do
    tester ["foo","-v"] === "options=[Verbose]  args=[\"foo\"]"
    tester ["foo","--","-v"] === "options=[]  args=[\"foo\",\"-v\"]"
    tester ["-?o","--name","bar","--na=baz"] === "options=[Version,Output \"stdout\",Name \"bar\",Name \"baz\"]  args=[]"
    tester ["--ver","foo"] === "failed"

a === b | fst a == b = success
        | otherwise = failure "Mismatch in GetOpt" [("Wanted",b),("Got",fst a)]