-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_options.cpp
86 lines (68 loc) · 2.27 KB
/
parse_options.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <string.h>
#include <string>
#include <vector>
#include <filesystem>
#include "parse_options.hpp"
// This program is a test of how to parse options in C++
struct programOptions
{
bool verbose{false};
std::filesystem::path input;
std::filesystem::path output;
std::vector<std::string> source; // the source files
int integer{0};
};
/* ----------------------------------------------------------------------------
* process_test
---------------------------------------------------------------------------- */
int process_test( const programOptions& options, const std::string_view& file_path )
{
std::cout << "Verbose: " << options.verbose << "\n";
std::cout << "input_path: " << options.input << "\n";
std::cout << "output_path: " << options.output << "\n";
std::cout << "file_path: " << file_path << "\n";
return 0;
}
/* ----------------------------------------------------------------------------
* main
---------------------------------------------------------------------------- */
int main( int argc, char* argv[] )
{
programOptions options;
parse_options::OptionParser parser( "This is the test framework for the option parser" );
parser.add( "verbose", "Print semi-useful stuff", &options.verbose );
parser.add( "input_path", "The path to read information from", &options.input );
parser.add( "output_path", "The path to write data to", &options.output );
parser.add( "real_long_option_name", "This option has a lot of text to test wrapping", &options.integer );
int status = 0;
if( 1 < argc )
{
try
{
parser.parse( argc, argv );
// If we didn't throw, then out options were parsed correctly.
for( const auto& one : parser.non_option_args())
{
status = process_test( options, one );
if( status != 0 ) break;
}
}
catch( std::invalid_argument& e1 )
{
std::cerr << "# Failed to parse program options\n";
std::cerr << "# " << e1.what();
status = 1;
}
catch( const std::exception& e2 )
{
std::cerr << "ERROR: " << e2.what() << "\n";
status = 2;
}
}
else
{
std::cout << parser.usage().c_str();
}
return status;
}