Skip to content

Commit dc12592

Browse files
committed
fdsdump: use new argparser component instead of getopts
1 parent f8e7825 commit dc12592

File tree

1 file changed

+64
-54
lines changed

1 file changed

+64
-54
lines changed

src/tools/fdsdump/src/options.cpp

Lines changed: 64 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <getopt.h>
1414
#include <unistd.h>
1515

16+
#include <common/common.hpp>
17+
#include <common/argParser.hpp>
1618
#include <options.hpp>
1719

1820
namespace fdsdump {
@@ -57,60 +59,67 @@ void Options::reset()
5759
*/
5860
void Options::parse(int argc, char *argv[])
5961
{
60-
enum long_opts_vals {
61-
OPT_BIFLOW_AUTOIGNORE_OFF = 256, // Value that cannot colide with chars
62-
};
63-
const struct option long_opts[] = {
64-
{"filter", required_argument, NULL, 'F'},
65-
{"output", required_argument, NULL, 'o'},
66-
{"order", required_argument, NULL, 'O'},
67-
{"limit", required_argument, NULL, 'c'},
68-
{"no-biflow-autoignore", no_argument, NULL, OPT_BIFLOW_AUTOIGNORE_OFF},
69-
{0, 0, 0, 0},
70-
};
71-
const char *short_opts = "r:c:o:O:F:A:S:I";
72-
int opt;
73-
74-
while ((opt = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
75-
switch (opt) {
76-
case 'r':
77-
m_input_files.add_files(optarg);
78-
break;
79-
case 'c':
80-
m_output_limit = std::stoull(optarg);
81-
break;
82-
case 'o':
83-
m_output_specifier = optarg;
84-
break;
85-
case 'O':
86-
m_order_by = optarg;
87-
break;
88-
case 'F':
89-
m_input_filter = optarg;
90-
break;
91-
case 'A':
92-
m_aggregation_keys = optarg;
93-
break;
94-
case 'I':
95-
m_mode = Mode::stats;
96-
break;
97-
case 'S':
98-
m_aggregation_values = optarg;
99-
break;
100-
case OPT_BIFLOW_AUTOIGNORE_OFF:
101-
m_biflow_autoignore = false;
102-
break;
103-
case '?':
104-
throw OptionsException("invalid command line option(s)");
105-
default:
106-
throw OptionsException("getopts_long() returned unexpected value " + std::to_string(opt));
107-
}
108-
}
109-
110-
if (optind < argc) {
111-
const char *arg = argv[optind];
112-
throw OptionsException("unknown argument '" + std::string(arg) + "'");
113-
}
62+
ArgParser parser;
63+
parser.add('r', "input", true);
64+
parser.add('F', "filter", true);
65+
parser.add('o', "output", true);
66+
parser.add('O', "order", true);
67+
parser.add('c', "limit", true);
68+
parser.add('A', "aggregation-keys", true);
69+
parser.add('S', "aggregation-values", true);
70+
parser.add("no-biflow-autoignore", false);
71+
parser.add('I', "stats-mode", false);
72+
73+
Args args;
74+
try {
75+
args = parser.parse(argc, argv);
76+
} catch (const ArgParser::MissingArgument& missing) {
77+
throw OptionsException("Missing argument for " + missing.arg);
78+
} catch (const ArgParser::UnknownArgument& unknown) {
79+
throw OptionsException("Unknown argument " + unknown.arg);
80+
}
81+
82+
if (args.has('r')) {
83+
for (const auto &arg : args.get_all('r')) {
84+
m_input_files.add_files(arg);
85+
}
86+
}
87+
88+
if (args.has('c')) {
89+
auto maybe_value = parse_number<unsigned int>(args.get('c'));
90+
if (!maybe_value) {
91+
throw OptionsException("invalid -c/--limit value - not a number");
92+
}
93+
m_output_limit = *maybe_value;
94+
}
95+
96+
if (args.has('o')) {
97+
m_output_specifier = args.get('o');
98+
}
99+
100+
if (args.has('O')) {
101+
m_order_by = args.get('O');
102+
}
103+
104+
if (args.has('F')) {
105+
m_input_filter = args.get('F');
106+
}
107+
108+
if (args.has('A')) {
109+
m_aggregation_keys = args.get('A');
110+
}
111+
112+
if (args.has('S')) {
113+
m_aggregation_values = args.get('S');
114+
}
115+
116+
if (args.has("no-biflow-autoignore")) {
117+
m_biflow_autoignore = false;
118+
}
119+
120+
if (args.has('I')) {
121+
m_mode = Mode::stats;
122+
}
114123
}
115124

116125
void Options::validate()
@@ -137,6 +146,7 @@ void Options::validate()
137146
m_output_specifier = "JSON-RAW";
138147
}
139148
}
149+
140150
}
141151

142152
} // fdsdump

0 commit comments

Comments
 (0)