diff --git a/common/common.cpp b/common/common.cpp index 804af1d94..0bf01ce2a 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -373,7 +373,7 @@ bool gpt_params_parse_ex(int argc, char ** argv, gpt_params & params, std::vecto } } catch (std::exception & e) { throw std::invalid_argument(format( - "error while handling environment variable \"%s\": %s\n\n", opt.env.c_str(), e.what())); + "error while handling environment variable \"%s\": %s\n\n", opt.env, e.what())); } } } @@ -395,7 +395,7 @@ bool gpt_params_parse_ex(int argc, char ** argv, gpt_params & params, std::vecto } auto opt = *arg_to_options[arg]; if (opt.has_value_from_env()) { - fprintf(stderr, "warn: %s environment variable is set, but will be overwritten by command line argument %s\n", opt.env.c_str(), arg.c_str()); + fprintf(stderr, "warn: %s environment variable is set, but will be overwritten by command line argument %s\n", opt.env, arg.c_str()); } try { if (opt.handler_void) { @@ -595,15 +595,19 @@ std::string llama_arg::to_string() { std::string leading_spaces(n_leading_spaces, ' '); std::ostringstream ss; - for (const auto & arg : args) { + for (const auto arg : args) { if (arg == args.front()) { - ss << (args.size() == 1 ? arg : format("%-7s", (arg + ",").c_str())); + if (args.size() == 1) { + ss << arg; + } else { + ss << format("%-7s", arg) << ", "; + } } else { ss << arg << (arg != args.back() ? ", " : ""); } } - if (!value_hint.empty()) ss << " " << value_hint; - if (!value_hint_2.empty()) ss << " " << value_hint_2; + if (value_hint) ss << " " << value_hint; + if (value_hint_2) ss << " " << value_hint_2; if (ss.tellp() > n_leading_spaces - 3) { // current line is too long, add new line ss << "\n" << leading_spaces; @@ -675,7 +679,7 @@ std::vector gpt_params_parser_init(gpt_params & params, llama_example if (seen_args.find(a) == seen_args.end()) { seen_args.insert(a); } else { - throw std::runtime_error(format("found duplicated argument in source code: %s", a.c_str())); + throw std::runtime_error(format("found duplicated argument in source code: %s", a)); } } options.push_back(std::move(arg)); @@ -693,7 +697,7 @@ std::vector gpt_params_parser_init(gpt_params & params, llama_example add_opt(llama_arg( {"--version"}, "show version and build info", - [](gpt_params & params) { + [](gpt_params &) { fprintf(stderr, "version: %d (%s)\n", LLAMA_BUILD_NUMBER, LLAMA_COMMIT); fprintf(stderr, "built with %s for %s\n", LLAMA_COMPILER, LLAMA_BUILD_TARGET); exit(0); @@ -2248,32 +2252,32 @@ std::vector gpt_params_parser_init(gpt_params & params, llama_example add_opt(llama_arg( {"--log-test"}, "Log test", - [](gpt_params & params) { log_param_single_parse("--log-test"); } + [](gpt_params &) { log_param_single_parse("--log-test"); } )); add_opt(llama_arg( {"--log-disable"}, "Log disable", - [](gpt_params & params) { log_param_single_parse("--log-disable"); } + [](gpt_params &) { log_param_single_parse("--log-disable"); } )); add_opt(llama_arg( {"--log-enable"}, "Log enable", - [](gpt_params & params) { log_param_single_parse("--log-enable"); } + [](gpt_params &) { log_param_single_parse("--log-enable"); } )); add_opt(llama_arg( {"--log-new"}, "Log new", - [](gpt_params & params) { log_param_single_parse("--log-new"); } + [](gpt_params &) { log_param_single_parse("--log-new"); } )); add_opt(llama_arg( {"--log-append"}, "Log append", - [](gpt_params & params) { log_param_single_parse("--log-append"); } + [](gpt_params &) { log_param_single_parse("--log-append"); } )); add_opt(llama_arg( {"--log-file"}, "FNAME", "Log file", - [](gpt_params & params, const std::string & value) { log_param_pair_parse(false, "--log-file", value); } + [](gpt_params &, const std::string & value) { log_param_pair_parse(false, "--log-file", value); } )); #endif // LOG_DISABLE_LOGS diff --git a/common/common.h b/common/common.h index 1f709271d..b79149da0 100644 --- a/common/common.h +++ b/common/common.h @@ -305,10 +305,10 @@ struct gpt_params { struct llama_arg { std::set examples = {LLAMA_EXAMPLE_COMMON}; - std::vector args; - std::string value_hint; // help text or example for arg value - std::string value_hint_2; // for second arg value - std::string env; + std::vector 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; void (*handler_void) (gpt_params & params) = nullptr; void (*handler_string) (gpt_params & params, const std::string &) = nullptr; @@ -316,42 +316,42 @@ struct llama_arg { void (*handler_int) (gpt_params & params, int) = nullptr; llama_arg( - const std::initializer_list & args, - const std::string & value_hint, + const std::initializer_list & args, + const char * value_hint, const std::string & help, void (*handler)(gpt_params & params, const std::string &) ) : args(args), value_hint(value_hint), help(help), handler_string(handler) {} llama_arg( - const std::initializer_list & args, - const std::string & value_hint, + const std::initializer_list & args, + const char * value_hint, const std::string & help, void (*handler)(gpt_params & params, int) ) : args(args), value_hint(value_hint), help(help), handler_int(handler) {} llama_arg( - const std::initializer_list & args, + const std::initializer_list & args, const std::string & help, void (*handler)(gpt_params & params) ) : args(args), help(help), handler_void(handler) {} // support 2 values for arg llama_arg( - const std::initializer_list & args, - const std::string & value_hint, - const std::string & value_hint_2, + const std::initializer_list & args, + const char * value_hint, + const char * value_hint_2, const std::string & help, void (*handler)(gpt_params & params, const std::string &, const std::string &) ) : args(args), value_hint(value_hint), value_hint_2(value_hint_2), help(help), handler_str_str(handler) {} - llama_arg & set_examples(std::set examples) { + llama_arg & set_examples(std::initializer_list examples) { this->examples = std::move(examples); return *this; } - llama_arg & set_env(std::string env) { + llama_arg & set_env(const char * env) { help = help + "\n(env: " + env + ")"; - this->env = std::move(env); + this->env = env; return *this; } @@ -360,8 +360,8 @@ struct llama_arg { } bool get_value_from_env(std::string & output) const { - if (env.empty()) return false; - char * value = std::getenv(env.c_str()); + if (env == nullptr) return false; + char * value = std::getenv(env); if (value) { output = value; return true; @@ -370,7 +370,7 @@ struct llama_arg { } bool has_value_from_env() const { - return std::getenv(env.c_str()); + return env != nullptr && std::getenv(env); } std::string to_string(); diff --git a/examples/export-docs/export-docs.cpp b/examples/export-docs/export-docs.cpp index 86c041a81..a09036dcf 100644 --- a/examples/export-docs/export-docs.cpp +++ b/examples/export-docs/export-docs.cpp @@ -22,18 +22,19 @@ static void export_md(std::string fname, llama_example ex) { // args for (const auto & arg : opt.args) { if (arg == opt.args.front()) { - file << (opt.args.size() == 1 ? arg : (arg + ", ")); + file << arg; + if (opt.args.size() > 1) file << ", "; } else { file << arg << (arg != opt.args.back() ? ", " : ""); } } // value hint - if (!opt.value_hint.empty()) { + if (opt.value_hint) { std::string md_value_hint(opt.value_hint); string_replace_all(md_value_hint, "|", "\\|"); file << " " << md_value_hint; } - if (!opt.value_hint_2.empty()) { + if (opt.value_hint_2) { std::string md_value_hint_2(opt.value_hint_2); string_replace_all(md_value_hint_2, "|", "\\|"); file << " " << md_value_hint_2;