arg.h 4.5 KB

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