elements-ocaml/elements.ml

68 lines
1.7 KiB
OCaml
Raw Normal View History

2021-08-10 09:21:23 -04:00
module Opts = struct
open Arg
type lasttype = NAME | SYMB | NUMB
2021-08-10 09:21:23 -04:00
(** Positional arguments search based on the last type searched. *)
2021-08-10 09:21:23 -04:00
let ltype = ref NAME
(** Json file that generates the Sqlite3 database. *)
let jsonf = ref ""
(** Limit the amount of matches. *)
2021-08-10 09:21:23 -04:00
let maxmatch = ref 3
(** Lists to search. *)
2021-08-10 09:21:23 -04:00
let match_name : string list ref = ref []
let match_symb : string list ref = ref []
let match_numb : int list ref = ref []
2021-08-10 09:21:23 -04:00
let dbfile = ref "elements.sqlite3"
let add_name s =
match_name := s::!match_name;
ltype := NAME
let add_symb s =
match_symb := s::!match_symb;
ltype := SYMB
let add_numb i =
match_numb := i::!match_numb;
ltype := NUMB
2021-08-10 09:21:23 -04:00
let add_def s = match !ltype with
| NAME -> add_name s
| SYMB -> add_symb s
| NUMB -> add_numb (int_of_string s)
2021-08-10 09:21:23 -04:00
let msg = "Periodic table search\n\
elements -gendb file [-dbfile outfile]\n\
elements [-maxmatch n] [-dbfile search] [-name ...] [-symb ...]\n\
"
let setfile name v f =
if f = "" then raise
(Bad (Printf.sprintf "%s requires a filename" name))
else v := f
2021-08-10 09:21:23 -04:00
let spec = [
"-gendb", String (setfile "-gendb" jsonf), "Generate DB file";
2021-08-10 09:21:23 -04:00
"-maxmatch", Set_int maxmatch, "Limit the amount of matches (def. 3)";
"-dbfile", String (setfile "-dbfile" dbfile), "Set the DB filename";
2021-08-10 09:21:23 -04:00
"-name", String add_name, "Search by name (default)";
"-symb", String add_symb, "Search by symbol";
"-num", Int add_numb, "Search by atomic number"
2021-08-10 09:21:23 -04:00
]
let exec() = parse spec add_def msg
end
let () = Opts.exec()
let db = Sqlite3.db_open !Opts.dbfile
let _ = if !Opts.jsonf <> "" then
Makedb.makedb db (if !Opts.jsonf = "-" then
Yojson.Basic.from_channel stdin
else
Yojson.Basic.from_file !Opts.jsonf
)