summaryrefslogtreecommitdiff
path: root/Jennisys/Options.fs
diff options
context:
space:
mode:
Diffstat (limited to 'Jennisys/Options.fs')
-rw-r--r--Jennisys/Options.fs133
1 files changed, 73 insertions, 60 deletions
diff --git a/Jennisys/Options.fs b/Jennisys/Options.fs
index 4bd59b11..567967ae 100644
--- a/Jennisys/Options.fs
+++ b/Jennisys/Options.fs
@@ -9,6 +9,7 @@ module Options
open Utils
type Config = {
+ help: bool;
inputFilename: string;
methodToSynth: string;
verifyPartialSolutions: bool;
@@ -19,7 +20,72 @@ type Config = {
numLoopUnrolls: int;
}
+type CfgOption<'a> = {
+ optionName: string;
+ optionType: string;
+ optionSetter: 'a -> Config -> Config;
+ descr: string;
+}
+
+exception InvalidCmdLineArg of string
+exception InvalidCmdLineOption of string
+
+let CheckNonEmpty value optName =
+ if value = "" then raise (InvalidCmdLineArg("A value for option " + optName + " must not be empty")) else value
+
+let CheckInt value optName =
+ try
+ System.Int32.Parse value
+ with
+ | ex -> raise (InvalidCmdLineArg("A value for option " + optName + " must be a boolean"))
+
+let CheckBool value optName =
+ if value = "" then
+ true
+ else
+ try
+ System.Boolean.Parse value
+ with
+ | ex -> raise (InvalidCmdLineArg("A value for option " + optName + " must be an integer"))
+
+let cfgOptions = [
+ { optionName = "help"; optionType = "bool"; optionSetter = (fun v (cfg: Config) -> {cfg with help = CheckBool v "help"}); descr = "description not available"; }
+ { optionName = "method"; optionType = "string"; optionSetter = (fun v (cfg: Config) -> {cfg with methodToSynth = CheckNonEmpty v "method"}); descr = "description not available"; }
+ { optionName = "verifyParSol"; optionType = "bool"; optionSetter = (fun v (cfg: Config) -> {cfg with verifyPartialSolutions = CheckBool v "verifyParSol"}); descr = "description not available"; }
+ { optionName = "noVerifyParSol"; optionType = "bool"; optionSetter = (fun v (cfg: Config) -> {cfg with verifyPartialSolutions = not (CheckBool v "verifyParSol")}); descr = "description not available"; }
+ { optionName = "verifySol"; optionType = "bool"; optionSetter = (fun v (cfg: Config) -> {cfg with verifySolutions = CheckBool v "verifySol"}); descr = "description not available"; }
+ { optionName = "noVerifySol"; optionType = "bool"; optionSetter = (fun v (cfg: Config) -> {cfg with verifySolutions = not (CheckBool v "verifySol")}); descr = "description not available"; }
+ { optionName = "checkUnifs"; optionType = "bool"; optionSetter = (fun v (cfg: Config) -> {cfg with checkUnifications = CheckBool v "checkUnifs"}); descr = "description not available"; }
+ { optionName = "noCheckUnifs"; optionType = "bool"; optionSetter = (fun v (cfg: Config) -> {cfg with checkUnifications = not (CheckBool v "noCheckUnifs")}); descr = "description not available"; }
+ { optionName = "genRepr"; optionType = "bool"; optionSetter = (fun v (cfg: Config) -> {cfg with genRepr = CheckBool v "genRepr"}); descr = "description not available"; }
+ { optionName = "noGenRepr"; optionType = "bool"; optionSetter = (fun v (cfg: Config) -> {cfg with genRepr = not (CheckBool v "noGenRepr")}); descr = "description not available"; }
+ { optionName = "timeout"; optionType = "int"; optionSetter = (fun v (cfg: Config) -> {cfg with timeout = CheckInt v "timeout"}); descr = "description not available"; }
+ { optionName = "unrolls"; optionType = "int"; optionSetter = (fun v (cfg: Config) -> {cfg with numLoopUnrolls = CheckInt v "unrolls"}); descr = "description not available"; }
+]
+
+let cfgOptMap = cfgOptions |> List.fold (fun acc o -> acc |> Map.add o.optionName o) Map.empty
+
+let newline = System.Environment.NewLine
+
+let PrintHelpMsg =
+ let maxw = cfgOptions |> List.fold (fun acc o -> if String.length o.optionName > acc then String.length o.optionName else acc) 0
+ let maxwStr = sprintf "%d" (maxw + 2)
+ let strf = new Printf.StringFormat<_>(" %-" + maxwStr + "s: %-6s | %s")
+ let rec __PrintHelp optLst =
+ match optLst with
+ | fs :: [] -> (sprintf strf fs.optionName fs.optionType fs.descr)
+ | fs :: rest -> (sprintf strf fs.optionName fs.optionType fs.descr) + newline + (__PrintHelp rest)
+ | [] -> ""
+ (* --- function body starts here --- *)
+ newline +
+ "Jennisys usage: Jennisys [ option ... ] filename" + newline +
+ " where <option> is one of " + newline + newline +
+ " ----- General options -----------------------------------------------------" + newline +
+ " (available switches are: /, -, --)" + newline + newline +
+ (__PrintHelp cfgOptions)
+
let defaultConfig: Config = {
+ help = false;
inputFilename = "";
methodToSynth = "*";
verifyPartialSolutions = true;
@@ -34,9 +100,6 @@ let defaultConfig: Config = {
/// typically called only once at the beginning of the program execution
let mutable CONFIG = defaultConfig
-exception InvalidCmdLineArg of string
-exception InvalidCmdLineOption of string
-
let ParseCmdLineArgs args =
let __StripSwitches str =
match str with
@@ -59,67 +122,17 @@ let ParseCmdLineArgs args =
else
let x = __StripSwitches splits.[0]
(x, "")
-
- let __CheckNonEmpty value optName =
- if value = "" then raise (InvalidCmdLineArg("A value for option " + optName + " must not be empty"))
-
- let __CheckInt value optName =
- try
- System.Int32.Parse value
- with
- | ex -> raise (InvalidCmdLineArg("A value for option " + optName + " must be a boolean"))
-
- let __CheckBool value optName =
- if value = "" then
- true
- else
- try
- System.Boolean.Parse value
- with
- | ex -> raise (InvalidCmdLineArg("A value for option " + optName + " must be an integer"))
-
+
let rec __Parse args cfg =
match args with
| fs :: rest ->
let opt,value = __Split fs
- match opt with
- | "method" ->
- __CheckNonEmpty value opt
- __Parse rest { cfg with methodToSynth = value }
- | "verifyParSol" ->
- let b = __CheckBool value opt
- __Parse rest { cfg with verifyPartialSolutions = b }
- | "noVerifyParSol" ->
- let b = __CheckBool value opt
- __Parse rest { cfg with verifyPartialSolutions = not b }
- | "verifySol" ->
- let b = __CheckBool value opt
- __Parse rest { cfg with verifySolutions = b }
- | "noVerifySol" ->
- let b = __CheckBool value opt
- __Parse rest { cfg with verifySolutions = not b }
- | "checkUnifs" ->
- let b = __CheckBool value opt
- __Parse rest { cfg with checkUnifications = b }
- | "noCheckUnifs" ->
- let b = __CheckBool value opt
- __Parse rest { cfg with checkUnifications = not b }
- | "genRepr" ->
- let b = __CheckBool value opt
- __Parse rest { cfg with genRepr = b }
- | "noGenRepr" ->
- let b = __CheckBool value opt
- __Parse rest { cfg with genRepr = not b }
- | "timeout" ->
- let t = __CheckInt value opt
- __Parse rest { cfg with timeout = t }
- | "unrolls" ->
- let t = __CheckInt value opt
- __Parse rest { cfg with numLoopUnrolls = t }
- | "" ->
- __Parse rest { cfg with inputFilename = value }
- | _ ->
- raise (InvalidCmdLineOption("Unknown option: " + opt))
+ if opt = "" then
+ __Parse rest { cfg with inputFilename = CheckNonEmpty value opt }
+ else
+ match Map.tryFind opt cfgOptMap with
+ | Some(opt) -> __Parse rest (opt.optionSetter value cfg)
+ | None -> raise (InvalidCmdLineOption("Unknown option: " + opt))
| [] -> cfg
(* --- function body starts here --- *)