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