diff --git a/include/Context.h b/include/Context.h index 2cfd0aa..a284e01 100644 --- a/include/Context.h +++ b/include/Context.h @@ -8,6 +8,7 @@ struct Context { std::string command; std::string topic; std::string message; + std::string address; bool verbose; }; @@ -22,14 +23,15 @@ struct ParseResult { ParseResult parseContext(int argc, char *argv[]) noexcept; const std::string USAGE = R"( -Usage: mqtt pub -t [-v] +Usage: mqtt pub [-v] -t -a
Options: - -t, --topic Specify the topic to publish to - -v, --verbose Enable verbose output - -h, --help Show this help message + -t, --topic Specify the topic to publish to (required) + -a, --address
[:] address to connect to (required) + -v, --verbose Enable verbose output + -h, --help Show this help message Example: - mqtt pub -t hello/topic -v '{"hello": "world"}' + mqtt pub -t hello/topic -v -a 127.0.0.1:8883 '{"hello": "world"}' )"; }; // namespace MqttClient \ No newline at end of file diff --git a/src/Context.cpp b/src/Context.cpp index 2b648bb..a7d8b45 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -23,18 +23,22 @@ ParseResult parseContext(int argc, char *argv[]) noexcept { argv += 1; struct option long_options[] = {{"topic", required_argument, 0, 't'}, + {"address", required_argument, 0, 'a'}, {"verbose", no_argument, 0, 'v'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0}}; int option_index = 0; int option; - while ((option = getopt_long(argc, argv, "t:vh", long_options, + while ((option = getopt_long(argc, argv, "t:a:vh", long_options, &option_index)) != -1) { switch (option) { case 't': context.topic = optarg; break; + case 'a': + context.address = optarg; + break; case 'v': context.verbose = true; break; @@ -52,6 +56,11 @@ ParseResult parseContext(int argc, char *argv[]) noexcept { return {ParseResultCode::FAILURE, "Error: Topic is required"}; } + if (context.address.empty()) { + // TODO more validations + return {ParseResultCode::FAILURE, "Error: Address is required"}; + } + if (optind == argc) { return {ParseResultCode::FAILURE, "Error: Message is required"}; } diff --git a/src/main.cpp b/src/main.cpp index 29f6399..e5296f1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,6 +25,7 @@ int main(int argc, char *argv[]) { if (context.verbose) { std::cout << "Context[command=" << context.command << ", topic=" << context.topic + << ", address=" << context.address << ", message=" << context.message << "]\n"; } diff --git a/test/ContextTest.cpp b/test/ContextTest.cpp index 1c8837e..c25a97d 100644 --- a/test/ContextTest.cpp +++ b/test/ContextTest.cpp @@ -4,8 +4,22 @@ using namespace MqttClient; -TEST(ContextTest, NoArgs) { - auto res = parseContext(0, {}); +class ContextTest : public testing::Test { + public: + ParseResult parse(char *args[]) { + int argc = sizeof(args) / sizeof(args[0]); + return parseContext(argc, args); + } +}; + +TEST_F(ContextTest, NoArgs) { + auto res = parse({}); + EXPECT_EQ(ParseResultCode::FAILURE, res.code); +} + +TEST_F(ContextTest, WithAllRequiredOptions) { + char *args[] = {"mqtt", "pub", "-t", "topic", "-a", "127.0.0.1:8883"}; + auto res = parse(args); EXPECT_EQ(ParseResultCode::FAILURE, res.code); }