Skip to content

Commit bbed852

Browse files
Mandatory arguments in examples
Signed-off-by: Eugenio Collado <eugeniocollado@eprosima.com>
1 parent 94ca6c7 commit bbed852

4 files changed

Lines changed: 73 additions & 60 deletions

File tree

ddsenabler/examples/action/CLIParser.hpp

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class CLIParser
4949
static void print_help(
5050
uint8_t return_code)
5151
{
52-
std::cout << "Usage: ddsenabler_example_action [options]" <<
52+
std::cout << "Usage: ddsenabler_example_action <client|server> [options]" <<
5353
std::endl;
5454
std::cout << "" <<
5555
std::endl;
@@ -61,10 +61,6 @@ class CLIParser
6161
std::endl;
6262
std::cout << " (Default: 'Fibonacci/_action/')" <<
6363
std::endl;
64-
std::cout << "-client Run as a client (mutually exclusive with -server)" <<
65-
std::endl;
66-
std::cout << "-server Run as a server (mutually exclusive with -client)" <<
67-
std::endl;
6864
std::cout << "--timeout <num> Time (seconds) to wait before stopping the" <<
6965
std::endl;
7066
std::cout << " program if expectations are not met" <<
@@ -111,14 +107,37 @@ class CLIParser
111107

112108
if (argc < 2)
113109
{
114-
std::cerr << "Configuration file path is required" << std::endl;
110+
std::cerr << "Mode is required as the first argument: 'client' or 'server'" << std::endl;
115111
print_help(EXIT_FAILURE);
116112
}
117113

118114
bool client_flag = false;
119115
bool server_flag = false;
120116

121-
for (int i = 1; i < argc; ++i)
117+
// First positional argument must be the mode
118+
std::string mode = argv[1];
119+
if (mode == "-h" || mode == "--help")
120+
{
121+
print_help(EXIT_SUCCESS);
122+
}
123+
else if (mode == "client")
124+
{
125+
client_flag = true;
126+
config.announce_server = false;
127+
}
128+
else if (mode == "server")
129+
{
130+
server_flag = true;
131+
config.announce_server = true;
132+
}
133+
else
134+
{
135+
std::cerr << "Invalid mode '" << mode << "'. First argument must be 'client' or 'server'" << std::endl;
136+
print_help(EXIT_FAILURE);
137+
}
138+
139+
// Parse optional arguments starting from index 2
140+
for (int i = 2; i < argc; ++i)
122141
{
123142
std::string arg = argv[i];
124143

@@ -155,26 +174,6 @@ class CLIParser
155174
print_help(EXIT_FAILURE);
156175
}
157176
}
158-
else if (arg == "-client")
159-
{
160-
if (server_flag)
161-
{
162-
std::cerr << "Cannot specify both -client and -server flags" << std::endl;
163-
print_help(EXIT_FAILURE);
164-
}
165-
client_flag = true;
166-
config.announce_server = false;
167-
}
168-
else if (arg == "-server")
169-
{
170-
if (client_flag)
171-
{
172-
std::cerr << "Cannot specify both -client and -server flags" << std::endl;
173-
print_help(EXIT_FAILURE);
174-
}
175-
server_flag = true;
176-
config.announce_server = true;
177-
}
178177
else if (arg == "--timeout")
179178
{
180179
if (++i < argc)
@@ -270,9 +269,10 @@ class CLIParser
270269
}
271270
}
272271

272+
// Sanity check (should be set from the positional mode)
273273
if (!client_flag && !server_flag)
274274
{
275-
std::cerr << "Either -client or -server flag must be specified" << std::endl;
275+
std::cerr << "Either 'client' or 'server' must be specified as the first argument" << std::endl;
276276
print_help(EXIT_FAILURE);
277277
}
278278

ddsenabler/examples/action/Readme.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,17 @@ Users are encouraged to implement their own functional server logic as needed in
1010

1111
### CLIENT
1212
```bash
13-
./install/ddsenabler/bin/ddsenabler_example_action -client --persistence-path $PATH_TO_WS/ddsenabler/ddsenabler/examples/persistence/ --config $PATH_TO_WS/ddsenabler/ddsenabler/DDS_ENABLER_CONFIGURATION.yaml --request-initial-wait 3 --cancel-requests false
13+
./install/ddsenabler/bin/ddsenabler_example_action client \
14+
--persistence-path $PATH_TO_WS/ddsenabler/ddsenabler/examples/persistence/ \
15+
--config $PATH_TO_WS/ddsenabler/ddsenabler/DDS_ENABLER_CONFIGURATION.yaml \
16+
--request-initial-wait 3 \
17+
--cancel-requests false
1418
```
1519

1620
### SERVER
1721
```bash
18-
./install/ddsenabler/bin/ddsenabler_example_action -server --persistence-path $PATH_TO_WS/ddsenabler/ddsenabler/examples/persistence/ --config $PATH_TO_WS/ddsenabler/ddsenabler/DDS_ENABLER_CONFIGURATION.yaml --expected-requests 3
22+
./install/ddsenabler/bin/ddsenabler_example_action server \
23+
--persistence-path $PATH_TO_WS/ddsenabler/ddsenabler/examples/persistence/ \
24+
--config $PATH_TO_WS/ddsenabler/ddsenabler/DDS_ENABLER_CONFIGURATION.yaml \
25+
--expected-requests 3
1926
```

