From f18e5ec19f9b3c8c8e9b082951334608b8e5aadc Mon Sep 17 00:00:00 2001 From: Johann Rudloff Date: Sat, 13 Jul 2019 14:29:37 +0200 Subject: [PATCH] Add command line parser and basic command line flags for input and output. --- bin/eeschema2netlist.ml | 34 +++++++++++++++++++++++++++++++--- eeschema2netlist.opam | 1 + lib/parser.ml | 2 ++ lib/util.ml | 10 ++++++++++ lib/version.ml | 1 + 5 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 lib/util.ml create mode 100644 lib/version.ml diff --git a/bin/eeschema2netlist.ml b/bin/eeschema2netlist.ml index abd4c70..a75d4a7 100644 --- a/bin/eeschema2netlist.ml +++ b/bin/eeschema2netlist.ml @@ -1,4 +1,32 @@ -let compdef_filename = "data/minimal/minimal-cache.lib" -let sch_filename = "data/minimal/minimal.sch" +open BatOptParse -let () = Eeschema.Parser.convert compdef_filename sch_filename "out2.cir" +let main () = + let overwrite_opt = StdOpt.store_true () in + let out_filename_opt = Opt.value_option "FILE" (Some None) (BatOption.some) (fun _exn file -> "error: " ^ file) in + let optparser = OptParser.make ~version:Eeschema.Version.version () in + OptParser.add optparser + ~help:"overwrite output file" + ~short_name:'f' + ~long_name:"overwrite" + overwrite_opt; + OptParser.add optparser + ~help:"output filename" + ~short_name:'o' + ~long_name:"output" + out_filename_opt; + let args = OptParser.parse_argv optparser in + let overwrite = Opt.get overwrite_opt in + match args with + | [input_filename] -> + let compdef_filename = Eeschema.Parser.cache_lib input_filename in + let output_filename = match (Opt.get out_filename_opt) with + | Some o -> o + | None -> Eeschema.Util.replace_suffix_exn input_filename ".sch" ".cir" (Invalid_argument "no output filename given, and unable to determine one automatically") + in + if Sys.file_exists output_filename && not overwrite then failwith "output file exists (specify \"-f\" to overwrite)"; + Printf.printf "f: %s\nc: %s\no: %s\n" input_filename compdef_filename output_filename; + Eeschema.Parser.convert compdef_filename input_filename output_filename + | [] -> failwith "input filename missing" + | _::extra::_ -> failwith ("unexpected extra argument: " ^ extra) + +let () = main () diff --git a/eeschema2netlist.opam b/eeschema2netlist.opam index 6d01776..2b4d8c6 100644 --- a/eeschema2netlist.opam +++ b/eeschema2netlist.opam @@ -1,5 +1,6 @@ opam-version: "2.0" name: "eeschema2netlist" +version: "0.1.0" maintainer: "Johann Rudloff " authors: "Johann Rudloff " homepage: "https://git.sinyax.net/cypheon/eeschame2netlist" diff --git a/lib/parser.ml b/lib/parser.ml index 2abb2df..7b87e94 100644 --- a/lib/parser.ml +++ b/lib/parser.ml @@ -57,6 +57,8 @@ let resolve compdefs (version, items) = items=List.map (resolve_item compdefs) items; } +let cache_lib sch_filename = + BatOption.get_exn (Util.replace_suffix sch_filename ".sch" "-cache.lib") (Invalid_argument ("can not find cache lib for file: " ^ sch_filename)) let convert compdef_filename sch_filename output_filename = match process Parse_lib.main Lex_lib.read format_compdeflist compdef_filename with | Some compdefs -> begin diff --git a/lib/util.ml b/lib/util.ml new file mode 100644 index 0000000..5d728da --- /dev/null +++ b/lib/util.ml @@ -0,0 +1,10 @@ +open BatString + +let replace_suffix str suffix_to_remove suffix_to_add = if ends_with str suffix_to_remove + then Some ((slice ~last:(-length suffix_to_remove) str) ^ suffix_to_add) + else None + +let replace_suffix_exn a b c err = match replace_suffix a b c with + | Some r -> r + | None -> raise err + diff --git a/lib/version.ml b/lib/version.ml new file mode 100644 index 0000000..effb246 --- /dev/null +++ b/lib/version.ml @@ -0,0 +1 @@ +let version = "0.1.0" -- 2.38.5