Skip to content

Commit

Permalink
parse address (#4)
Browse files Browse the repository at this point in the history
add an address arg
  • Loading branch information
jcosentino11 authored Jun 1, 2024
1 parent 8277293 commit 51ecdbe
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
12 changes: 7 additions & 5 deletions include/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ struct Context {
std::string command;
std::string topic;
std::string message;
std::string address;
bool verbose;
};

Expand All @@ -22,14 +23,15 @@ struct ParseResult {
ParseResult parseContext(int argc, char *argv[]) noexcept;

const std::string USAGE = R"(
Usage: mqtt pub -t <topic> [-v] <message>
Usage: mqtt pub [-v] -t <topic> -a <address> <message>
Options:
-t, --topic <topic> Specify the topic to publish to
-v, --verbose Enable verbose output
-h, --help Show this help message
-t, --topic <topic> Specify the topic to publish to (required)
-a, --address <address> <host>[:<port>] 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
11 changes: 10 additions & 1 deletion src/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"};
}
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

Expand Down
18 changes: 16 additions & 2 deletions test/ContextTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 51ecdbe

Please sign in to comment.