qualcomm/qbox
Loading...
Searching...
No Matches
argparser.h
1/*
2 * Copyright (c) 2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef __GS_ARG_PARSER_H__
8#define __GS_ARG_PARSER_H__
9
10#include <scp/report.h>
11#include <cci_configuration>
12#include <iostream>
13#include <string>
14#include <vector>
15#include <sstream>
16#include <scp/report.h>
17#include <getopt.h>
18#include <memory>
19#include "luafile_tool.h"
20
25
26class ArgParser
27{
28 SCP_LOGGER();
29
30public:
31 ArgParser(cci::cci_broker_handle a_broker, const int argc, char* const argv[], bool enforce_config_file = false)
32 : lua()
33 {
34 SCP_DEBUG(()) << "ArgParser Constructor";
35 parseCommandLineWithGetOpt(a_broker, argc, argv, enforce_config_file);
36 }
37
38protected:
40
45 std::pair<std::string, std::string> get_key_val_args(const std::string& arg)
46 {
47 std::stringstream ss(arg);
48 std::string key, value;
49
50 if (!std::getline(ss, key, '=')) {
51 SCP_FATAL(()) << "parameter name not found!" << std::endl;
52 }
53 if (!std::getline(ss, value)) {
54 SCP_FATAL(()) << "parameter value not found!" << std::endl;
55 }
56
57 // If the value doesn't look like valid JSON, treat it as a string
58 // by wrapping it in quotes. This allows command lines like:
59 // -p key=multithread (instead of -p key=\"multithread\")
60 // which makes CTest's "Command:" log line copy-pasteable.
61 if (!value.empty() && value[0] != '"' && value[0] != '[' && value[0] != '{' && value != "true" &&
62 value != "false" && value != "null" && !std::isdigit(value[0]) && value[0] != '-') {
63 value = "\"" + value + "\"";
64 }
65
66 SCP_INFO(()) << "Setting param " << key << " to value " << value;
67
68 return std::make_pair(key, value);
69 }
70
72
77 void parseCommandLineWithGetOpt(cci::cci_broker_handle a_broker, const int argc, const char* const* argv,
78 bool enforce_config_file)
79 {
80 SCP_INFO(()) << "Parse command line for --gs_luafile option (" << argc << " arguments)";
81
82 bool luafile_found = false;
83 char* images_dir = NULL;
84 // getopt permutes argv array, so copy it to argv_cp
85 std::vector<std::unique_ptr<char[]>> argv_cp;
86 char* argv_cp_raw[argc];
87 for (int i = 0; i < argc; i++) {
88 size_t len = strlen(argv[i]) + 1; // count \0
89 argv_cp.emplace_back(std::make_unique<char[]>(len));
90 strcpy(argv_cp[i].get(), argv[i]);
91 argv_cp_raw[i] = argv_cp[i].get();
92 }
93
94 // configure getopt
95 optind = 0; // reset of getopt
96 opterr = 0; // avoid error message for not recognized option
97
98 // Don't add 'i' here. It must be specified as a long option.
99 const char* optstring = "l:p:";
100
101 struct option long_options[] = { { "gs_luafile", required_argument, 0, 'l' }, // '--luafile filename'
102 { "param", required_argument, 0, 'p' }, // --param foo.baa=10
103 { "images-dir", required_argument, 0, 'i' }, // --images-dir path
104 { 0, 0, 0, 0 } };
105
106 // parse --images-dir first to make it available to every lua config
107 while (1) {
108 int c = getopt_long(argc, argv_cp_raw, optstring, long_options, 0);
109 if (c == EOF) break;
110 if (c == 'i') images_dir = optarg;
111 }
112 optind = 0; // reset of getopt
113
114 while (1) {
115 int c = getopt_long(argc, argv_cp_raw, optstring, long_options, 0);
116 if (c == EOF) break;
117
118 switch (c) {
119 case 'l': // -l and --gs_luafile
120 {
121 SCP_INFO(()) << "Option --gs_luafile with value " << optarg;
122 SCP_INFO(()) << "Lua file command line parser: parse option --gs_luafile " << optarg << std::endl;
123 lua.config(a_broker, optarg, images_dir);
124 luafile_found = true;
125 break;
126 }
127
128 case 'p': // -p and --param
129 {
130 auto param = get_key_val_args(std::string(optarg));
131 a_broker.set_preset_cci_value(param.first, cci::cci_value(cci::cci_value::from_json(param.second)));
132 break;
133 }
134
135 default:
136 /* unrecognized option */
137 break;
138 }
139 }
140
141 if (enforce_config_file && !luafile_found) {
142 SCP_FATAL(()) << "fatal: missing required --gs_luafile argument";
143 }
144 }
145
146private:
148};
149
150#endif
std::pair< std::string, std::string > get_key_val_args(const std::string &arg)
convert "foo=bar" kind of command line arg to std::pair<std::string, std::string>
Definition argparser.h:45
void parseCommandLineWithGetOpt(cci::cci_broker_handle a_broker, const int argc, const char *const *argv, bool enforce_config_file)
Parses the command line with getopt and extracts the luafile option.
Definition argparser.h:77
Definition luafile_tool.h:54