<commit_before>open ListApis_t

open Utils

let cmdliner_type_of_param p = match p.ty with
  | "string" -> "string"
  | "uuid" -> "string"
  | "boolean" -> "bool"
  | "integer" -> "int";
  | "long" -> "Int64";
  | "short" -> "int";
  | "map" -> "string";
  | "date" -> "string";
  | "tzdate" -> "string";
  | "list" -> "String.concat \",\""
  | x -> failwith x

let cmdliner_default_of_param p =
  if not(p.required) then "None" else match p.ty with
  | "string" -> "\"\""
  | "uuid" -> "\"\""
  | "boolean" -> "false"
  | "integer" -> "0";
  | "long" -> "0L";
  | "short" -> "0";
  | "map" -> "\"\"";
  | "date" -> "\"\"";
  | "tzdate" -> "\"\"";
  | "list" -> "\"\""
  | x -> failwith x

(* A commandliner term to construct the arguments *)
let output_term write { api_name; params } =
  write (Printf.sprintf "let %s_term =\n" (ocaml_escape api_name));
  let indent x = write ("  " ^ x) in
  let indent2 x = write ("    " ^ x) in
  List.iter
    (fun p ->
      indent (Printf.sprintf "let %s =\n" (name_of_param p));
      indent2 (Printf.sprintf "let doc = \"%s\" in\n" p.description);
      indent2 (Printf.sprintf "Arg.(value & %s %s & info [ \"%s\" ] ~docv:\"%s\" ~doc) in\n"
        (cmdliner_type_of_param p) (cmdliner_default_of_param p)
        p.name (String.uppercase p.name));
    ) params;
  indent (Printf.sprintf "Term.(pure %s.Internal.make_opt %s)"
    (ocaml_module api_name)
    (String.concat "" (List.map (fun p -> Printf.sprintf "$ %s " (name_of_param
    p)) params)));
  write "\n\n"

(* An implementation *)
let output_impl write { api_name; params } =
  write (Printf.sprintf "let %s_impl common args =\n" (ocaml_escape api_name));
  let indent x = write ("  " ^ x) in
  indent (Printf.sprintf "let uri = Cloudstack.%s.request common args in\n"
    (ocaml_module api_name));
  indent "let t =\n";
  let indent2 x = write ("    " ^ x) in
  indent2 "Cloudstack_lwt_unix.get uri >>= fun response ->\n";
  indent2 "Printf.fprintf stdout \"%s\\n\" response;\n";
  indent2 "return (`Ok ()) in\n";
  indent "try Lwt_main.run t with e -> exit 1";
  write "\n\n"

(* A commandliner command *)
let output_cmd write { api_name; api_description; params } =
  write (Printf.sprintf "let %s_cmd =\n" (ocaml_escape api_name));
  let indent x = write ("  " ^ x) in
  indent (Printf.sprintf "let doc = \"%s\" in\n" api_name);
  indent "let man = [\n";
  let indent2 x = write ("    " ^ x) in
  indent2 "`S \"DESCRIPTION\";\n";
  indent2 (Printf.sprintf "`P \"%s\";\n" api_description);
  indent "] in\n";
  indent (Printf.sprintf "Term.(ret(pure %s_impl $ common_options_t $ %s_term)),\n"
    (ocaml_escape api_name) (ocaml_escape api_name));
  indent (Printf.sprintf "Term.info \"%s\" ~sdocs:_common_options ~doc ~man" api_name);
  write "\n\n"

let apis_ml { listapisresponse = { api = all } } =
  with_output_channel "cmdliner_commands.ml"
    (fun oc ->
      let write = output_string oc in
      autogenerated_banner write;
      List.iter (fun api ->
        output_term write api;
        output_impl write api;
        output_cmd write api;
      ) all
    )<commit_msg>generator: make a list of all commands<commit_after>open ListApis_t

open Utils

let cmdliner_type_of_param p = match p.ty with
  | "string" -> "string"
  | "uuid" -> "string"
  | "boolean" -> "bool"
  | "integer" -> "int";
  | "long" -> "Int64";
  | "short" -> "int";
  | "map" -> "string";
  | "date" -> "string";
  | "tzdate" -> "string";
  | "list" -> "String.concat \",\""
  | x -> failwith x