ddsenabler/examples/service/CLIParser.hpp

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class CLIParser
4848
static void print_help(
4949
uint8_t return_code)
5050
{
51-
std::cout << "Usage: ddsenabler_example_service [options]" <<
51+
std::cout << "Usage: ddsenabler_example_service <client|server> [options]" <<
5252
std::endl;
5353
std::cout << "" <<
5454
std::endl;
@@ -60,10 +60,6 @@ class CLIParser
6060
std::endl;
6161
std::cout << " (Default: 'add_two_ints')" <<
6262
std::endl;
63-
std::cout << "-client Run as a client (mutually exclusive with -server)" <<
64-
std::endl;
65-
std::cout << "-server Run as a server (mutually exclusive with -client)" <<
66-
std::endl;
6763
std::cout << "--timeout <num> Time (seconds) to wait before stopping the" <<
6864
std::endl;
6965
std::cout << " program if expectations are not met" <<
@@ -106,14 +102,37 @@ class CLIParser
106102

107103
if (argc < 2)
108104
{
109-
std::cerr << "Configuration file path is required" << std::endl;
105+
std::cerr << "Mode is required as the first argument: 'client' or 'server'" << std::endl;
110106
print_help(EXIT_FAILURE);
111107
}
112108

113109
bool client_flag = false;
114110
bool server_flag = false;
115111

116-
for (int i = 1; i < argc; ++i)
112+
// First positional argument must be the mode
113+
std::string mode = argv[1];
114+
if (mode == "-h" || mode == "--help")
115+
{
116+
print_help(EXIT_SUCCESS);
117+
}
118+
else if (mode == "client")
119+
{
120+
client_flag = true;
121+
config.announce_server = false;
122+
}
123+
else if (mode == "server")
124+
{
125+
server_flag = true;
126+
config.announce_server = true;
127+
}
128+
else
129+
{
130+
std::cerr << "Invalid mode '" << mode << "'. First argument must be 'client' or 'server'" << std::endl;
131+
print_help(EXIT_FAILURE);
132+
}
133+
134+
// Parse optional arguments starting from index 2
135+
for (int i = 2; i < argc; ++i)
117136
{
118137
std::string arg = argv[i];
119138

@@ -150,26 +169,6 @@ class CLIParser
150169
print_help(EXIT_FAILURE);
151170
}
152171
}
153-
else if (arg == "-client")
154-
{
155-
if (server_flag)
156-
{
157-
std::cerr << "Cannot specify both -client and -server flags" << std::endl;
158-
print_help(EXIT_FAILURE);
159-
}
160-
client_flag = true;
161-
config.announce_server = false;
162-
}
163-
else if (arg == "-server")
164-
{
165-
if (client_flag)
166-
{
167-
std::cerr << "Cannot specify both -client and -server flags" << std::endl;
168-
print_help(EXIT_FAILURE);
169-
}
170-
server_flag = true;
171-
config.announce_server = true;
172-
}
173172
else if (arg == "--timeout")
174173
{
175174
if (++i < argc)
@@ -251,9 +250,10 @@ class CLIParser
251250
}
252251
}
253252

253+
// Sanity check (should be set from the positional mode)
254254
if (!client_flag && !server_flag)
255255
{
256-
std::cerr << "Either -client or -server flag must be specified" << std::endl;
256+
std::cerr << "Either 'client' or 'server' must be specified as the first argument" << std::endl;
257257
print_help(EXIT_FAILURE);
258258
}
259259

ddsenabler/examples/service/Readme.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@ Users are encouraged to implement their own functional server logic as needed in
1010

1111
### CLIENT
1212
```bash
13-
./install/ddsenabler/bin/ddsenabler_example_service -client --persistence-path $PATH_TO_WS/ddsenabler/ddsenabler/examples/persistence/ --config $PATH_TO_WS/ddsenabler/ddsenabler/DDS_ENABLER_CONFIGURATION.yaml --request-initial-wait 3
13+
./install/ddsenabler/bin/ddsenabler_example_service client \
14+
--persistence-path $PATH_TO_WS/ddsenabler/ddsenabler/examples/persistence/ \
15+
--config $PATH_TO_WS/ddsenabler/ddsenabler/DDS_ENABLER_CONFIGURATION.yaml \
16+
--request-initial-wait 3
1417
```
1518

1619
### SERVER
1720
```bash
18-
./install/ddsenabler/bin/ddsenabler_example_service -server --persistence-path $PATH_TO_WS/ddsenabler/ddsenabler/examples/persistence/ --config $PATH_TO_WS/ddsenabler/ddsenabler/DDS_ENABLER_CONFIGURATION.yaml --expected-requests 3
21+
./install/ddsenabler/bin/ddsenabler_example_service server \
22+
--persistence-path $PATH_TO_WS/ddsenabler/ddsenabler/examples/persistence/ \
23+
--config $PATH_TO_WS/ddsenabler/ddsenabler/DDS_ENABLER_CONFIGURATION.yaml \
24+
--expected-requests 3
1925
```

0 commit comments

Comments
 (0)