package opam-client
Client library for opam 2.1
Install
Dune Dependency
Authors
-
VVincent Bernardoff <vb@luminar.eu.org>
-
RRaja Boujbel <raja.boujbel@ocamlpro.com>
-
RRoberto Di Cosmo <roberto@dicosmo.org>
-
TThomas Gazagnaire <thomas@gazagnaire.org>
-
LLouis Gesbert <louis.gesbert@ocamlpro.com>
-
FFabrice Le Fessant <Fabrice.Le_fessant@inria.fr>
-
AAnil Madhavapeddy <anil@recoil.org>
-
GGuillem Rieu <guillem.rieu@ocamlpro.com>
-
RRalf Treinen <ralf.treinen@pps.jussieu.fr>
-
FFrederic Tuong <tuong@users.gforge.inria.fr>
Maintainers
Sources
2.1.6.tar.gz
md5=6f69e91a0535297272127e184c314272
sha512=f75d81b4eb19ef6603e7a2a18e1a281ea6f5eea47b820ae888593102db5b98ded5b593b176e70235611b63a07f4a406a57fe016386f4e921f51b55eb62b9864d
doc/src/opam-client/opamArgTools.ml.html
Source file opamArgTools.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
(**************************************************************************) (* *) (* Copyright 2021 OCamlPro *) (* *) (* All rights reserved. This file is distributed under the terms of the *) (* GNU Lesser General Public License version 2.1, with the special *) (* exception on linking described in the file LICENSE. *) (* *) (**************************************************************************) open OpamStd.Op open Cmdliner open OpamCLIVersion.Op let cli2_0 = OpamCLIVersion.of_string "2.0" let cli2_1 = OpamCLIVersion.of_string "2.1" type 'b validity_and_content = { valid: OpamCLIVersion.t; removed: (OpamCLIVersion.t * string option) option; content: 'b; default: bool; } type 'a content = Valid of 'a | Removed of 'a type 'a contented_validity = 'a content validity_and_content type validity = unit validity_and_content let elem_of_vr = function Valid e | Removed e -> e let contented_validity (validity:validity) content : 'a contented_validity = match validity.removed with | None -> { validity with content = Valid content} | Some _ -> { validity with content = Removed content} let is_original_cli validity = OpamCLIVersion.compare validity.valid cli2_0 = 0 let cli_from valid = { valid ; removed = None; content = (); default = false } let cli_between since ?(default=false) ?replaced removal = if since >= removal then OpamConsole.error_and_exit `Internal_error "An option can't be added in %s and removed in %s" (OpamCLIVersion.to_string since) (OpamCLIVersion.to_string removal); { valid = since ; removed = Some (removal, replaced); content = (); default } let cli_original = cli_from cli2_0 let bold = OpamConsole.colorise `bold let string_of_sourced_cli (c,_) = OpamCLIVersion.to_string c let string_of_cli_option cli = if cli = cli2_0 then Printf.sprintf "set %s environment variable to %s" (bold "OPAMCLI") (bold "2.0") else Printf.sprintf "use --cli=%s" (bold @@ OpamCLIVersion.to_string cli) let update_doc_w_cli doc ~cli = function | { valid = c ; removed = None; _} -> if cli @< c then Printf.sprintf "(Since $(b,%s)) %s" (OpamCLIVersion.to_string c) doc else doc | { removed = Some (since, instead); _} -> if cli @< since then doc else Printf.sprintf "Removed in $(b,%s)%s" (OpamCLIVersion.to_string since) (match instead with | Some instead -> Printf.sprintf ", use $(i,%s) instead." instead | None -> ".") (* Error messages *) type target = | Flags of string list | Option of string | Verbatim of string let get_long_form flags = List.fold_left (fun (lgth,long) f -> let flgth = String.length f in if flgth > lgth then (flgth, f) else (lgth, long)) (0,"") flags |> snd let string_of_target = function | Flags flags -> bold "--"^get_long_form flags | Option o -> bold o | Verbatim s -> s let newer_flag_msg cli valid_since target = Printf.sprintf "%s was added in version %s of the opam CLI, \ but version %s has been requested, which is older." target (OpamCLIVersion.to_string valid_since) (string_of_sourced_cli cli) let newer_flag_error cli valid_since targets = let target = string_of_target targets in let msg = newer_flag_msg cli valid_since target in `Error (false, msg) let previously_str removal instead = let previous = string_of_cli_option (OpamCLIVersion.previous removal) in match instead with | Some ist -> Printf.sprintf ". Use %s instead or %s" (bold ist) previous | None -> Printf.sprintf ", %s" previous let older_flag_msg cli removal instead target = Printf.sprintf "%s was removed in version %s of the opam CLI, \ but version %s has been requested%s." target (OpamCLIVersion.to_string removal) (string_of_sourced_cli cli) (previously_str removal instead) let older_flag_error cli removal instead targets = let target = string_of_target targets in let msg = older_flag_msg cli removal instead target in `Error (false, msg) let deprecated_warning removal instead targets = let target = string_of_target targets in OpamConsole.warning "%s was deprecated in version %s of the opam CLI%s." target (OpamCLIVersion.to_string removal) (previously_str removal instead) (* Cli version check *) let cond_new cli c = cli @< c let cond_removed cli removal = cli @>= removal let check_cli_validity_t ~newer ~default_cli ~older ~valid ?(cond=fun x -> x) cli = function | { removed = None ; valid = c; _ } when cond (cond_new cli c) -> newer c | { removed = Some (removal, instead); default = true; _ } when (snd cli = `Default) && OpamCLIVersion.default < removal -> (* default cli case : we dont even check if the condition is required *) default_cli removal instead | { removed = Some (removal, instead); _ } when cond (cond_removed cli removal) -> older removal instead | _ -> valid () let check_cli_validity cli validity ?cond elem targets = check_cli_validity_t cli validity ?cond ~newer:(fun c -> newer_flag_error cli c targets) ~default_cli:(fun removal instead -> deprecated_warning removal instead targets; `Ok elem) ~older:(fun removal instead -> older_flag_error cli removal instead targets) ~valid:(fun () -> `Ok elem) let term_cli_check ~check arg = Term.(ret ((const check) $ (Arg.value arg))) (* Arguments *) let mk_flag ~cli validity ?section flags doc = let doc = update_doc_w_cli doc ~cli validity in let doc = Arg.info ?docs:section ~doc flags in let check elem = check_cli_validity cli validity ~cond:(fun c -> c && elem) elem (Flags flags) in term_cli_check ~check Arg.(flag & doc) let mk_opt ~cli validity ?section ?vopt flags value doc kind default = let doc = update_doc_w_cli doc ~cli validity in let doc = Arg.info ?docs:section ~docv:value ~doc flags in let check elem = check_cli_validity cli validity ~cond:(fun c -> c && default <> elem) elem (Flags flags) in term_cli_check ~check Arg.(opt ?vopt kind default & doc) let mk_opt_all ~cli validity ?section ?vopt ?(default=[]) flags value doc kind = let doc = update_doc_w_cli doc ~cli validity in let doc = Arg.info ?docs:section ~docv:value ~doc flags in let check elem = check_cli_validity cli validity ~cond:(fun c -> c && default <> elem) elem (Flags flags) in term_cli_check ~check Arg.(opt_all ?vopt kind default & doc) let mk_vflag ~cli ?section default flags = let flags = List.map (fun (v,c,f,d) -> contented_validity v c, f, d) flags in let info_flags = List.map (fun (validity, flag, doc) -> let doc = update_doc_w_cli doc ~cli validity in validity.content, Arg.info ?docs:section flag ~doc) flags in let check elem = match OpamStd.List.find_opt (fun (validity, _, _) -> validity.content = elem) flags with | Some (validity, flags, _) -> check_cli_validity cli validity (elem_of_vr elem) (Flags flags) | None -> `Ok (elem_of_vr elem) in term_cli_check ~check Arg.(vflag (Valid default) info_flags) let mk_flag_replaced ~cli ?section flags doc = let flags = List.map (fun (c,f) -> c, true, f, doc) flags in mk_vflag ~cli ?section false flags let mk_vflag_all ~cli ?section ?(default=[]) flags = let flags = List.map (fun (v,c,f,d) -> contented_validity v c, f, d) flags in let info_flags = List.map (fun (validity, flag, doc) -> let doc = update_doc_w_cli doc ~cli validity in validity.content, Arg.info ?docs:section flag ~doc) flags in let check selected = let newer_cli, older_cli = List.fold_left (fun (newer_cli,older_cli as acc) elem -> match OpamStd.List.find_opt (fun (validity, _, _) -> validity.content = elem) flags with | Some (validity, flags, _) -> check_cli_validity_t cli validity ~newer:(fun c -> (flags, c)::newer_cli, older_cli) ~default_cli:(fun _ _ -> acc) ~older:(fun removal instead -> newer_cli, (flags, (removal, instead))::older_cli) ~valid:(fun () -> acc) | None -> acc) ([],[]) selected in let max_cli clis = OpamCLIVersion.to_string @@ match clis with | [] -> assert false | c::cl -> List.fold_left max c cl in let string_of_options options = OpamStd.Format.pretty_list (List.map (fun o -> string_of_target (Flags o)) options) in match newer_cli, older_cli with | [], [] -> `Ok (List.map elem_of_vr selected) | [flags, c], [] -> newer_flag_error cli c (Flags flags) | [], [flags, (c, instead)] -> older_flag_error cli c instead (Flags flags) | _::_, []-> let options, clis = List.split newer_cli in let msg = Printf.sprintf "%s can only be used with at least version %s of the opam \ CLI, but version %s has been requested." (string_of_options options) (max_cli clis) (string_of_sourced_cli cli) in `Error (false, msg) | [], _::_-> let options, clis = List.split older_cli in let clis = List.split clis |> fst in let in_all = match clis with | c::cs when List.for_all ((=) c) cs -> Some c | _ -> None in let msg = Printf.sprintf "%s %swere all removed by version %s of the opam CLI, \ but version %s has been requested." (string_of_options options) (OpamStd.Option.to_string (OpamCLIVersion.to_string @> Printf.sprintf "were all in %s, and ") in_all) (max_cli clis) (string_of_sourced_cli cli) in `Error (false, msg) | _,_ -> let newer, nclis = List.split newer_cli in let older, rclis_ist = List.split older_cli in let rclis, insteads = List.split rclis_ist in let msg = if List.for_all ((<>) None) insteads then Printf.sprintf "This combination of options is not possible: %s require \ at least version %s of the opam CLI and the newer %s \ flags must be used for %s respectively!" (string_of_options newer) (max_cli nclis) (string_of_options older) (OpamStd.Format.pretty_list (List.map (function Some f -> f | None -> assert false) insteads)) else Printf.sprintf "This combination of options is not possible: %s require \ at least version %s of the opam CLI but %s were all \ removed by version %s of the opam CLI!" (string_of_options newer) (max_cli nclis) (string_of_options older) (max_cli rclis) in `Error (false, msg) in let default = List.map (fun x -> Valid x) default in term_cli_check ~check Arg.(vflag_all default info_flags) let string_of_enum enum = Arg.doc_alts_enum (List.map (fun (_, s, v) -> s,v) enum) let mk_enum_opt ~cli validity ?section flags value states doc = let doc = update_doc_w_cli doc ~cli validity in let doc = Arg.info ?docs:section ~docv:value ~doc flags in let check elem = (* first check validity of flag *) let flag_validity = check_cli_validity cli validity ~cond:(fun c -> c && elem <> None) elem (Flags flags) in (* then check validity of the argument *) match flag_validity with | `Ok (Some elem) -> let validity, str, _ = List.find (fun (_,_,v) -> v = elem) states in check_cli_validity cli validity (Some elem) (Verbatim (Printf.sprintf "the %s option for %s" (bold str) (bold "--"^get_long_form flags))) | _ -> flag_validity in let states = List.map (fun (_, s, v) -> s,v) states in term_cli_check ~check Arg.(opt (some (enum states)) None & doc) let mk_enum_opt_all ~cli validity ?section flags value states doc = let doc = update_doc_w_cli doc ~cli validity in let doc = Arg.info ?docs:section ~docv:value ~doc flags in let check elems = (* first check validity of flag *) let flag_validity = check_cli_validity cli validity ~cond:(fun c -> c && elems <> []) elems (Flags flags) in (* then check validity of the argument *) match flag_validity with | `Error _ -> flag_validity | `Ok elems -> let newer_cli, older_cli, valid = List.fold_left (fun (newer_cli,older_cli,valid) elem -> let (validity, str, _) = List.find (fun (_,_,v) -> v = elem) states in check_cli_validity_t cli validity ~newer:(fun c -> (str, c)::newer_cli, older_cli, valid) ~default_cli:(fun _ _ -> newer_cli, older_cli, elem::valid) ~older:(fun removal instead -> newer_cli, (str, (removal, instead))::older_cli, valid) ~valid:(fun () -> newer_cli, older_cli, elem::valid)) ([],[],[]) elems in let max_cli clis = OpamCLIVersion.to_string @@ match clis with | [] -> assert false | c::cl -> List.fold_left max c cl in let long_form_flags = "--"^get_long_form flags in let to_str states = Printf.sprintf "the option%s %s for %s" (match states with [_] -> "s" | _ -> "") (bold @@ OpamStd.Format.pretty_list states) (bold long_form_flags) in match newer_cli, older_cli, List.rev valid with | [], [], elems -> `Ok elems | [str, c], [], [] -> newer_flag_error cli c (Verbatim (to_str [str])) | [str, c], [], elems -> (OpamConsole.warning "%s" (newer_flag_msg cli c (to_str [str])); `Ok elems) | [], [str, (c, instead)], [] -> older_flag_error cli c instead (Verbatim (to_str [str])) | [], [str, (c, instead)], elems -> (OpamConsole.warning "%s" (older_flag_msg cli c instead (to_str [str])); `Ok elems) | _::_, [], elems -> let states, clis = List.split newer_cli in let msg = Printf.sprintf "%s can only be used with at least version %s of the opam \ CLI, but version %s has been requested." (to_str states) (max_cli clis) (string_of_sourced_cli cli) in if elems = [] then `Error (false, msg) else (OpamConsole.warning "%s" msg; `Ok elems) | [], _::_, elems-> let states, clis = List.split older_cli in let clis = List.split clis |> fst in let in_all = match clis with | c::cs when List.for_all ((=) c) cs -> Some c | _ -> None in let msg = Printf.sprintf "%s %swere all removed by version %s of the opam CLI, \ but version %s has been requested." (to_str states) (OpamStd.Option.to_string (OpamCLIVersion.to_string @> Printf.sprintf "were all in %s, and ") in_all) (max_cli clis) (string_of_sourced_cli cli) in if elems = [] then `Error (false, msg) else (OpamConsole.warning "%s" msg; `Ok elems) | _, _, elems -> let newer, nclis = List.split newer_cli in let older, rclis_ist = List.split older_cli in let rclis, insteads = List.split rclis_ist in let msg = if List.for_all ((<>) None) insteads then Printf.sprintf "This combination of %s options is not possible: %s require \ at least version %s of the opam CLI and the newer %s \ flags must be used for %s respectively!" (bold long_form_flags) (bold @@ OpamStd.Format.pretty_list newer) (max_cli nclis) (bold @@ OpamStd.Format.pretty_list older) (OpamStd.Format.pretty_list (List.map (function Some f -> f | None -> assert false) insteads)) else Printf.sprintf "This combination of %s options is not possible: %s require \ at least version %s of the opam CLI but %s were all \ removed by version %s of the opam CLI!" (bold long_form_flags) (bold @@ OpamStd.Format.pretty_list newer) (max_cli nclis) (bold @@ OpamStd.Format.pretty_list older) (max_cli rclis) in if elems = [] then `Error (false, msg) else (OpamConsole.warning "%s" msg; `Ok elems) in let states = List.map (fun (_, s, v) -> s,v) states in term_cli_check ~check Arg.(opt_all (enum states) [] & doc) (* Subcommands *) type 'a subcommand = validity * string * 'a * string list * string type 'a subcommands = 'a subcommand list let mk_subdoc ~cli ?(defaults=[]) commands = let bold s = Printf.sprintf "$(b,%s)" s in let it s = Printf.sprintf "$(i,%s)" s in `S Manpage.s_commands :: (List.map (function | "", name -> `P (Printf.sprintf "Without argument, defaults to %s." (bold name)) | arg, default -> `I (it arg, Printf.sprintf "With a %s argument, defaults to %s %s." (it arg) (bold default) (it arg)) ) defaults) @ List.map (fun (validity, c, _, args,d) -> let cmds = bold c ^ " " ^ OpamStd.List.concat_map " " it args in let d = update_doc_w_cli d ~cli validity in `I (cmds, d) ) commands let mk_subcommands_aux ~cli my_enum commands = let commands = List.map (fun (v,n,c,a,d) -> contented_validity v c, n, a, d) commands in let command = let doc = Arg.info ~docv:"COMMAND" [] in let scommand = List.rev_map (fun (v,f,_,_) -> f,v.content) commands in let check = function | None -> `Ok None | Some elem -> match OpamStd.List.find_opt (fun (validity, _, _, _) -> validity.content = elem) commands with | Some (validity, sbcmd, _,_) -> check_cli_validity cli validity (Some (elem_of_vr elem)) (Option sbcmd) | None -> `Ok (Some (elem_of_vr elem)) in term_cli_check ~check Arg.(pos 0 (some & my_enum scommand) None & doc) in let params = let doc = Arg.info ~doc:"Optional parameters." [] in Arg.(value & pos_right 0 string [] & doc) in command, params let mk_subcommands ~cli commands = mk_subcommands_aux ~cli Arg.enum commands type 'a default = [> `default of string] as 'a let mk_subcommands_with_default ~cli commands = let enum_with_default_valrem sl = let parse, print = Arg.enum sl in let parse s = match parse s with | `Ok x -> `Ok (x) | _ -> `Ok (Valid (`default s)) in parse, print in mk_subcommands_aux ~cli enum_with_default_valrem commands let bad_subcommand ~cli subcommands (command, usersubcommand, userparams) = match usersubcommand with | None -> `Error (false, Printf.sprintf "Missing subcommand. Valid subcommands are %s." (OpamStd.Format.pretty_list (OpamStd.List.filter_map (fun (validity,sb,_,_,_) -> match validity with | {valid = c; removed = None; _} when cli @>= c -> None | {removed = Some (c,_); _} when cli @< c -> None | _ -> Some sb) subcommands))) | Some (`default cmd) -> `Error (true, Printf.sprintf "Invalid %s subcommand %S" command cmd) | Some usersubcommand -> let exe = Filename.basename Sys.executable_name in match List.find_all (fun (_,_,cmd,_,_) -> cmd = usersubcommand) subcommands with | [ _, name, _, args, _doc] -> let usage = Printf.sprintf "%s %s [OPTION]... %s %s" exe command name (String.concat " " args) in if List.length userparams < List.length args then `Error (false, Printf.sprintf "%s: Missing argument.\nUsage: %s\n" exe usage) else `Error (false, Printf.sprintf "%s: Too many arguments.\nUsage: %s\n" exe usage) | _ -> `Error (true, Printf.sprintf "Invalid %s subcommand" command) (* Commands *) type command = unit Term.t * Term.info (* As [term_info] is defined later, we need to have it as argument *) let mk_command ~cli validity term_info name ~doc ~man cmd = let doc = update_doc_w_cli doc ~cli validity in let info = term_info ~cli name ~doc ~man in let check = check_cli_validity cli validity () (Option name) |> Term.const |> Term.ret in Term.(cmd $ check), info let mk_command_ret ~cli validity term_info name ~doc ~man cmd = let doc = update_doc_w_cli doc ~cli validity in let info = term_info ~cli name ~doc ~man in let check = check_cli_validity cli validity () (Option name) |> Term.const |> Term.ret in Term.(ret (cmd $ check)), info (* Environment variables *) let check_cli_env_validity cli validity var cons = let is_defined () = OpamStd.Config.env (fun x -> x) var <> None in let ovar = "OPAM"^var in match validity with | { removed = None ; valid = c; _ } when cond_new cli c -> if is_defined () then OpamConsole.warning "%s was ignored because CLI %s \ was requested and it was introduced in %s." ovar (string_of_sourced_cli cli) (OpamCLIVersion.to_string c); None | { removed = Some (removal, instead); _ } when cond_removed cli removal -> if is_defined () then OpamConsole.warning "%s was ignored because CLI %s \ was requested and it was removed in %s%s." ovar (string_of_sourced_cli cli) (OpamCLIVersion.to_string removal) (previously_str removal instead); None | _ -> Some (cons var) let env_with_cli environment = let doc_env cli = List.map (fun (var, validity, _cons, doc) -> let doc = update_doc_w_cli doc ~cli validity in `P (Printf.sprintf "$(i,OPAM%s) %s" var doc)) environment in let init_env cli = OpamStd.List.filter_map (fun (var, validity, cons, _doc) -> check_cli_env_validity cli validity var cons) environment |> OpamStd.Config.E.updates in doc_env, init_env
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>