let cmdliner_default_of_param p =
  if not(p.required) then "None" else match p.ty with
  | "string" -> "\"\""
  | "uuid" -> "\"\""
  | "boolean" -> "false"
  | "integer" -> "0";
  | "long" -> "0L";
  | "short" -> "0";
  | "map" -> "\"\"";
  | "date" -> "\"\"";
  | "tzdate" -> "\"\"";
  | "list" -> "\"\""
  | x -> failwith x

(* A commandliner term to construct the arguments *)
let output_term write { api_name; params } =
  write (Printf.sprintf "let %s_term =\n" (ocaml_escape api_name));
  let indent x = write ("  " ^ x) in
  let indent2 x = write ("    " ^ x) in
  List.iter
    (fun p ->
      indent (Printf.sprintf "let %s =\n" (name_of_param p));
      indent2 (Printf.sprintf "let doc = \"%s\" in\n" p.description);
      indent2 (Printf.sprintf "Arg.(value & %s %s & info [ \"%s\" ] ~docv:\"%s\" ~doc) in\n"
        (cmdliner_type_of_param p) (cmdliner_default_of_param p)
        p.name (String.uppercase p.name));
    ) params;
  indent (Printf.sprintf "Term.(pure %s.Internal.make_opt %s)"
    (ocaml_module api_name)
    (String.concat "" (List.map (fun p -> Printf.sprintf "$ %s " (name_of_param
    p)) params)));
  write "\n\n"

(* An implementation *)
let output_impl write { api_name; params } =
  write (Printf.sprintf "let %s_impl common args =\n" (ocaml_escape api_name));
  let indent x = write ("  " ^ x) in
  indent (Printf.sprintf "let uri = Cloudstack.%s.request common args in\n"
    (ocaml_module api_name));
  indent "let t =\n";
  let indent2 x = write ("    " ^ x) in
  indent2 "Cloudstack_lwt_unix.get uri >>= fun response ->\n";
  indent2 "Printf.fprintf stdout \"%s\\n\" response;\n";
  indent2 "return (`Ok ()) in\n";
  indent "try Lwt_main.run t with e -> exit 1";
  write "\n\n"

(* A commandliner command *)
let output_cmd write { api_name; api_description; params } =
  write (Printf.sprintf "let %s_cmd =\n" (ocaml_escape api_name));
  let indent x = write ("  " ^ x) in
  indent (Printf.sprintf "let doc = \"%s\" in\n" api_name);
  indent "let man = [\n";
  let indent2 x = write ("    " ^ x) in
  indent2 "`S \"DESCRIPTION\";\n";
  indent2 (Printf.sprintf "`P \"%s\";\n" api_description);
  indent "] in\n";
  indent (Printf.sprintf "Term.(ret(pure %s_impl $ common_options_t $ %s_term)),\n"
    (ocaml_escape api_name) (ocaml_escape api_name));
  indent (Printf.sprintf "Term.info \"%s\" ~sdocs:_common_options ~doc ~man" api_name);
  write "\n\n"

(* A complete list of all commands *)
let output_all write apis =
  write "let all = [\n";
  let indent x = write ("  " ^ x) in
  List.iter (fun { api_name } ->
    indent (Printf.sprintf "%s_cmd;\n" (ocaml_escape api_name))
  ) apis; 
  write "]\n"

let apis_ml { listapisresponse = { api = all } } =
  with_output_channel "cmdliner_commands.ml"
    (fun oc ->
      let write = output_string oc in
      autogenerated_banner write;
      List.iter (fun api ->
        output_term write api;
        output_impl write api;
        output_cmd write api;
      ) all;
      output_all write all
    )
{'fraction_non_alphanumeric': 0.18331791551032994, 'fraction_numerical': 0.004933703361085414, 'mean_word_length': 2.8937575030012006, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7216197', 'n_tokens_mistral': 2437, 'n_tokens_neox': 2452, 'n_words': 660}