Commit eb9c25f1 authored by Camil Staps's avatar Camil Staps 🙂

Initial argument parsing

parent 6571ac8a
Clean System Files/
module CleanTest
Start = "Placeholder..."
// Build with -nr -nt -nortsopts
import StdFile
import StdList
import StdString
import Data.Error
from Data.Func import $
import Data.Generics.GenDefault
import Data.Maybe
import System.CommandLine
import System.FilePath
:: Options =
{ runs :: [Run]
, help :: Bool
}
:: Run =
{ executable :: FilePath
, options :: [String]
}
gDefault{|Bool|} = False
derive gDefault Options, Run
Start w
# ([prog:args],w) = getCommandLine w
# opts = parseOpts gDefault{|*|} args
| isError opts = exit True (Just $ fromError opts) prog w
# opts = fromOk opts
| opts.help = exit True Nothing prog w
= w
where
parseOpts :: Options [String] -> MaybeErrorString Options
parseOpts opts [] = Ok {opts & runs=reverse opts.runs}
parseOpts opts ["--run":args] = parseOpts opts ["-r":args]
parseOpts opts ["-r":exe:args] = parseOpts {opts & runs=[new:opts.runs]} args
with new = {gDefault{|*|} & executable=exe}
parseOpts opts ["-r"] = Error "-r requires a parameter"
parseOpts opts ["--help":args] = parseOpts opts ["-h":args]
parseOpts opts ["-h":args] = parseOpts {opts & help=True} args
parseOpts opts ["--option":args] = parseOpts opts ["-O":args]
parseOpts opts ["-O":opt:args] = case opts.runs of
[] -> Error "-O used before -r"
[r:rs] -> parseOpts {opts & runs=[{r & options=r.options ++ [opt]}:rs]} args
parseOpts opts ["-O"] = Error "-O requires a parameter"
parseOpts opts [arg:args] = Error $ "Unknown argument '" +++ arg +++ "'"
exit :: Bool (Maybe String) String *World -> *World
exit show_help error prog w
# io = stderr
# io = io <<< case error of
Just e -> "Error: " +++ e +++ if show_help ".\n\n" ".\n"
Nothing -> ""
# io = if show_help (io <<< help) io
# (_,w) = fclose io w
# w = setReturnCode (if (isNothing error) 0 1) w
= w
where
help :: String
help = prog +++ ": run Clean tests\nOptions:\n" +++
" --help/-h Show this help\n" +++
" --run/-r EXE Execute tests from executable EXE\n" +++
" --option/-O OPT Add OPT to the command line of the previously added run\n"
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment