• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright Sascha Ochsenknecht 2009.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt
4 // or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 
7 #include <boost/program_options/parsers.hpp>
8 #include <boost/program_options/options_description.hpp>
9 #include <boost/program_options/variables_map.hpp>
10 #include <boost/program_options/cmdline.hpp>
11 using namespace boost::program_options;
12 
13 #include <iostream>
14 #include <sstream>
15 #include <vector>
16 #include <cassert>
17 using namespace std;
18 
19 #include "minitest.hpp"
20 
check_value(const string & option,const string & value)21 void check_value(const string& option, const string& value)
22 {
23     BOOST_CHECK(option == value);
24 }
25 
split_whitespace(const options_description & description)26 void split_whitespace(const options_description& description)
27 {
28 
29    const char* cmdline = "prg --input input.txt \r --optimization 4  \t  --opt \n  option";
30 
31    vector< string > tokens =  split_unix(cmdline, " \t\n\r");
32 
33    BOOST_REQUIRE(tokens.size() == 7);
34 
35    check_value(tokens[0], "prg");
36    check_value(tokens[1], "--input");
37    check_value(tokens[2], "input.txt");
38    check_value(tokens[3], "--optimization");
39    check_value(tokens[4], "4");
40    check_value(tokens[5], "--opt");
41    check_value(tokens[6], "option");
42 
43    variables_map vm;
44    store(command_line_parser(tokens).options(description).run(), vm);
45    notify(vm);
46 }
47 
split_equalsign(const options_description & description)48 void split_equalsign(const options_description& description)
49 {
50 
51    const char* cmdline = "prg --input=input.txt  --optimization=4 --opt=option";
52 
53    vector< string > tokens =  split_unix(cmdline, "= ");
54 
55    BOOST_REQUIRE(tokens.size() == 7);
56    check_value(tokens[0], "prg");
57    check_value(tokens[1], "--input");
58    check_value(tokens[2], "input.txt");
59    check_value(tokens[3], "--optimization");
60    check_value(tokens[4], "4");
61    check_value(tokens[5], "--opt");
62    check_value(tokens[6], "option");
63 
64    variables_map vm;
65    store(command_line_parser(tokens).options(description).run(), vm);
66    notify(vm);
67 }
68 
split_semi(const options_description & description)69 void split_semi(const options_description& description)
70 {
71 
72    const char* cmdline = "prg;--input input.txt;--optimization 4;--opt option";
73 
74    vector< string > tokens =  split_unix(cmdline, "; ");
75 
76    BOOST_REQUIRE(tokens.size() == 7);
77    check_value(tokens[0], "prg");
78    check_value(tokens[1], "--input");
79    check_value(tokens[2], "input.txt");
80    check_value(tokens[3], "--optimization");
81    check_value(tokens[4], "4");
82    check_value(tokens[5], "--opt");
83    check_value(tokens[6], "option");
84 
85    variables_map vm;
86    store(command_line_parser(tokens).options(description).run(), vm);
87    notify(vm);
88 }
89 
split_quotes(const options_description & description)90 void split_quotes(const options_description& description)
91 {
92    const char* cmdline = "prg --input \"input.txt input.txt\" --optimization 4 --opt \"option1 option2\"";
93 
94    vector< string > tokens =  split_unix(cmdline, " ");
95 
96    BOOST_REQUIRE(tokens.size() == 7);
97    check_value(tokens[0], "prg");
98    check_value(tokens[1], "--input");
99    check_value(tokens[2], "input.txt input.txt");
100    check_value(tokens[3], "--optimization");
101    check_value(tokens[4], "4");
102    check_value(tokens[5], "--opt");
103    check_value(tokens[6], "option1 option2");
104 
105    variables_map vm;
106    store(command_line_parser(tokens).options(description).run(), vm);
107    notify(vm);
108 }
109 
split_escape(const options_description & description)110 void split_escape(const options_description& description)
111 {
112    const char* cmdline = "prg --input \\\"input.txt\\\" --optimization 4 --opt \\\"option1\\ option2\\\"";
113 
114    vector< string > tokens =  split_unix(cmdline, " ");
115 
116    BOOST_REQUIRE(tokens.size() == 7);
117    check_value(tokens[0], "prg");
118    check_value(tokens[1], "--input");
119    check_value(tokens[2], "\"input.txt\"");
120    check_value(tokens[3], "--optimization");
121    check_value(tokens[4], "4");
122    check_value(tokens[5], "--opt");
123    check_value(tokens[6], "\"option1 option2\"");
124 
125    variables_map vm;
126    store(command_line_parser(tokens).options(description).run(), vm);
127    notify(vm);
128 }
129 
130 
split_single_quote(const options_description & description)131 void split_single_quote(const options_description& description)
132 {
133    const char* cmdline = "prg --input 'input.txt input.txt' --optimization 4 --opt 'option1 option2'";
134 
135    vector< string > tokens =  split_unix(cmdline, " ", "'");
136 
137    BOOST_REQUIRE(tokens.size() == 7);
138    check_value(tokens[0], "prg");
139    check_value(tokens[1], "--input");
140    check_value(tokens[2], "input.txt input.txt");
141    check_value(tokens[3], "--optimization");
142    check_value(tokens[4], "4");
143    check_value(tokens[5], "--opt");
144    check_value(tokens[6], "option1 option2");
145 
146    variables_map vm;
147    store(command_line_parser(tokens).options(description).run(), vm);
148    notify(vm);
149 }
150 
split_defaults(const options_description & description)151 void split_defaults(const options_description& description)
152 {
153    const char* cmdline = "prg --input \t \'input file.txt\' \t   --optimization 4 --opt \\\"option1\\ option2\\\"";
154 
155    vector< string > tokens =  split_unix(cmdline);
156 
157    BOOST_REQUIRE(tokens.size() == 7);
158    check_value(tokens[0], "prg");
159    check_value(tokens[1], "--input");
160    check_value(tokens[2], "input file.txt");
161    check_value(tokens[3], "--optimization");
162    check_value(tokens[4], "4");
163    check_value(tokens[5], "--opt");
164    check_value(tokens[6], "\"option1 option2\"");
165 
166    variables_map vm;
167    store(command_line_parser(tokens).options(description).run(), vm);
168    notify(vm);
169 }
170 
main(int,char **)171 int main(int /*ac*/, char** /*av*/)
172 {
173    options_description desc;
174    desc.add_options()
175         ("input,i", value<string>(), "the input file")
176         ("optimization,O", value<unsigned>(), "optimization level")
177         ("opt,o", value<string>(), "misc option")
178       ;
179 
180    split_whitespace(desc);
181    split_equalsign(desc);
182    split_semi(desc);
183    split_quotes(desc);
184    split_escape(desc);
185    split_single_quote(desc);
186    split_defaults(desc);
187 
188    return 0;
189 }
190