123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134(*---------------------------------------------------------------------------
Copyright (c) 2018 The b0 programmers. All rights reserved.
SPDX-License-Identifier: ISC
---------------------------------------------------------------------------*)openB0_stdopenResult.SyntaxopenCmdlinerletoutput_details=B0_std_cli.output_details()letlog_format=B0_memo_cli.Log.format_cli()letmemo_op_query=B0_memo_cli.Op.query_cli()letno_pager=B0_pager.no_pager()letdef_conv(moduleDef:B0_def.S)=letcomplete_defs_ctx~token=letcomplete_defu=letname=Def.nameuinifnot(String.starts_with~prefix:tokenname)thenNoneelseSome(Arg.Completion.stringname~doc:(Def.docu))inOk(List.filter_mapcomplete_def(Def.list()))inletcompletion=Arg.Completion.makecomplete_defsinArg.Conv.of_convArg.string~completion(* Specifying units and packs *)letget_excluded_units~x_units~x_packs=let*units=B0_unit.get_list_or_hint~all_if_empty:falsex_unitsinlet*packs=B0_pack.get_list_or_hint~all_if_empty:falsex_packsinletadd_unitaccu=B0_unit.Set.adduaccinletadd_pack_unitspacc=List.fold_leftadd_unitacc(B0_pack.unitsp)inletpacks=B0_pack.Set.of_listpacksinOk(B0_pack.Set.foldadd_pack_unitspacks(B0_unit.Set.of_listunits))letunit_conv=def_conv(moduleB0_unit)letpack_conv=def_conv(moduleB0_pack)letuse_units?docs?(doc="Use unit $(docv).")()=Arg.(value&opt_allunit_conv[]&info["u";"unit"]?docs~doc~docv:"UNIT")letuse_x_units?docs?(doc="Exclude unit $(docv). Takes over inclusion.")()=letdocv="UNIT"inArg.(value&opt_allunit_conv[]&info["x";"x-unit"]?docs~doc~docv)letuse_packs?docs?(doc="Use pack $(docv).")()=Arg.(value&opt_allpack_conv[]&info["p";"pack"]?docs~doc~docv:"PACK")letuse_x_packs?docs?(doc="Exclude pack $(docv). Takes over inclusion.")()=letdocv="PACK"inArg.(value&opt_allpack_conv[]&info["X";"x-pack"]?docs~doc~docv)letbuild_units=use_units~doc:"Build unit $(docv). Repeatable."()letbuild_x_units=letdoc="Exclude unit $(docv) from the build. Takes over inclusion."inuse_x_units~doc()letbuild_packs=use_packs~doc:"Build pack $(docv). Repeteable."()letbuild_x_packs=letdoc="Exclude units in pack $(docv) from the build. Takes over inclusion."inuse_x_packs~doc()letact_on_units_posn?(doc="The $(docv) to act on. All of them if unspecified.")~first()=Arg.(value&pos_right(first-1)unit_conv[]&info[]~doc~docv:"UNIT")letact_on_units_pos0=act_on_units_posn~first:0()letact_on_units_pos1=act_on_units_posn~first:1()letact_on_packs_posn?(doc="The $(docv) to act on. All of them if unspecified.")~first()=Arg.(value&pos_right(first-1)pack_conv[]&info[]~doc~docv:"PACK")letact_on_packs_pos0=act_on_packs_posn~first:0()letact_on_packs_pos1=act_on_packs_posn~first:1()(* Metadata keys *)letrequired_metadata_key_pos0=letdoc="The metadata key $(docv) to get."anddocv="KEY"inArg.(required&pos0(somestring)None&info[]~doc~docv)(* b0 directory *)letb0_dir_var=Cmd.Env.info"B0_DIR"letb0_dirname="_b0"letb0_dir?(opts=["b0-dir"])?(docs=Manpage.s_common_options)?(doc="Use $(docv) for the b0 directory.")?doc_none:(absent="$(b,_b0) in root directory")?(env=b0_dir_var)()=Arg.(value&opt(someB0_std_cli.dirpath)None&infoopts~env~absent~doc~docs)letget_b0_dir~cwd~root~b0_dir=matchb0_dirwith|None->Fpath.(root/b0_dirname)|Somed->Fpath.(cwd//d)letget_b0_dir_path~cwd~b0_dirdefaultp=matchpwith|None->Fpath.(b0_dir/default)|Somep->Fpath.(cwd//p)letfind_dir_with_b0_dir~start=letrecloopp=ifFpath.is_rootpthenNoneelsematchOs.Dir.existsFpath.(p/b0_dirname)with|Error_|Okfalse->loop(Fpath.parentp)|Oktrue->SomepinifFpath.is_relativestartthenNoneelse(loopstart)(* File cache directory *)letget_cache_dir~cwd~b0_dir~cache_dir=get_b0_dir_path~cwd~b0_dirB0_memo_cli.File_cache.dirnamecache_dir