-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_commands.cpp
58 lines (45 loc) · 1.42 KB
/
parse_commands.cpp
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <cstdlib>
#include <stdio.h> // Standard input/output definitions
#include <unistd.h> // UNIX standard function definitions
#include <fcntl.h> // File control definitions
#include <termios.h> // POSIX terminal control definitions
#include <signal.h>
// ------------------------------------------------------------------------------
// Parse Command Line
// ------------------------------------------------------------------------------
// throws EXIT_FAILURE if could not open the port
void
parse_commandline(int argc, char **argv, char *&uart_name, int &baudrate)
{
// string for command line usage
const char *commandline_usage = "usage: mavlink_serial -d <devicename> -b <baudrate>";
// Read input arguments
for (int i = 1; i < argc; i++) { // argv[0] is "mavlink"
// Help
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
printf("%s\n",commandline_usage);
throw EXIT_FAILURE;
}
// UART device ID
if (strcmp(argv[i], "-d") == 0 || strcmp(argv[i], "--device") == 0) {
if (argc > i + 1) {
uart_name = argv[i + 1];
} else {
printf("%s\n",commandline_usage);
throw EXIT_FAILURE;
}
}
// Baud rate
if (strcmp(argv[i], "-b") == 0 || strcmp(argv[i], "--baud") == 0) {
if (argc > i + 1) {
baudrate = atoi(argv[i + 1]);
} else {
printf("%s\n",commandline_usage);
throw EXIT_FAILURE;
}
}
}
// end: for each input argument
// Done!
return;
}