arg.h 5.2 KB

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