| 1 | #pragma once |
| 2 | |
| 3 | #include "common.h" |
| 4 | |
| 5 | #include <set> |
| 6 | #include <string> |
| 7 | #include <vector> |
| 8 | |
| 9 | // |
| 10 | // CLI argument parsing |
| 11 | // |
| 12 | |
| 13 | struct common_arg { |
| 14 | std::set<enum llama_example> examples = {LLAMA_EXAMPLE_COMMON}; |
| 15 | std::set<enum llama_example> excludes = {}; |
| 16 | std::vector<const char *> args; |
| 17 | const char * value_hint = nullptr; // help text or example for arg value |
| 18 | const char * value_hint_2 = nullptr; // for second arg value |
| 19 | const char * env = nullptr; |
| 20 | std::string help; |
| 21 | bool is_sparam = false; // is current arg a sampling param? |
| 22 | void (*handler_void) (common_params & params) = nullptr; |
| 23 | void (*handler_string) (common_params & params, const std::string &) = nullptr; |
| 24 | void (*handler_str_str)(common_params & params, const std::string &, const std::string &) = nullptr; |
| 25 | void (*handler_int) (common_params & params, int) = nullptr; |
| 26 | |
| 27 | common_arg( |
| 28 | const std::initializer_list<const char *> & args, |
| 29 | const char * value_hint, |
| 30 | const std::string & help, |
| 31 | void (*handler)(common_params & params, const std::string &) |
| 32 | ) : args(args), value_hint(value_hint), help(help), handler_string(handler) {} |
| 33 | |
| 34 | common_arg( |
| 35 | const std::initializer_list<const char *> & args, |
| 36 | const char * value_hint, |
| 37 | const std::string & help, |
| 38 | void (*handler)(common_params & params, int) |
| 39 | ) : args(args), value_hint(value_hint), help(help), handler_int(handler) {} |
| 40 | |
| 41 | common_arg( |
| 42 | const std::initializer_list<const char *> & args, |
| 43 | const std::string & help, |
| 44 | void (*handler)(common_params & params) |
| 45 | ) : args(args), help(help), handler_void(handler) {} |
| 46 | |
| 47 | // support 2 values for arg |
| 48 | common_arg( |
| 49 | const std::initializer_list<const char *> & args, |
| 50 | const char * value_hint, |
| 51 | const char * value_hint_2, |
| 52 | const std::string & help, |
| 53 | void (*handler)(common_params & params, const std::string &, const std::string &) |
| 54 | ) : args(args), value_hint(value_hint), value_hint_2(value_hint_2), help(help), handler_str_str(handler) {} |
| 55 | |
| 56 | common_arg & set_examples(std::initializer_list<enum llama_example> examples); |
| 57 | common_arg & set_excludes(std::initializer_list<enum llama_example> excludes); |
| 58 | common_arg & set_env(const char * env); |
| 59 | common_arg & set_sparam(); |
| 60 | bool in_example(enum llama_example ex); |
| 61 | bool is_exclude(enum llama_example ex); |
| 62 | bool get_value_from_env(std::string & output) const; |
| 63 | bool has_value_from_env() const; |
| 64 | std::string to_string(); |
| 65 | }; |
| 66 | |
| 67 | struct common_params_context { |
| 68 | enum llama_example ex = LLAMA_EXAMPLE_COMMON; |
| 69 | common_params & params; |
| 70 | std::vector<common_arg> options; |
| 71 | void(*print_usage)(int, char **) = nullptr; |
| 72 | common_params_context(common_params & params) : params(params) {} |
| 73 | }; |
| 74 | |
| 75 | // parse input arguments from CLI |
| 76 | // if one argument has invalid value, it will automatically display usage of the specific argument (and not the full usage message) |
| 77 | bool common_params_parse(int argc, char ** argv, common_params & params, llama_example ex, void(*print_usage)(int, char **) = nullptr); |
| 78 | |
| 79 | // function to be used by test-arg-parser |
| 80 | common_params_context common_params_parser_init(common_params & params, llama_example ex, void(*print_usage)(int, char **) = nullptr); |
| 81 | |
| 82 | struct common_remote_params { |
| 83 | std::vector<std::string> ; |
| 84 | long timeout = 0; // CURLOPT_TIMEOUT, in seconds ; 0 means no timeout |
| 85 | long max_size = 0; // max size of the response ; unlimited if 0 ; max is 2GB |
| 86 | }; |
| 87 | // get remote file content, returns <http_code, raw_response_body> |
| 88 | std::pair<long, std::vector<char>> common_remote_get_content(const std::string & url, const common_remote_params & params); |
| 89 | |