From 547e5824e995c65d016c4aa5f562ffb36a9f154c Mon Sep 17 00:00:00 2001 From: Peter McGoron Date: Tue, 10 Aug 2021 10:06:31 -0400 Subject: [PATCH] generate database from command line arguments --- elements.ml | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/elements.ml b/elements.ml index ad99740..a2877fb 100644 --- a/elements.ml +++ b/elements.ml @@ -4,7 +4,7 @@ module Opts = struct type lasttype = NAME | SYMB let ltype = ref NAME - let gendb = ref "" + let jsonf = ref "" let maxmatch = ref 3 let match_name : string list ref = ref [] let match_symb : string list ref = ref [] @@ -25,10 +25,15 @@ module Opts = struct 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 + let spec = [ - "-gendb", Set_string gendb, "Generate DB file"; + "-gendb", String (setfile "-gendb" jsonf), "Generate DB file"; "-maxmatch", Set_int maxmatch, "Limit the amount of matches (def. 3)"; - "-dbfile", Set_string dbfile, "Set the DB filename"; + "-dbfile", String (setfile "-dbfile" dbfile), "Set the DB filename"; "-name", String add_name, "Search by name (default)"; "-symb", String add_symb, "Search by symbol" ] @@ -37,3 +42,11 @@ module Opts = struct end let () = Opts.exec() + +let db = Sqlite3.db_open !Opts.dbfile +if !Opts.jsonf <> "" then + Makedb.makedb db (if !Opts.jsonf = "-" then + Yojson.Basic.from_channel stdin + else + Yojson.Basic.from_file !Opts.jsonf + )