summaryrefslogtreecommitdiff
path: root/Jennisys/Options.fs
blob: 291bd678d9c9fdda9618751a0db36b670e628ef3 (plain)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//  ####################################################################
///   This module is intended to store and handle configuration options
///
///   author: Aleksandar Milicevic (t-alekm@microsoft.com)
//  ####################################################################

module Options

open Utils

type Config = {
   inputFilename: string;
   methodToSynth: string;
   verifySolutions: bool;
   timeout: int;
}

let defaultConfig: Config = {
  inputFilename   = "";
  methodToSynth   = "*";
  verifySolutions = true;
  timeout         = 0;
}

let mutable CONFIG = defaultConfig

exception InvalidCmdLineArg of string
exception InvalidCmdLineOption of string

let ParseCmdLineArgs args = 
  let __StripSwitches str =
    match str with 
    | Prefix "--" x 
    | Prefix "-" x
    | Prefix "/" x -> x
    | _ -> str

  let __Split (str: string) =
    let stripped = __StripSwitches str
    if stripped = str then
      ("",str) 
    else
      let splits = stripped.Split([| ':' |])
      if splits.Length > 2 then raise (InvalidCmdLineOption("more than 2 colons in " + str))
      if splits.Length = 2 then
        let opt = splits.[0]
        let value = splits.[1]
        (opt,value)
      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 }
        | "verifySol" -> 
            let b = __CheckBool value opt
            __Parse rest { cfg with verifySolutions = b }
        | "timeout" ->
            let t = __CheckInt value opt
            __Parse rest { cfg with timeout = t }
        | "" -> 
            __Parse rest { cfg with inputFilename = value }
        | _ -> 
            raise (InvalidCmdLineOption("Unknown option: " + opt))
    | [] -> cfg   
    
  let __CheckBool value optName =
    if value = "" then
      true
    else
      try 
        System.Boolean.Parse value
      with 
        | ex -> raise (InvalidCmdLineArg("Option " + optName " must be boolean"))

  (* --- function body starts here --- *)           
  CONFIG <- __Parse args defaultConfig