Skip to content

Commit 8952aee

Browse files
Lukas HutakLukas955
authored andcommitted
fdsdump: options: define component
1 parent 1e9ee27 commit 8952aee

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed

src/tools/fdsdump/src/options.cpp

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
2+
#include <getopt.h>
3+
#include <unistd.h>
4+
5+
#include "options.hpp"
6+
7+
Options::Options()
8+
{
9+
reset();
10+
}
11+
12+
Options::Options(int argc, char *argv[])
13+
{
14+
reset();
15+
parse(argc, argv);
16+
validate();
17+
}
18+
19+
void Options::reset()
20+
{
21+
m_mode = Mode::undefined;
22+
23+
m_input_files.clear();
24+
m_input_filter.clear();
25+
26+
m_output_limit = 0;
27+
m_output_specifier = "JSON-RAW";
28+
29+
m_biflow_autoignore = true;
30+
31+
m_order_by.clear();
32+
}
33+
34+
/**
35+
* @brief Parse command line arguments.
36+
*
37+
* Previously specified values are not reset. Previous values might be
38+
* redefined or extended (e.g. files to process).
39+
* @param[in] argc Number of arguments
40+
* @param[in] argv Array of arguments
41+
*/
42+
void Options::parse(int argc, char *argv[])
43+
{
44+
enum long_opts_vals {
45+
OPT_BIFLOW_AUTOIGNORE_OFF = 256, // Value that cannot colide with chars
46+
};
47+
const struct option long_opts[] = {
48+
{"filter", required_argument, NULL, 'F'},
49+
{"output", required_argument, NULL, 'o'},
50+
{"order", required_argument, NULL, 'O'},
51+
{"limit", required_argument, NULL, 'c'},
52+
{"no-biflow-autoignore", no_argument, NULL, OPT_BIFLOW_AUTOIGNORE_OFF},
53+
{0, 0, 0, 0},
54+
};
55+
const char *short_opts = "r:c:o:O:F:";
56+
int opt;
57+
58+
while ((opt = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
59+
switch (opt) {
60+
case 'r':
61+
m_input_files.add_files(optarg);
62+
break;
63+
case 'c':
64+
m_output_limit = std::stoull(optarg);
65+
break;
66+
case 'o':
67+
m_output_specifier = optarg;
68+
break;
69+
case 'O':
70+
m_order_by = optarg;
71+
break;
72+
case 'F':
73+
m_input_filter = optarg;
74+
break;
75+
case OPT_BIFLOW_AUTOIGNORE_OFF:
76+
m_biflow_autoignore = false;
77+
break;
78+
case '?':
79+
throw OptionsException("invalid command line option(s)");
80+
default:
81+
throw OptionsException("getopts_long() returned unexpected value " + std::to_string(opt));
82+
}
83+
}
84+
85+
if (optind < argc) {
86+
const char *arg = argv[optind];
87+
throw OptionsException("unknown argument '" + std::string(arg) + "'");
88+
}
89+
}
90+
91+
void Options::validate()
92+
{
93+
if (m_mode == Mode::undefined) {
94+
m_mode = Mode::list;
95+
}
96+
}

src/tools/fdsdump/src/options.hpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
2+
#include <cstddef>
3+
#include <string>
4+
#include <stdexcept>
5+
6+
#include "filelist.hpp"
7+
8+
class OptionsException : public std::invalid_argument {
9+
public:
10+
OptionsException(const std::string &what_arg)
11+
: std::invalid_argument(what_arg)
12+
{};
13+
14+
OptionsException(const char *what_arg)
15+
: std::invalid_argument(what_arg)
16+
{};
17+
};
18+
19+
/**
20+
* @brief Command line arguments.
21+
*
22+
* Parse and validate user-specified command line arguments.
23+
*/
24+
class Options {
25+
public:
26+
enum class Mode {
27+
undefined,
28+
list,
29+
aggregate,
30+
};
31+
32+
/**
33+
* @brief Create options with default values.
34+
*/
35+
Options();
36+
/**
37+
* @brief Create options and parse command line arguments.
38+
* @param[in] argc Number of arguments
39+
* @param[in] argv Array of arguments
40+
*/
41+
Options(int argc, char *argv[]);
42+
~Options() = default;
43+
44+
/**
45+
* @brief Reset all values to default.
46+
*/
47+
void reset();
48+
49+
const Mode &get_mode() const { return m_mode; };
50+
51+
/** @brief Get list of files to process. */
52+
const FileList &get_input_files() const { return m_input_files; };
53+
/** @brief Get input flow filter. */
54+
const std::string &get_input_filter() const { return m_input_filter; };
55+
56+
/** @brief Get number of records to print on output. */
57+
size_t get_output_limit() const { return m_output_limit; };
58+
/** @brief Get output format specifier. */
59+
const std::string &get_output_specifier() const { return m_output_specifier; };
60+
61+
/** @brief Whether to ignore biflow direction with zero bytes and packets counter */
62+
bool get_biflow_autoignore() const { return m_biflow_autoignore; };
63+
64+
/** @brief Get output order specificiation */
65+
const std::string &get_order_by() const { return m_order_by; };
66+
67+
private:
68+
Mode m_mode;
69+
70+
FileList m_input_files;
71+
std::string m_input_filter;
72+
73+
size_t m_output_limit;
74+
std::string m_output_specifier;
75+
76+
std::string m_order_by;
77+
78+
bool m_biflow_autoignore;
79+
80+
void parse(int argc, char *argv[]);
81+
void validate();
82+
};

0 commit comments

Comments
 (0)