1
0

arg.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #pragma once
  2. #include "common.h"
  3. #include <set>
  4. #include <map>
  5. #include <string>
  6. #include <vector>
  7. #include <cstring>
  8. //
  9. // CLI argument parsing
  10. //
  11. struct common_arg {
  12. std::set<enum llama_example> examples = {LLAMA_EXAMPLE_COMMON};
  13. std::set<enum llama_example> excludes = {};
  14. std::vector<const char *> args;
  15. std::vector<const char *> args_neg; // for negated args like --no-xxx
  16. const char * value_hint = nullptr; // help text or example for arg value
  17. const char * value_hint_2 = nullptr; // for second arg value
  18. const char * env = nullptr;
  19. std::string help;
  20. bool is_sparam = false; // is current arg a sampling param?
  21. void (*handler_void) (common_params & params) = nullptr;
  22. void (*handler_string) (common_params & params, const std::string &) = nullptr;
  23. void (*handler_str_str)(common_params & params, const std::string &, const std::string &) = nullptr;
  24. void (*handler_int) (common_params & params, int) = nullptr;
  25. void (*handler_bool) (common_params & params, bool) = nullptr;
  26. common_arg() = default;
  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. common_arg(
  34. const std::initializer_list<const char *> & args,
  35. const char * value_hint,
  36. const std::string & help,
  37. void (*handler)(common_params & params, int)
  38. ) : args(args), value_hint(value_hint), help(help), handler_int(handler) {}
  39. common_arg(
  40. const std::initializer_list<const char *> & args,
  41. const std::string & help,
  42. void (*handler)(common_params & params)
  43. ) : args(args), help(help), handler_void(handler) {}
  44. common_arg(
  45. const std::initializer_list<const char *> & args,
  46. const std::initializer_list<const char *> & args_neg,
  47. const std::string & help,
  48. void (*handler)(common_params & params, bool)
  49. ) : args(args), args_neg(args_neg), help(help), handler_bool(handler) {}
  50. // support 2 values for arg
  51. common_arg(
  52. const std::initializer_list<const char *> & args,
  53. const char * value_hint,
  54. const char * value_hint_2,
  55. const std::string & help,
  56. void (*handler)(common_params & params, const std::string &, const std::string &)
  57. ) : args(args), value_hint(value_hint), value_hint_2(value_hint_2), help(help), handler_str_str(handler) {}
  58. common_arg & set_examples(std::initializer_list<enum llama_example> examples);
  59. common_arg & set_excludes(std::initializer_list<enum llama_example> excludes);
  60. common_arg & set_env(const char * env);
  61. common_arg & set_sparam();
  62. bool in_example(enum llama_example ex);
  63. bool is_exclude(enum llama_example ex);
  64. bool get_value_from_env(std::string & output) const;
  65. bool has_value_from_env() const;
  66. std::string to_string() const;
  67. // for using as key in std::map
  68. bool operator<(const common_arg& other) const {
  69. if (args.empty() || other.args.empty()) {
  70. return false;
  71. }
  72. return strcmp(args[0], other.args[0]) < 0;
  73. }
  74. bool operator==(const common_arg& other) const {
  75. if (args.empty() || other.args.empty()) {
  76. return false;
  77. }
  78. return strcmp(args[0], other.args[0]) == 0;
  79. }
  80. // get all args and env vars (including negated args/env)
  81. std::vector<std::string> get_args() const;
  82. std::vector<std::string> get_env() const;
  83. };
  84. namespace common_arg_utils {
  85. bool is_truthy(const std::string & value);
  86. bool is_falsey(const std::string & value);
  87. bool is_autoy(const std::string & value);
  88. }
  89. struct common_params_context {
  90. enum llama_example ex = LLAMA_EXAMPLE_COMMON;
  91. common_params & params;
  92. std::vector<common_arg> options;
  93. void(*print_usage)(int, char **) = nullptr;
  94. common_params_context(common_params & params) : params(params) {}
  95. };
  96. // parse input arguments from CLI
  97. // if one argument has invalid value, it will automatically display usage of the specific argument (and not the full usage message)
  98. bool common_params_parse(int argc, char ** argv, common_params & params, llama_example ex, void(*print_usage)(int, char **) = nullptr);
  99. // parse input arguments from CLI into a map
  100. // TODO: support repeated args in the future
  101. bool common_params_to_map(int argc, char ** argv, llama_example ex, std::map<common_arg, std::string> & out_map);
  102. // initialize argument parser context - used by test-arg-parser and preset
  103. common_params_context common_params_parser_init(common_params & params, llama_example ex, void(*print_usage)(int, char **) = nullptr);
  104. struct common_remote_params {
  105. std::vector<std::string> headers;
  106. long timeout = 0; // CURLOPT_TIMEOUT, in seconds ; 0 means no timeout
  107. long max_size = 0; // max size of the response ; unlimited if 0 ; max is 2GB
  108. };
  109. // get remote file content, returns <http_code, raw_response_body>
  110. std::pair<long, std::vector<char>> common_remote_get_content(const std::string & url, const common_remote_params & params);