1
0

arg.h 3.6 KB

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