Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Config.ml1 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 88open Prelude type t = { server : string; port : int; username : string; password : string option; realname : string; nick : string; tls: bool; tls_cert : Ssl.certificate option; sasl: bool; channel : string; state_file : string; log_level: Logs.level; prefix: string; (** prefix for commands *) } let default = { server = "irc.libera.chat"; port = 7000; username = "calculon"; password = None; realname = "calculon"; nick = "calculon"; tls = true; tls_cert = None; sasl = true; channel = "#ocaml"; state_file = "state.json"; log_level=Logs.Info; prefix = "!"; } let parse ?(extra_args=[]) conf args = let custom_nick = ref None in let custom_chan = ref None in let custom_server = ref None in let custom_state = ref None in let custom_port = ref conf.port in let custom_tls = ref None in let custom_sasl = ref None in let prefix = ref default.prefix in let log_lvl = ref None in let options = Arg.align @@ extra_args @ [ "--nick", Arg.String (fun s -> custom_nick := Some s), " custom nickname (default: " ^ default.nick ^ ")" ; "--chan", Arg.String (fun s -> custom_chan := Some s), " channel to join (default: " ^ default.channel ^ ")" ; "--port", Arg.Set_int custom_port, " port of the server" ; "--server", Arg.String (fun s -> custom_server := Some s), " server to join (default: " ^ default.server ^ ")" ; "--state", Arg.String (fun s -> custom_state := Some s), " file containing factoids (default: " ^ default.state_file ^ ")" ; "--tls", Arg.Bool (fun b -> custom_tls := Some b), " enable/disable TLS" ; "--sasl", Arg.Bool (fun b -> custom_sasl := Some b), " enable/disable SASL auth" ; "--debug", Arg.Unit (fun() ->log_lvl := Some Logs.Debug), " set log level to debug" ; "--prefix", Arg.Set_string prefix, " set prefix for commands (default \"!\")"; ] in Arg.parse_argv args options ignore "parse options"; (* env vars are also used *) let user = try Sys.getenv "USER" with _ -> conf.username and password = try Some (Sys.getenv "PASSWORD") with _ -> None in { conf with nick = !custom_nick |? conf.nick; username = user; password; channel = !custom_chan |? conf.channel; server = !custom_server |? conf.server; tls = !custom_tls |? conf.tls; sasl = !custom_sasl |? conf.sasl; port = !custom_port; state_file = !custom_state |? conf.state_file; log_level = !log_lvl |? conf.log_level; prefix = !prefix; } let of_argv ?extra_args () = try parse ?extra_args default Sys.argv with | Arg.Bad msg -> print_endline msg; exit 1 | Arg.Help msg -> print_endline msg; exit 0