Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

First experimental port to cmdliner

  • Loading branch information...
commit 419f416472d7a36fc2114a82096cbd0247ad0283 1 parent 24b7a23
Thomas Gazagnaire authored
9  depends.ocp.in
@@ -26,11 +26,6 @@ begin library "graph"
26 26
   generated = true
27 27
 end
28 28
 
29  
-begin library "arg"
30  
-  dirname  = "%{lib}%/ocaml-arg"
31  
-  generated = true
32  
-end
33  
-
34 29
 begin library "cudf"
35 30
   dirname  = "%{lib}%/cudf"
36 31
   generated = true
@@ -48,3 +43,7 @@ begin library "extLib"
48 43
   generated = true
49 44
 end
50 45
 
  46
+begin library "cmdliner"
  47
+  dirname = "%{lib}%/cmdliner"
  48
+  generated = true
  49
+end
2  src/client/client.ocp
@@ -25,7 +25,7 @@ begin program "opam"
25 25
   ]
26 26
   requires = [
27 27
     "opam-lib"
28  
-    "arg"
  28
+    "cmdliner"
29 29
  ]
30 30
 
31 31
 end
148  src/client/opamMain.ml
@@ -14,8 +14,9 @@
14 14
 (***********************************************************************)
15 15
 
16 16
 open OpamTypes
17  
-open SubCommand
  17
+open Cmdliner
18 18
 
  19
