Blame view

tools/publish_cli/argumentparser.cpp 4.59 KB
890ef5ed   Peter M. Groen   Added argumentparser
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  /* ****************************************************************************
   * Copyright 2024 Open Systems Development BV                                 *
   *                                                                            *
   * Permission is hereby granted, free of charge, to any person obtaining a    *
   * copy of this software and associated documentation files (the "Software"), *
   * to deal in the Software without restriction, including without limitation  *
   * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
   * and/or sell copies of the Software, and to permit persons to whom the      *
   * Software is furnished to do so, subject to the following conditions:       *
   *                                                                            *
   * The above copyright notice and this permission notice shall be included in *
   * all copies or substantial portions of the Software.                        *
   *                                                                            *
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
   * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    *
   * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        *
   * DEALINGS IN THE SOFTWARE.                                                  *
   * ***************************************************************************/
  #include "argumentparser.h"
  
  #include <iostream>
  
bf715122   Peter M. Groen   Added CLI Tools
26
27
28
29
30
31
32
33
34
  ArgumentParser::ArgumentParser(int num_of_args, char* argv[])
      : ArgumentParserBase(num_of_args, argv)
  {
      if (count() == 0)
      {
          printUsage();
      }
  }
  
890ef5ed   Peter M. Groen   Added argumentparser
35
36
  std::string ArgumentParser::getHost()
  {
bf715122   Peter M. Groen   Added CLI Tools
37
38
39
40
41
42
43
44
45
46
47
48
      // -h | --host
      std::string host = getArgument("-h");
      if (host.empty())
      {
          host = getArgument("--host");
          if(host.empty())
          {
              host = "127.0.0.1";
          }
      }
  
      return host;
890ef5ed   Peter M. Groen   Added argumentparser
49
50
51
52
  }
  
  std::string ArgumentParser::getPort()
  {
bf715122   Peter M. Groen   Added CLI Tools
53
54
55
56
57
58
59
60
61
62
63
64
      // -p | --port
      std::string port = getArgument("-p");
      if (port.empty())
      {
          port = getArgument("--port");
          if(port.empty())
          {
              port = "1883";
          }
      }
  
      return port;
890ef5ed   Peter M. Groen   Added argumentparser
65
66
67
68
  }
  
  std::string ArgumentParser::getUserName()
  {
bf715122   Peter M. Groen   Added CLI Tools
69
70
71
72
73
74
75
76
      // -u | --username
      std::string username = getArgument("-u");
      if (username.empty())
      {
          username = getArgument("--username");
      }
  
      return username;
890ef5ed   Peter M. Groen   Added argumentparser
77
78
79
80
  }
  
  std::string ArgumentParser::getPassword()
  {
bf715122   Peter M. Groen   Added CLI Tools
81
82
83
84
85
86
87
88
      // -pw | --password
      std::string password = getArgument("-pw");
      if (password.empty())
      {
          password = getArgument("--password");
      }
  
      return password;
890ef5ed   Peter M. Groen   Added argumentparser
89
90
91
92
  }
  
  std::string ArgumentParser::getTopic()
  {
bf715122   Peter M. Groen   Added CLI Tools
93
94
95
96
97
98
99
100
      // -t | --topic
      std::string topic = getArgument("-t");
      if (topic.empty())
      {
          topic = getArgument("--topic");
      }
  
      return topic;
890ef5ed   Peter M. Groen   Added argumentparser
101
102
103
104
  }
  
  std::string ArgumentParser::getFile()
  {
bf715122   Peter M. Groen   Added CLI Tools
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
      // -f | --file
      std::string file = getArgument("-f");
      if (file.empty())
      {
          file = getArgument("--file");
      }
  
      return file;
  }
  
  std::string ArgumentParser::getMessage()
  {
      // -m | --message
      std::string message = getArgument("-m");
      if (message.empty())
      {
          message = getArgument("--message");
      }
  
      return message;
  }
  
  void ArgumentParser::printUsage()
  {
      std::cout << "Usage: " << getProgramName() << " [options]" << std::endl;
      std::cout << "Options:" << std::endl;
      std::cout << "  -h | --host <host>       Hostname or IP address of the MQTT broker. (Default 127.0.0.1)" << std::endl;
      std::cout << "  -p | --port <port>       Port number of the MQTT broker. (Default 1883)" << std::endl;
      std::cout << "  -u | --username <user>   Username for authentication" << std::endl;
      std::cout << "  -pw | --password <pass>  Password for authentication" << std::endl;
      std::cout << "  -t | --topic <topic>     Topic to publish the message to" << std::endl;
      std::cout << "  -f | --file <file>       File to publish" << std::endl;
      std::cout << "  -m | --message <message> Message to publish" << std::endl;
      std::cout << std::endl;
      std::cout << "Example(s): " << getProgramName() << " -h localhost -p 1883 -u user -pw password -t topic -f file.txt" << std::endl;
      std::cout << "            " << getProgramName() << " -h localhost -p 1883 -t topic -f file.txt" << std::endl;
      std::cout << "            " << getProgramName() << " -t topic -f file.txt" << std::endl;
      std::cout << std::endl;
  }