-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommandline.cpp
71 lines (56 loc) · 1.73 KB
/
commandline.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
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <stdexcept>
#include <getopt.h>
#include "commandline.hpp"
#include "string.hpp"
using namespace std;
namespace dsmq {
static char const* cmdLineMapShortToLong( int shortopt )
{
switch ( shortopt ) {
case 'h': return "help";
case 'c': return "config-file";
case 'l': return "log-file";
default: throw invalid_argument( str( "cmdLineMapShortToLong( ", static_cast< char >( shortopt ), ")" ));
}
}
CommandLine::CommandLine( char* const *argv, int argc )
: propertiesFile_ { "dsmqbridge.json" }
{
struct option options[] = {
{ nullptr, no_argument, nullptr, 'h' },
{ nullptr, required_argument, nullptr, 'c' },
{ nullptr, required_argument, nullptr, 'l' },
{}
};
for ( auto& option : options ) {
if ( option.val != 0 ) {
option.name = cmdLineMapShortToLong( option.val );
}
}
opterr = 0;
int optchar;
int optind;
while ( ( optchar = getopt_long( argc, argv, ":hc:l:", options, &optind ) ) != -1 ) {
switch ( optchar ) {
case ':':
throw CommandLineError( str( "missing argument to --", cmdLineMapShortToLong( optopt ) ) );
case '?':
throw CommandLineError( str( "unknown option -", static_cast< char >( optopt ) ) );
case 'h':
throw CommandLineError( str( "Usage: ", argv[ 0 ], " [OPTION]...\n",
" -c, --config-file=FILE load configuration from FILE\n",
" -l, --log-file=FILE write logs to FILE instead of standard error\n",
" -h, --help show this help and exit" ));
case 'c':
propertiesFile_ = optarg;
break;
case 'l':
logFile_ = optarg;
break;
default:
throw invalid_argument( string { "getopt_long( ... ) -> '" } + static_cast< char >( optchar ) + "'" );
}
}
}
} // namespace dsmq