Commit 8725de6706ea495e3bed6694a9977eb4d850995b

Authored by Henry Fredrick Schreiner
1 parent 22612c51

Fixes to enable windows compat

CMakeLists.txt
@@ -15,7 +15,7 @@ endif() @@ -15,7 +15,7 @@ endif()
15 15
16 # Be moderately paranoid with flags 16 # Be moderately paranoid with flags
17 if(CMAKE_COMPILER_IS_GNUCC) 17 if(CMAKE_COMPILER_IS_GNUCC)
18 - add_definitions("-Wall -Wextra") 18 + add_definitions("-Wall -Wextra -pedantic")
19 endif() 19 endif()
20 if(MSVC) 20 if(MSVC)
21 add_definitions("/W4") 21 add_definitions("/W4")
include/CLI/App.hpp
@@ -112,9 +112,9 @@ public: @@ -112,9 +112,9 @@ public:
112 } 112 }
113 113
114 /// Add a subcommand. Like the constructor, you can override the help message addition by setting help=false 114 /// Add a subcommand. Like the constructor, you can override the help message addition by setting help=false
115 - App* add_subcommand(std::string name, std::string description="", bool help=true) { 115 + App* add_subcommand(std::string name_, std::string description="", bool help=true) {
116 subcommands.emplace_back(new App(description, help)); 116 subcommands.emplace_back(new App(description, help));
117 - subcommands.back()->name = name; 117 + subcommands.back()->name = name_;
118 return subcommands.back().get(); 118 return subcommands.back().get();
119 } 119 }
120 120
@@ -132,17 +132,17 @@ public: @@ -132,17 +132,17 @@ public:
132 * program.add_option("filename", filename, "description of filename"); 132 * program.add_option("filename", filename, "description of filename");
133 */ 133 */
134 Option* add_option( 134 Option* add_option(
135 - std::string name, 135 + std::string name_,
136 callback_t callback, 136 callback_t callback,
137 std::string description="", 137 std::string description="",
138 bool defaulted=false 138 bool defaulted=false
139 ) { 139 ) {
140 - Option myopt{name, description, callback, defaulted}; 140 + Option myopt{name_, description, callback, defaulted};
141 if(std::find_if(std::begin(options), std::end(options), 141 if(std::find_if(std::begin(options), std::end(options),
142 [&myopt](const Option_p &v){return *v == myopt;}) == std::end(options)) { 142 [&myopt](const Option_p &v){return *v == myopt;}) == std::end(options)) {
143 options.emplace_back(); 143 options.emplace_back();
144 Option_p& option = options.back(); 144 Option_p& option = options.back();
145 - option.reset(new Option(name, description, callback, defaulted)); 145 + option.reset(new Option(name_, description, callback, defaulted));
146 return option.get(); 146 return option.get();
147 } else 147 } else
148 throw OptionAlreadyAdded(myopt.get_name()); 148 throw OptionAlreadyAdded(myopt.get_name());
@@ -152,7 +152,7 @@ public: @@ -152,7 +152,7 @@ public:
152 /// Add option for string 152 /// Add option for string
153 template<typename T, enable_if_t<!is_vector<T>::value, detail::enabler> = detail::dummy> 153 template<typename T, enable_if_t<!is_vector<T>::value, detail::enabler> = detail::dummy>
154 Option* add_option( 154 Option* add_option(
155 - std::string name, 155 + std::string name_,
156 T &variable, ///< The variable to set 156 T &variable, ///< The variable to set
157 std::string description="", 157 std::string description="",
158 bool defaulted=false 158 bool defaulted=false
@@ -169,7 +169,7 @@ public: @@ -169,7 +169,7 @@ public:
169 return detail::lexical_cast(res[0][0], variable); 169 return detail::lexical_cast(res[0][0], variable);
170 }; 170 };
171 171
172 - Option* retval = add_option(name, fun, description, defaulted); 172 + Option* retval = add_option(name_, fun, description, defaulted);
173 retval->typeval = detail::type_name<T>(); 173 retval->typeval = detail::type_name<T>();
174 if(defaulted) { 174 if(defaulted) {
175 std::stringstream out; 175 std::stringstream out;
@@ -182,7 +182,7 @@ public: @@ -182,7 +182,7 @@ public:
182 /// Add option for vector of results 182 /// Add option for vector of results
183 template<typename T> 183 template<typename T>
184 Option* add_option( 184 Option* add_option(
185 - std::string name, 185 + std::string name_,
186 std::vector<T> &variable, ///< The variable vector to set 186 std::vector<T> &variable, ///< The variable vector to set
187 std::string description="", 187 std::string description="",
188 bool defaulted=false 188 bool defaulted=false
@@ -199,7 +199,7 @@ public: @@ -199,7 +199,7 @@ public:
199 return variable.size() > 0 && retval; 199 return variable.size() > 0 && retval;
200 }; 200 };
201 201
202 - Option* retval = add_option(name, fun, description, defaulted); 202 + Option* retval = add_option(name_, fun, description, defaulted);
203 retval->allow_vector = true; 203 retval->allow_vector = true;
204 retval->_expected = -1; 204 retval->_expected = -1;
205 retval->typeval = detail::type_name<T>(); 205 retval->typeval = detail::type_name<T>();
@@ -211,14 +211,14 @@ public: @@ -211,14 +211,14 @@ public:
211 211
212 /// Add option for flag 212 /// Add option for flag
213 Option* add_flag( 213 Option* add_flag(
214 - std::string name, 214 + std::string name_,
215 std::string description="" 215 std::string description=""
216 ) { 216 ) {
217 CLI::callback_t fun = [](CLI::results_t){ 217 CLI::callback_t fun = [](CLI::results_t){
218 return true; 218 return true;
219 }; 219 };
220 220
221 - Option* opt = add_option(name, fun, description, false); 221 + Option* opt = add_option(name_, fun, description, false);
222 if(opt->get_positional()) 222 if(opt->get_positional())
223 throw IncorrectConstruction("Flags cannot be positional"); 223 throw IncorrectConstruction("Flags cannot be positional");
224 opt->_expected = 0; 224 opt->_expected = 0;
@@ -229,7 +229,7 @@ public: @@ -229,7 +229,7 @@ public:
229 template<typename T, 229 template<typename T,
230 enable_if_t<std::is_integral<T>::value && !is_bool<T>::value, detail::enabler> = detail::dummy> 230 enable_if_t<std::is_integral<T>::value && !is_bool<T>::value, detail::enabler> = detail::dummy>
231 Option* add_flag( 231 Option* add_flag(
232 - std::string name, 232 + std::string name_,
233 T &count, ///< A varaible holding the count 233 T &count, ///< A varaible holding the count
234 std::string description="" 234 std::string description=""
235 ) { 235 ) {
@@ -240,7 +240,7 @@ public: @@ -240,7 +240,7 @@ public:
240 return true; 240 return true;
241 }; 241 };
242 242
243 - Option* opt = add_option(name, fun, description, false); 243 + Option* opt = add_option(name_, fun, description, false);
244 if(opt->get_positional()) 244 if(opt->get_positional())
245 throw IncorrectConstruction("Flags cannot be positional"); 245 throw IncorrectConstruction("Flags cannot be positional");
246 opt->_expected = 0; 246 opt->_expected = 0;
@@ -251,7 +251,7 @@ public: @@ -251,7 +251,7 @@ public:
251 template<typename T, 251 template<typename T,
252 enable_if_t<is_bool<T>::value, detail::enabler> = detail::dummy> 252 enable_if_t<is_bool<T>::value, detail::enabler> = detail::dummy>
253 Option* add_flag( 253 Option* add_flag(
254 - std::string name, 254 + std::string name_,
255 T &count, ///< A varaible holding true if passed 255 T &count, ///< A varaible holding true if passed
256 std::string description="" 256 std::string description=""
257 ) { 257 ) {
@@ -262,7 +262,7 @@ public: @@ -262,7 +262,7 @@ public:
262 return res.size() == 1; 262 return res.size() == 1;
263 }; 263 };
264 264
265 - Option* opt = add_option(name, fun, description, false); 265 + Option* opt = add_option(name_, fun, description, false);
266 if(opt->get_positional()) 266 if(opt->get_positional())
267 throw IncorrectConstruction("Flags cannot be positional"); 267 throw IncorrectConstruction("Flags cannot be positional");
268 opt->_expected = 0; 268 opt->_expected = 0;
@@ -273,7 +273,7 @@ public: @@ -273,7 +273,7 @@ public:
273 /// Add set of options 273 /// Add set of options
274 template<typename T> 274 template<typename T>
275 Option* add_set( 275 Option* add_set(
276 - std::string name, 276 + std::string name_,
277 T &member, ///< The selected member of the set 277 T &member, ///< The selected member of the set
278 std::set<T> _options, ///< The set of posibilities 278 std::set<T> _options, ///< The set of posibilities
279 std::string description="", 279 std::string description="",
@@ -293,7 +293,7 @@ public: @@ -293,7 +293,7 @@ public:
293 return std::find(std::begin(_options), std::end(_options), member) != std::end(_options); 293 return std::find(std::begin(_options), std::end(_options), member) != std::end(_options);
294 }; 294 };
295 295
296 - Option* retval = add_option(name, fun, description, defaulted); 296 + Option* retval = add_option(name_, fun, description, defaulted);
297 retval->typeval = detail::type_name<T>(); 297 retval->typeval = detail::type_name<T>();
298 retval->typeval += " in {" + detail::join(_options) + "}"; 298 retval->typeval += " in {" + detail::join(_options) + "}";
299 if(defaulted) { 299 if(defaulted) {
@@ -306,7 +306,7 @@ public: @@ -306,7 +306,7 @@ public:
306 306
307 307
308 /// Add a configuration ini file option 308 /// Add a configuration ini file option
309 - Option* add_config(std::string name="--config", 309 + Option* add_config(std::string name_="--config",
310 std::string default_filename="", 310 std::string default_filename="",
311 std::string help="Read an ini file", 311 std::string help="Read an ini file",
312 bool required=false) { 312 bool required=false) {
@@ -316,7 +316,7 @@ public: @@ -316,7 +316,7 @@ public:
316 remove_option(ini_setting); 316 remove_option(ini_setting);
317 ini_file = default_filename; 317 ini_file = default_filename;
318 ini_required = required; 318 ini_required = required;
319 - ini_setting = add_option(name, ini_file, help, default_filename!=""); 319 + ini_setting = add_option(name_, ini_file, help, default_filename!="");
320 return ini_setting; 320 return ini_setting;
321 } 321 }
322 322
@@ -366,13 +366,13 @@ public: @@ -366,13 +366,13 @@ public:
366 } 366 }
367 367
368 /// Counts the number of times the given option was passed. 368 /// Counts the number of times the given option was passed.
369 - int count(std::string name) const { 369 + int count(std::string name_) const {
370 for(const Option_p &opt : options) { 370 for(const Option_p &opt : options) {
371 - if(opt->check_name(name)) { 371 + if(opt->check_name(name_)) {
372 return opt->count(); 372 return opt->count();
373 } 373 }
374 } 374 }
375 - throw OptionNotFound(name); 375 + throw OptionNotFound(name_);
376 } 376 }
377 377
378 /// Makes a help message, with a column `wid` for column 1 378 /// Makes a help message, with a column `wid` for column 1
@@ -547,7 +547,7 @@ protected: @@ -547,7 +547,7 @@ protected:
547 _parse_long(values, false); 547 _parse_long(values, false);
548 } 548 }
549 549
550 - } catch (const FileError &e) { 550 + } catch (const FileError &) {
551 if(ini_required) 551 if(ini_required)
552 throw; 552 throw;
553 } 553 }
@@ -615,16 +615,16 @@ protected: @@ -615,16 +615,16 @@ protected:
615 void _parse_short(std::vector<std::string> &args) { 615 void _parse_short(std::vector<std::string> &args) {
616 std::string current = args.back(); 616 std::string current = args.back();
617 617
618 - std::string name; 618 + std::string name_;
619 std::string rest; 619 std::string rest;
620 - if(!detail::split_short(current, name, rest)) 620 + if(!detail::split_short(current, name_, rest))
621 throw HorribleError("Short"); 621 throw HorribleError("Short");
622 args.pop_back(); 622 args.pop_back();
623 623
624 - auto op_ptr = std::find_if(std::begin(options), std::end(options), [name](const Option_p &opt){return opt->check_sname(name);}); 624 + auto op_ptr = std::find_if(std::begin(options), std::end(options), [name_](const Option_p &opt){return opt->check_sname(name_);});
625 625
626 if(op_ptr == std::end(options)) { 626 if(op_ptr == std::end(options)) {
627 - missing_options.push_back("-" + name); 627 + missing_options.push_back("-" + name_);
628 return; 628 return;
629 } 629 }
630 630
@@ -651,9 +651,9 @@ protected: @@ -651,9 +651,9 @@ protected:
651 } 651 }
652 } else while(num>0 && args.size() > 0) { 652 } else while(num>0 && args.size() > 0) {
653 num--; 653 num--;
654 - std::string current = args.back(); 654 + std::string current_ = args.back();
655 args.pop_back(); 655 args.pop_back();
656 - op->add_result(vnum,current); 656 + op->add_result(vnum, current_);
657 } 657 }
658 658
659 if(rest != "") { 659 if(rest != "") {
@@ -666,16 +666,16 @@ protected: @@ -666,16 +666,16 @@ protected:
666 void _parse_long(std::vector<std::string> &args, bool overwrite=true) { 666 void _parse_long(std::vector<std::string> &args, bool overwrite=true) {
667 std::string current = args.back(); 667 std::string current = args.back();
668 668
669 - std::string name; 669 + std::string name_;
670 std::string value; 670 std::string value;
671 - if(!detail::split_long(current, name, value)) 671 + if(!detail::split_long(current, name_, value))
672 throw HorribleError("Long"); 672 throw HorribleError("Long");
673 args.pop_back(); 673 args.pop_back();
674 674
675 - auto op_ptr = std::find_if(std::begin(options), std::end(options), [name](const Option_p &v){return v->check_lname(name);}); 675 + auto op_ptr = std::find_if(std::begin(options), std::end(options), [name_](const Option_p &v){return v->check_lname(name_);});
676 676
677 if(op_ptr == std::end(options)) { 677 if(op_ptr == std::end(options)) {
678 - missing_options.push_back("--" + name); 678 + missing_options.push_back("--" + name_);
679 return; 679 return;
680 } 680 }
681 681
include/CLI/StringTools.hpp
@@ -94,13 +94,13 @@ void format_help(std::stringstream &amp;out, std::string name, std::string descripti @@ -94,13 +94,13 @@ void format_help(std::stringstream &amp;out, std::string name, std::string descripti
94 /// Verify the first character of an option 94 /// Verify the first character of an option
95 template<typename T> 95 template<typename T>
96 bool valid_first_char(T c) { 96 bool valid_first_char(T c) {
97 - return std::isalpha(c) || c=='_'; 97 + return std::isalpha(c, std::locale::classic()) || c=='_';
98 } 98 }
99 99
100 /// Verify following characters of an option 100 /// Verify following characters of an option
101 template<typename T> 101 template<typename T>
102 bool valid_later_char(T c) { 102 bool valid_later_char(T c) {
103 - return std::isalnum(c) || c=='_' || c=='.' || c=='-'; 103 + return std::isalnum(c, std::locale::classic()) || c=='_' || c=='.' || c=='-';
104 } 104 }
105 105
106 /// Verify an option name 106 /// Verify an option name
include/CLI/Validators.hpp
@@ -27,7 +27,7 @@ namespace CLI { @@ -27,7 +27,7 @@ namespace CLI {
27 bool ExistingFile(std::string filename) { 27 bool ExistingFile(std::string filename) {
28 struct stat buffer; 28 struct stat buffer;
29 bool exist = stat(filename.c_str(), &buffer) == 0; 29 bool exist = stat(filename.c_str(), &buffer) == 0;
30 - bool is_dir = buffer.st_mode & S_IFDIR; 30 + bool is_dir = buffer.st_mode & S_IFDIR != 0;
31 if(!exist) { 31 if(!exist) {
32 std::cerr << "File does not exist: " << filename << std::endl; 32 std::cerr << "File does not exist: " << filename << std::endl;
33 return false; 33 return false;
@@ -43,7 +43,7 @@ bool ExistingFile(std::string filename) { @@ -43,7 +43,7 @@ bool ExistingFile(std::string filename) {
43 bool ExistingDirectory(std::string filename) { 43 bool ExistingDirectory(std::string filename) {
44 struct stat buffer; 44 struct stat buffer;
45 bool exist = stat(filename.c_str(), &buffer) == 0; 45 bool exist = stat(filename.c_str(), &buffer) == 0;
46 - bool is_dir = buffer.st_mode & S_IFDIR; 46 + bool is_dir = buffer.st_mode & S_IFDIR != 0;
47 if(!exist) { 47 if(!exist) {
48 std::cerr << "Directory does not exist: " << filename << std::endl; 48 std::cerr << "Directory does not exist: " << filename << std::endl;
49 return false; 49 return false;
tests/AppTest.cpp
@@ -250,7 +250,7 @@ TEST_F(TApp, Reset) { @@ -250,7 +250,7 @@ TEST_F(TApp, Reset) {
250 250
251 EXPECT_EQ(1, app.count("--simple")); 251 EXPECT_EQ(1, app.count("--simple"));
252 EXPECT_EQ(1, app.count("-d")); 252 EXPECT_EQ(1, app.count("-d"));
253 - EXPECT_FLOAT_EQ(1.2, doub); 253 + EXPECT_DOUBLE_EQ(1.2, doub);
254 254
255 app.reset(); 255 app.reset();
256 256
@@ -261,7 +261,7 @@ TEST_F(TApp, Reset) { @@ -261,7 +261,7 @@ TEST_F(TApp, Reset) {
261 261
262 EXPECT_EQ(1, app.count("--simple")); 262 EXPECT_EQ(1, app.count("--simple"));
263 EXPECT_EQ(1, app.count("-d")); 263 EXPECT_EQ(1, app.count("-d"));
264 - EXPECT_FLOAT_EQ(1.2, doub); 264 + EXPECT_DOUBLE_EQ(1.2, doub);
265 265
266 } 266 }
267 267
@@ -489,7 +489,7 @@ TEST_F(TApp, RequiresChainedFlags) { @@ -489,7 +489,7 @@ TEST_F(TApp, RequiresChainedFlags) {
489 489
490 TEST_F(TApp, Env) { 490 TEST_F(TApp, Env) {
491 491
492 - setenv("CLI11_TEST_ENV_TMP", "2", true); 492 + put_env("CLI11_TEST_ENV_TMP", "2", true);
493 493
494 int val=1; 494 int val=1;
495 CLI::Option* vopt = app.add_option("--tmp", val)->envname("CLI11_TEST_ENV_TMP"); 495 CLI::Option* vopt = app.add_option("--tmp", val)->envname("CLI11_TEST_ENV_TMP");
@@ -504,7 +504,7 @@ TEST_F(TApp, Env) { @@ -504,7 +504,7 @@ TEST_F(TApp, Env) {
504 EXPECT_NO_THROW(run()); 504 EXPECT_NO_THROW(run());
505 505
506 app.reset(); 506 app.reset();
507 - unsetenv("CLI11_TEST_ENV_TMP"); 507 + unset_env("CLI11_TEST_ENV_TMP");
508 EXPECT_THROW(run(), CLI::RequiredError); 508 EXPECT_THROW(run(), CLI::RequiredError);
509 } 509 }
510 510
tests/app_helper.hpp
@@ -42,3 +42,20 @@ public: @@ -42,3 +42,20 @@ public:
42 operator const std::string& () const {return _name;} 42 operator const std::string& () const {return _name;}
43 const char* c_str() const {return _name.c_str();} 43 const char* c_str() const {return _name.c_str();}
44 }; 44 };
  45 +
  46 +inline void put_env(std::string name, std::string value) {
  47 +#ifdef _MSC_VER
  48 + _putenv_s(name.c_str(), value.c_str());
  49 +#else
  50 + setenv(name.c_str(), value.c_str(), 1);
  51 +#endif
  52 +}
  53 +
  54 +inline void unset_env(std::string name) {
  55 +#ifdef _MSC_VER
  56 + _putenv_s(name.c_str(), "");
  57 +#else
  58 + unsetenv(name.c_str());
  59 +#endif
  60 +}
  61 +