Skip to content

Commit

Permalink
コマンドラインオプションのバリデーションを強化
Browse files Browse the repository at this point in the history
  • Loading branch information
idofront committed Nov 29, 2024
1 parent 66fae28 commit dd86e91
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
19 changes: 7 additions & 12 deletions include/ParseOptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <algorithm>
#include <filesystem>
#include <iostream>
#include <spdlog/spdlog.h>
#include <spdlog/common.h>

enum Mode
{
Expand All @@ -25,7 +25,11 @@ class ParseOptions
// General options
app.add_option("--pcap,-p", _PcapFilePath, "Path to the pcap file")->required();
app.add_option("--interface,-i", _InterfaceName, "Network interface to send packets")->required();
app.add_option("--log-level", _LogLevelString, "Log level (trace, debug, info, warn, error, critical)");

app.add_option("--log-level", _LogLevelString, "Log level")
->check(CLI::IsMember({"trace", "debug", "info", "warn", "error", "critical", "off"}))
->default_val("info");

app.add_option("--report-interval", _ReportIntervalSec, "Interval to show reports in seconds")
->default_val(1.0);
app.add_option("--repeat", _RepeatCount, "Number of times to repeat the traffic. 0 means infinite repeat")
Expand Down Expand Up @@ -53,6 +57,7 @@ class ParseOptions
catch (const CLI::ParseError &e)
{
app.exit(e);
throw std::runtime_error(e.what());
}

// If help flag was specified, display help
Expand Down Expand Up @@ -165,41 +170,31 @@ class ParseOptions
{
throw std::runtime_error("Repeat count must be greater than or equal to 0");
}

spdlog::info("Interface: {}", _InterfaceName);
spdlog::info("Pcap file: {}", _PcapFilePath.string());
spdlog::info("Log level: {}", spdlog::level::to_string_view(LogLevel()));
spdlog::info("Report interval: {} seconds", _ReportIntervalSec);
spdlog::info("Repeat count: {}", _RepeatCount);
}

void HandleThroughputSubcommand()
{
spdlog::info("Throughput: {} Mbps", _ThroughputMbps);
if (_ThroughputMbps <= 0)
{
throw std::runtime_error("Throughput must be greater than 0");
}
}
void HandleSpeesdScaleSubcommand()
{
spdlog::info("SpeedScale: factor {}", _SpeedScaleFactor);
if (_SpeedScaleFactor <= 0)
{
throw std::runtime_error("Speed scale factor must be greater than 0");
}
}
void HandleDurationSubcommand()
{
spdlog::info("Duration: {} seconds", _DurationTime);
if (_DurationTime <= 0)
{
throw std::runtime_error("Duration time must be greater than 0");
}
}
void HandlePacketsPerSecondSubcommand()
{
spdlog::info("PacketsPerSecond: {} packets per second", _PacketsPerSecond);
if (_PacketsPerSecond <= 0)
{
throw std::runtime_error("Packets per second must be greater than 0");
Expand Down
4 changes: 2 additions & 2 deletions main/TrafficPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ int main(int argc, char *argv[])
}
if (repeatCount > 1)
{
spdlog::info("Cycle: {}/{}", repeat, repeatCount);
spdlog::debug("Cycle: {}/{}", repeat, repeatCount);
}
}
else
{
spdlog::info("Cycle: {}", repeat);
spdlog::debug("Cycle: {}", repeat);
}

std::for_each(trafficRecords.begin(), trafficRecords.end(),
Expand Down

0 comments on commit dd86e91

Please sign in to comment.