+(*
19 20
 let ano_args = ref []
20 21
 let anon s =
21 22
   ano_args := s :: !ano_args
@@ -31,11 +32,6 @@ let noanon cmd s =
31 32
 (* Useful for switch, which can overwrite the default verbose flag *)
32 33
 let quiet = ref false
33 34
 
34  
-let set_root_dir dir =
35  
-  OpamGlobals.root_dir := OpamSystem.real_path dir
36  
-
37  
-let set_switch switch =
38  
-  OpamGlobals.switch := Some switch
39 35
 
40 36
 let global_args = [
41 37
   "--debug"     , Arg.Set OpamGlobals.debug   , " Print internal debug messages (very verbose)";
@@ -469,7 +465,7 @@ let commands = [
469 465
   pin;
470 466
 ]
471 467
 
472  
-let () =
  468
+let f () =
473 469
   Sys.catch_break true;
474 470
   Printexc.register_printer (function
475 471
     | Unix.Unix_error (e,fn, msg) ->
@@ -500,3 +496,141 @@ let () =
500 496
       Printf.fprintf stderr "Fatal error: exception %s\n%s%!"
501 497
         (Printexc.to_string e) bt;
502 498
       exit 2
  499
+
  500
+let global_args = [
  501
+  "--no-checksums", Arg.Clear OpamGlobals.verify_checksums, " Do not verify checksums on download";
  502
+  "--keep-build-dir", Arg.Set OpamGlobals.keep_build_dir, " Keep the build directory";
  503
+]
  504
+
  505
+*)
  506
+type global_options = {
  507
+  debug  : bool;
  508
+  verbose: bool;
  509
+  switch : string option;
  510
+  yes    : bool;
  511
+  root   : string;
  512
+}
  513
+
  514
+let apply f o =
  515
+  OpamGlobals.debug := o.debug;
  516
+  OpamGlobals.verbose := o.verbose;
  517
+  OpamGlobals.switch := o.switch;
  518
+  OpamGlobals.root_dir := OpamSystem.real_path o.root;
  519
+  f
  520
+
  521
+let help copts man_format cmds topic = match topic with
  522
+  | None       -> `Help (`Pager, None) (* help about the program. *)
  523
+  | Some topic ->
  524
+    let topics = "topics" :: cmds in
  525
+    let conv, _ = Cmdliner.Arg.enum (List.rev_map (fun s -> (s, s)) topics) in
  526
+    match conv topic with
  527
+    | `Error e -> `Error (false, e)
  528
+    | `Ok t when t = "topics" -> List.iter print_endline topics; `Ok ()
  529
+    | `Ok t -> `Help (man_format, Some t)
  530
+
  531
+(* Help sections common to all commands *)
  532
+let global_option_section = "COMMON OPTIONS"
  533
+let help_sections = [
  534
+  `S global_option_section;
  535
+  `P "These options are common to all commands.";
  536
+  `S "MORE HELP";
  537
+  `P "Use `$(mname) $(i,COMMAND) --help' for help on a single command.";`Noblank;
  538
+  `P "Use `$(mname) help patterns' for help on patch matching."; `Noblank;
  539
+  `P "Use `$(mname) help environment' for help on environment variables.";
  540
+  `S "BUGS"; `P "Check bug reports at http://bugs.example.org.";]
  541
+
  542
+(* Options common to all commands *)
  543
+let create_global_options debug verbose switch yes root =
  544
+  { debug; verbose; switch; yes; root }
  545
+
  546
+let global_options =
  547
+  let docs = global_option_section in
  548
+  let debug =
  549
+    let doc = "Give only debug output." in
  550
+    Arg.(value & flag & info ["debug"] ~docs ~doc)
  551
+  in
  552
+  let verbose =
  553
+    let quiet =
  554
+      let doc = "Suppress informational output." in
  555
+      false, Arg.info ["q"; "quiet"] ~docs ~doc in
  556
+    let verbose =
  557
+      let doc = "Give verbose output." in
  558
+      true, Arg.info ["v"; "verbose"] ~docs ~doc in
  559
+    Arg.(last & vflag_all [false] [quiet; verbose])
  560
+  in
  561
+  let switch =
  562
+    let doc = Printf.sprintf
  563
+      "Overwrite the compiler switch name%s."
  564
+      (match !OpamGlobals.switch with
  565
+      | None   -> ""
  566
+      | Some s -> Printf.sprintf "(current is %s)" s) in
  567
+    Arg.(value & opt (some string) !OpamGlobals.switch & info ["switch"] ~docs ~doc)
  568
+  in
  569
+  let yes =
  570
+    let doc = "Always answer 'yes' to questions." in
  571
+    Arg.(value & flag & info ["yes"] ~docs ~doc)
  572
+  in
  573
+  let root =
  574
+    let doc = Printf.sprintf
  575
+      "Change root patch (default is %s)"
  576
+      !OpamGlobals.root_dir in
  577
+    Arg.(value & opt string !OpamGlobals.root_dir & info ["root"] ~docs ~doc)
  578
+  in
  579
+  Term.(pure create_global_options $ debug $ verbose $ switch $ yes $ root)
  580
+
  581
+(* Commands *)
  582
+type repo = GIT | LOCAL | HTTP
  583
+let repo_list = [
  584
+  "git"  , GIT;
  585
+  "local", LOCAL;
  586
+  "http" , HTTP;
  587
+]
  588
+let init =
  589
+  let _kind =
  590
+    let doc = "Set the repository kind." in
  591
+    Arg.(value & opt (enum repo_list) HTTP & info ["kind"] ~docv:"KIND" ~doc) in
  592
+  let compiler = Term.pure None in
  593
+  let cores =
  594
+    let doc = "Number of cores." in
  595
+    Arg.(value & opt int 1 & info ["cores"] ~docv:"CORES" ~doc) in
  596
+  let doc = "Initialize opam." in
  597
+  let repository = OpamRepository.default in
  598
+  let man = [
  599
+    `S "DESCRIPTION";
  600
+    `P "Turns the current directory into a Darcs repository. Any
  601
+
  602
+       existing files and subdirectories become ..."
  603
+  ] @ help_sections
  604
+  in
  605
+  Term.(pure apply $ pure OpamClient.init $ global_options $ pure repository $ compiler $ cores),
  606
+  Term.info "initialize" ~sdocs:global_option_section ~doc ~man
  607
+
  608
+let help =
  609
+  let topic =
  610
+    let doc = "The topic to get help on. `topics' lists the topics." in
  611
+    Arg.(value & pos 0 (some string) None & info [] ~docv:"TOPIC" ~doc)
  612
+  in
  613
+  let doc = "display help about opam and opam commands" in
  614
+  let man =
  615
+    [`S "DESCRIPTION";
  616
+     `P "Prints help about opam commands"] @ help_sections
  617
+  in
  618
+  Term.(ret (pure help $ global_options $ Term.man_format $ Term.choice_names $ topic)),
  619
+  Term.info "help" ~doc ~man
  620
+
  621
+let default =
  622
+  let doc = "a Package Manager for OCaml" in
  623
+  let man = help_sections in
  624
+  Term.(ret (pure (fun _ -> `Help (`Pager, None)) $ global_options)),
  625
+  Term.info "opam"
  626
+    ~version:(OpamVersion.to_string OpamVersion.current)
  627
+    ~sdocs:global_option_section
  628
+    ~doc
  629
+    ~man
  630
+
  631
+let cmds = [init; help]
  632
+
  633
+let () =
  634
+  match Term.eval_choice default cmds with
  635
+  | `Error _ -> exit 1
  636
+  | _        -> exit 0

0 notes on commit 419f416

Please sign in to comment.
Something went wrong with that request. Please try again.