Skip to content

Commit 29bc4a5

Browse files
feat(arg): Add --config flag with YAML pre-scan and precedence logic
- Add --config flag to argument parser with pre-scan before flag parsing - Implement YAML loading before common_params_parse_ex to ensure proper precedence - Flags override YAML values, YAML overrides defaults - Add --config option to usage help and argument definitions Co-Authored-By: Jaime Mizrachi <[email protected]>
1 parent 5e0f94f commit 29bc4a5

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

common/arg.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "chat.h"
44
#include "common.h"
5+
#include "config.h"
56
#include "gguf.h" // for reading GGUF splits
67
#include "json-schema-to-grammar.h"
78
#include "log.h"
@@ -1223,6 +1224,19 @@ bool common_params_parse(int argc, char ** argv, common_params & params, llama_e
12231224
const common_params params_org = ctx_arg.params; // the example can modify the default params
12241225

12251226
try {
1227+
for (int i = 1; i < argc; ++i) {
1228+
if (std::string(argv[i]) == "--config") {
1229+
if (i + 1 >= argc) {
1230+
throw std::invalid_argument("error: --config requires a file path");
1231+
}
1232+
std::string cfg_path = argv[++i];
1233+
if (!common_load_yaml_config(cfg_path, ctx_arg.params)) {
1234+
throw std::invalid_argument("error: failed to load YAML config: " + cfg_path);
1235+
}
1236+
break; // single --config supported; first one wins
1237+
}
1238+
}
1239+
12261240
if (!common_params_parse_ex(argc, argv, ctx_arg)) {
12271241
ctx_arg.params = params_org;
12281242
return false;
@@ -1317,6 +1331,14 @@ common_params_context common_params_parser_init(common_params & params, llama_ex
13171331
params.completion = true;
13181332
}
13191333
));
1334+
1335+
add_opt(common_arg(
1336+
{"--config"},
1337+
"<path/to/config.yaml>",
1338+
"Load parameters from a YAML config file; flags passed on the command line override values from the YAML file.",
1339+
[](common_params &, const std::string &) {
1340+
}
1341+
));
13201342
add_opt(common_arg(
13211343
{"--verbose-prompt"},
13221344
string_format("print a verbose prompt before generation (default: %s)", params.verbose_prompt ? "true" : "false"),

0 commit comments

Comments
 (0)