| 1 | #include "arg.h" |
| 2 | #include "common.h" |
| 3 | |
| 4 | #include <fstream> |
| 5 | #include <string> |
| 6 | |
| 7 | // Export usage message (-h) to markdown format |
| 8 | |
| 9 | static void (std::ofstream & file) { |
| 10 | file << "| Argument | Explanation |\n" ; |
| 11 | file << "| -------- | ----------- |\n" ; |
| 12 | } |
| 13 | |
| 14 | static void write_table_entry(std::ofstream & file, const common_arg & opt) { |
| 15 | file << "| `" ; |
| 16 | // args |
| 17 | for (const auto & arg : opt.args) { |
| 18 | if (arg == opt.args.front()) { |
| 19 | file << arg; |
| 20 | if (opt.args.size() > 1) file << ", " ; |
| 21 | } else { |
| 22 | file << arg << (arg != opt.args.back() ? ", " : "" ); |
| 23 | } |
| 24 | } |
| 25 | // value hint |
| 26 | if (opt.value_hint) { |
| 27 | std::string md_value_hint(opt.value_hint); |
| 28 | string_replace_all(s&: md_value_hint, search: "|" , replace: "\\|" ); |
| 29 | file << " " << md_value_hint; |
| 30 | } |
| 31 | if (opt.value_hint_2) { |
| 32 | std::string md_value_hint_2(opt.value_hint_2); |
| 33 | string_replace_all(s&: md_value_hint_2, search: "|" , replace: "\\|" ); |
| 34 | file << " " << md_value_hint_2; |
| 35 | } |
| 36 | // help text |
| 37 | std::string md_help(opt.help); |
| 38 | string_replace_all(s&: md_help, search: "\n" , replace: "<br/>" ); |
| 39 | string_replace_all(s&: md_help, search: "|" , replace: "\\|" ); |
| 40 | file << "` | " << md_help << " |\n" ; |
| 41 | } |
| 42 | |
| 43 | static void write_table(std::ofstream & file, std::vector<common_arg *> & opts) { |
| 44 | write_table_header(file); |
| 45 | for (const auto & opt : opts) { |
| 46 | write_table_entry(file, opt: *opt); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | static void export_md(std::string fname, llama_example ex) { |
| 51 | std::ofstream file(fname, std::ofstream::out | std::ofstream::trunc); |
| 52 | |
| 53 | common_params params; |
| 54 | auto ctx_arg = common_params_parser_init(params, ex); |
| 55 | |
| 56 | std::vector<common_arg *> common_options; |
| 57 | std::vector<common_arg *> sparam_options; |
| 58 | std::vector<common_arg *> specific_options; |
| 59 | for (auto & opt : ctx_arg.options) { |
| 60 | // in case multiple LLAMA_EXAMPLE_* are set, we prioritize the LLAMA_EXAMPLE_* matching current example |
| 61 | if (opt.is_sparam) { |
| 62 | sparam_options.push_back(x: &opt); |
| 63 | } else if (opt.in_example(ex: ctx_arg.ex)) { |
| 64 | specific_options.push_back(x: &opt); |
| 65 | } else { |
| 66 | common_options.push_back(x: &opt); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | file << "**Common params**\n\n" ; |
| 71 | write_table(file, opts&: common_options); |
| 72 | file << "\n\n**Sampling params**\n\n" ; |
| 73 | write_table(file, opts&: sparam_options); |
| 74 | file << "\n\n**Example-specific params**\n\n" ; |
| 75 | write_table(file, opts&: specific_options); |
| 76 | } |
| 77 | |
| 78 | int main(int, char **) { |
| 79 | export_md(fname: "autogen-main.md" , ex: LLAMA_EXAMPLE_MAIN); |
| 80 | export_md(fname: "autogen-server.md" , ex: LLAMA_EXAMPLE_SERVER); |
| 81 | |
| 82 | return 0; |
| 83 | } |
| 84 | |