quic/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
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";
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 SCP_INFO(()) << "Setting param " << key << " to value " << value;
58
59 return std::make_pair(key, value);
60 }
61
63
68 void parseCommandLineWithGetOpt(cci::cci_broker_handle a_broker, const int argc, const char* const* argv,
70 {
71 SCP_INFO(()) << "Parse command line for --gs_luafile option (" << argc << " arguments)";
72
73 bool luafile_found = false;
74 // getopt permutes argv array, so copy it to argv_cp
75 std::vector<std::unique_ptr<char[]>> argv_cp;
76 char* argv_cp_raw[argc];
77 for (int i = 0; i < argc; i++) {
78 size_t len = strlen(argv[i]) + 1; // count \0
79 argv_cp.emplace_back(std::make_unique<char[]>(len));
80 strcpy(argv_cp[i].get(), argv[i]);
81 argv_cp_raw[i] = argv_cp[i].get();
82 }
83
84 // configure getopt
85 optind = 0; // reset of getopt
86 opterr = 0; // avoid error message for not recognized option
87
88 // Don't add 'i' here. It must be specified as a long option.
89 const char* optstring = "l:p:";
90
91 struct option long_options[] = { { "gs_luafile", required_argument, 0, 'l' }, // '--luafile filename'
92 { "param", required_argument, 0, 'p' }, // --param foo.baa=10
93 { 0, 0, 0, 0 } };
94
95 while (1) {
97 if (c == EOF) break;
98
99 switch (c) {
100 case 'l': // -l and --gs_luafile
101 {
102 SCP_INFO(()) << "Option --gs_luafile with value " << optarg;
103 SCP_INFO(()) << "Lua file command line parser: parse option --gs_luafile " << optarg << std::endl;
104 lua.config(a_broker, optarg);
105 luafile_found = true;
106 break;
107 }
108
109 case 'p': // -p and --param
110 {
111 auto param = get_key_val_args(std::string(optarg));
112 a_broker.set_preset_cci_value(param.first, cci::cci_value(cci::cci_value::from_json(param.second)));
113 break;
114 }
115
116 default:
117 /* unrecognized option */
118 break;
119 }
120 }
121
123 SCP_FATAL(()) << "fatal: missing required --gs_luafile argument";
124 }
125 }
126
127private:
129};
130
131#endif
Definition argparser.h:27
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:68
Definition target.h:160
Definition luafile_tool.h:54
int config(cci::cci_broker_handle a_broker, const char *a_config_file, char *a_images_dir=NULL)
Makes the configuration.
Definition luautils.cc:73