Permalink
Browse files

ocp-get now have a nice command line interface (with subcommands) (us…

…ing ocaml-arg)
  • Loading branch information...
samoht committed Mar 22, 2012
1 parent 53ec046 commit f01c78cb79937f32c7d2bc5c6fc76c4945d08f73
Showing with 162 additions and 72 deletions.
  1. +6 −2 Makefile
  2. +9 −0 ocp-get.ocp
  3. +143 −66 src/ocp_get.ml
  4. +1 −1 tests/packages/P2-1/build.sh
  5. +1 −1 tests/packages/P3-1-weird.version/myocamlbuild.ml
  6. +2 −2 tests/packages/P4-1/build.sh
View
@@ -43,7 +43,7 @@ ocp-get: _obuild/ocp-get/ocp-get.asm
compile: ./_obuild/unixrun clone
$(OCPBUILD) -init -scan -sanitize $(TARGET)
-clone: cudf extlib ocaml-re ocamlgraph dose
+clone: cudf extlib ocaml-re ocamlgraph dose ocaml-arg
cudf:
$(WGET) http://www.ocamlpro.com/pub/cudf.tar.bz2
@@ -58,6 +58,10 @@ dose:
$(WGET) http://www.ocamlpro.com/pub/dose.tar.bz2
tar xvfj dose.tar.bz2
+ocaml-arg:
+ $(WGET) http://www.ocamlpro.com/pub/ocaml-arg.tar.bz2
+ tar xvfj ocaml-arg.tar.bz2
+
# ocaml-pcre:
# $(WGET) http://hg.ocaml.info/release/pcre-ocaml/archive/release-6.2.5.tar.gz
# tar xfvz release-6.2.5.tar.gz
@@ -80,7 +84,7 @@ ocaml-re:
distclean:
rm -f *.tar.gz *.tar.bz2
- rm -rf dose cudf extlib ocaml-re ocamlgraph
+ rm -rf dose cudf extlib ocaml-re ocamlgraph ocaml-arg
rm -rf _obuild
.PHONY: tests
View
@@ -43,6 +43,14 @@ begin library "re"
]
end
+begin library "ocaml-arg"
+ files = [
+ "ocaml-arg/src/subCommand.ml"
+ "ocaml-arg/src/formatExt.ml"
+ "ocaml-arg/src/argExt.ml"
+ ]
+end
+
begin library "cudf"
sort = true
files = [
@@ -185,6 +193,7 @@ begin library "ocp-get-lib"
"dose"
"bat"
"unix"
+ "ocaml-arg"
]
end
View
@@ -18,13 +18,7 @@ open Path
open Server
open Solver
open Client
-
-let usage =
- Printf.sprintf "%s [init|info|config|install|update|upgrade|upload|remove]" Sys.argv.(0)
-
-let nice_exit () =
- Printf.eprintf "%s\n%!" usage;
- exit 2
+open SubCommand
let version () =
Printf.printf "\
@@ -36,75 +30,158 @@ This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
Sys.argv.(0) Globals.version
-let port = ref Globals.default_port
let ano_args = ref []
+let anon s =
+ ano_args := s :: !ano_args
+
+let bad_argument () =
+ raise (Arg.Bad "Invalid argument")
+
+let noanon s =
+ raise (Arg.Bad (s ^ ": Invalid argument"))
+
let () = Globals.root_path := Globals.default_opam_path
-let args = Arg.align [
+let global_args = Arg.align [
"--debug" , Arg.Set Globals.debug, " Print more debug messages";
"--version", Arg.Unit version, " Display version information";
"--root" , Arg.Set_string Globals.root_path,
(Printf.sprintf " Change root path (default is %s)" Globals.default_opam_path)
]
-let _ = Arg.parse args (fun s -> ano_args := s :: !ano_args) usage
+let parse_args fn () =
+ fn (List.rev !ano_args)
+
+(* ocp-get init [HOSTNAME[:PORT]]*)
+let init = {
+ name = "init";
+ usage = "";
+ synopsis = "Initial setup";
+ help = "Create the initial config files";
+ specs = [];
+ anon;
+ main =
+ parse_args (function
+ | [] -> Client.init (url Globals.default_hostname Globals.default_port)
+ | [ host] -> Client.init (url host Globals.default_port)
+ | [ host; port] ->
+ let port =
+ try int_of_string port
+ with _ -> failwith (port ^ " is not a valid port") in
+ Client.init (url host port)
+ | _ -> bad_argument ())
+}
+
+(* ocp-get info [PACKAGE] *)
+let info = {
+ name = "info";
+ usage = "[package]*";
+ synopsis = "Display information about packages";
+ help = "";
+ specs = [];
+ anon;
+ main =
+ parse_args (function
+ | [] -> Client.info None
+ | l -> List.iter (fun name -> Client.info (Some (Name name))) l)
+}
+
+(* ocp-get config [R] [Include|Bytelink|Asmlink] PACKAGE *)
+let config =
+ let recursive = ref false in
+ let command = ref None in
+ let set_include () = command := Some Client.Include in
+ let set_asmlink () = command := Some Client.Asmlink in
+ let set_bytelink () = command := Some Client.Bytelink in
+ {
+ name = "config";
+ usage = "[package]+";
+ synopsis = "Display configuration options for packages";
+ help = "";
+ specs = [
+ ("-r", Arg.Set recursive , " Recursive search");
+ ("-I", Arg.Unit set_include , " Display include options");
+ ("-bytelink", Arg.Unit set_bytelink, " Display bytecode link options");
+ ("-asmlink" , Arg.Unit set_asmlink , " Display native link options");
+ ];
+ anon;
+ main =
+ function () ->
+ let names = List.rev !ano_args in
+ let command = match !command with
+ | None -> raise (Arg.Bad "Missing command [-I|-asmlink|-bytelink]")
+ | Some c -> c in
+ Client.config !recursive command (List.map (fun n -> Name n) names);
+ }
+
+(* ocp-get install PACKAGE *)
+let install = {
+ name = "install";
+ usage = "[package]+";
+ synopsis = "Install a package";
+ help = "";
+ specs = [];
+ anon;
+ main = parse_args (List.iter (fun name -> Client.install (Name name)))
+}
+
+(* ocp-get update *)
+let update = {
+ name = "update";
+ usage = "[package]+";
+ synopsis = "Update the installed package to latest version";
+ help = "";
+ specs = [];
+ anon = noanon;
+ main = Client.update;
+}
+
+(* ocp-get upgrade *)
+let upgrade = {
+ name = "upgrade";
+ usage = "";
+ synopsis = "Upgrade the list of available package";
+ help = "";
+ specs = [];
+ anon = noanon;
+ main = Client.upgrade;
+}
+
+(* ocp-get upload PACKAGE *)
+let upload = {
+ name = "upload";
+ usage = "";
+ synopsis = "Upload a package to the server";
+ help = "";
+ specs = [];
+ anon;
+ main = parse_args (List.iter Client.upload);
+}
+
+(* ocp-get remove PACKAGE *)
+let remove = {
+ name = "remove";
+ usage = "";
+ synopsis = "Remove a package";
+ help = "";
+ specs = [];
+ anon;
+ main = parse_args (List.iter (fun n -> Client.remove (Name n)));
+}
+
+let commands = [
+ init;
+ info;
+ config;
+ install;
+ update;
+ upgrade;
+ upload;
+ remove;
+]
let () =
Globals.log "CLIENT" "Root path is %s" !Globals.root_path;
-
- let error msg =
- Printf.eprintf "%s\n" msg;
- nice_exit () in
-
- let err l = error (String.concat " " l) in
-
- match List.rev !ano_args with
- | [] -> nice_exit ()
-
- (* ocp-get init [HOSTNAME[:PORT]]*)
- | ["init"] -> Client.init (url Globals.default_hostname Globals.default_port)
- | ["init"; host] -> Client.init (url host Globals.default_port)
- | ["init"; host; port] ->
- let port =
- try int_of_string port
- with _ -> failwith (port ^ " is not a valid port") in
- Client.init (url host port)
-
- (* ocp-get info [PACKAGE] *)
- | ["info"] -> Client.info None
- | ["info"; name] -> Client.info (Some (Name name))
-
- (* ocp-get config [R] [Include|Bytelink|Asmlink] PACKAGE *)
- | "config" :: l_arg ->
- let is_rec, req, names = match l_arg with
- | "R"::r :: names
- | r ::"R":: names -> true , r, names
- | r :: names -> false, r, names
- | _ -> err l_arg in
- let req = match req with
- | "Include" -> Client.Include
- | "Bytelink" -> Client.Bytelink
- | "Asmlink" -> Client.Asmlink
- | _ -> err l_arg in
- Client.config is_rec req (List.map (fun n -> Name n) names)
-
- (* ocp-get install PACKAGE *)
- | ["install"; name] -> Client.install (Name name)
-
- (* ocp-get update *)
- | ["update"] -> Client.update ()
-
- (* ocp-get upgrade *)
- | ["upgrade"] -> Client.upgrade ()
-
- (* ocp-get upload PACKAGE *)
- | ["upload"; s] -> Client.upload s
-
- (* ocp-get remove PACKAGE *)
- | ["remove"; name] -> Client.remove (Name name)
-
- | l -> err l
-
-
-
+ List.iter SubCommand.register commands;
+ ArgExt.parse global_args
@@ -1,7 +1,7 @@
#!/bin/bash
OCPGET="ocp-get --root /tmp/OPAM.TEST"
-FLAGS="`${OCPGET} config Include P1`"
+FLAGS="`${OCPGET} config -I P1`"
echo "Bytecode Compilation"
ocamlopt ${FLAGS} -a p2.ml -o p2.cmxa
@@ -4,7 +4,7 @@ open Ocamlbuild_plugin
let ocp_get pkg =
Ocamlbuild_pack.My_unix.run_and_open
- (Printf.sprintf "ocp-get --root %s config R Include %s"
+ (Printf.sprintf "ocp-get --root %s config -r -I %s"
(Unix.getenv "OPAM_ROOT")
(Filename.quote pkg))
input_line
@@ -1,8 +1,8 @@
#!/bin/bash
OCPGET="ocp-get --root /tmp/OPAM.TEST"
-INCLUDES0="`${OCPGET} config R Include P2 P3`"
-LINK0="`${OCPGET} config R Asmlink P2 P3`"
+INCLUDES0="`${OCPGET} config -r -I P2 P3`"
+LINK0="`${OCPGET} config -r -asmlink P2 P3`"
INCLUDES=${INCLUDES0// /,}
LINK=${LINK0// /,}

0 comments on commit f01c78c

Please sign in to comment.