2024-09-09 21:36:09 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
//
|
|
|
|
// CLI argument parsing
|
|
|
|
//
|
|
|
|
|
2024-10-10 20:57:42 +00:00
|
|
|
struct common_arg {
|
2024-09-09 21:36:09 +00:00
|
|
|
std::set<enum llama_example> examples = {LLAMA_EXAMPLE_COMMON};
|
|
|
|
std::vector<const char *> args;
|
|
|
|
const char * value_hint = nullptr; // help text or example for arg value
|
|
|
|
const char * value_hint_2 = nullptr; // for second arg value
|
|
|
|
const char * env = nullptr;
|
|
|
|
std::string help;
|
|
|
|
bool is_sparam = false; // is current arg a sampling param?
|
2024-10-10 20:57:42 +00:00
|
|
|
void (*handler_void) (common_params & params) = nullptr;
|
|
|
|
void (*handler_string) (common_params & params, const std::string &) = nullptr;
|
|
|
|
void (*handler_str_str)(common_params & params, const std::string &, const std::string &) = nullptr;
|
|
|
|
void (*handler_int) (common_params & params, int) = nullptr;
|
2024-09-09 21:36:09 +00:00
|
|
|
|
2024-10-10 20:57:42 +00:00
|
|
|
common_arg(
|
2024-09-09 21:36:09 +00:00
|
|
|
const std::initializer_list<const char *> & args,
|
|
|
|
const char * value_hint,
|
|
|
|
const std::string & help,
|
2024-10-10 20:57:42 +00:00
|
|
|
void (*handler)(common_params & params, const std::string &)
|
2024-09-09 21:36:09 +00:00
|
|
|
) : args(args), value_hint(value_hint), help(help), handler_string(handler) {}
|
|
|
|
|
2024-10-10 20:57:42 +00:00
|
|
|
common_arg(
|
2024-09-09 21:36:09 +00:00
|
|
|
const std::initializer_list<const char *> & args,
|
|
|
|
const char * value_hint,
|
|
|
|
const std::string & help,
|
2024-10-10 20:57:42 +00:00
|
|
|
void (*handler)(common_params & params, int)
|
2024-09-09 21:36:09 +00:00
|
|
|
) : args(args), value_hint(value_hint), help(help), handler_int(handler) {}
|
|
|
|
|
2024-10-10 20:57:42 +00:00
|
|
|
common_arg(
|
2024-09-09 21:36:09 +00:00
|
|
|
const std::initializer_list<const char *> & args,
|
|
|
|
const std::string & help,
|
2024-10-10 20:57:42 +00:00
|
|
|
void (*handler)(common_params & params)
|
2024-09-09 21:36:09 +00:00
|
|
|
) : args(args), help(help), handler_void(handler) {}
|
|
|
|
|
|
|
|
// support 2 values for arg
|
2024-10-10 20:57:42 +00:00
|
|
|
common_arg(
|
2024-09-09 21:36:09 +00:00
|
|
|
const std::initializer_list<const char *> & args,
|
|
|
|
const char * value_hint,
|
|
|
|
const char * value_hint_2,
|
|
|
|
const std::string & help,
|
2024-10-10 20:57:42 +00:00
|
|
|
void (*handler)(common_params & params, const std::string &, const std::string &)
|
2024-09-09 21:36:09 +00:00
|
|
|
) : args(args), value_hint(value_hint), value_hint_2(value_hint_2), help(help), handler_str_str(handler) {}
|
|
|
|
|
2024-10-10 20:57:42 +00:00
|
|
|
common_arg & set_examples(std::initializer_list<enum llama_example> examples);
|
|
|
|
common_arg & set_env(const char * env);
|
|
|
|
common_arg & set_sparam();
|
2024-09-09 21:36:09 +00:00
|
|
|
bool in_example(enum llama_example ex);
|
|
|
|
bool get_value_from_env(std::string & output);
|
|
|
|
bool has_value_from_env();
|
|
|
|
std::string to_string();
|
|
|
|
};
|
|
|
|
|
2024-10-10 20:57:42 +00:00
|
|
|
struct common_params_context {
|
2024-09-09 21:36:09 +00:00
|
|
|
enum llama_example ex = LLAMA_EXAMPLE_COMMON;
|
2024-10-10 20:57:42 +00:00
|
|
|
common_params & params;
|
|
|
|
std::vector<common_arg> options;
|
2024-09-09 21:36:09 +00:00
|
|
|
void(*print_usage)(int, char **) = nullptr;
|
2024-10-10 20:57:42 +00:00
|
|
|
common_params_context(common_params & params) : params(params) {}
|
2024-09-09 21:36:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// parse input arguments from CLI
|
|
|
|
// if one argument has invalid value, it will automatically display usage of the specific argument (and not the full usage message)
|
2024-10-10 20:57:42 +00:00
|
|
|
bool common_params_parse(int argc, char ** argv, common_params & params, llama_example ex, void(*print_usage)(int, char **) = nullptr);
|
2024-09-09 21:36:09 +00:00
|
|
|
|
|
|
|
// function to be used by test-arg-parser
|
2024-10-10 20:57:42 +00:00
|
|
|
common_params_context common_params_parser_init(common_params & params, llama_example ex, void(*print_usage)(int, char **) = nullptr);
|