Ticket #2982: patch_ticket2982.diff
File patch_ticket2982.diff, 5.5 KB (added by , 13 years ago) |
---|
-
boost/program_options/errors.hpp
38 38 std::string tokens, msg; 39 39 }; 40 40 41 /** Class thrown when a required/mandatory option is missing */ 42 class BOOST_PROGRAM_OPTIONS_DECL required_option : public error { 43 public: 44 required_option(const std::string& name) 45 : error(std::string("missing required option ").append(name)) 46 {} 47 }; 48 41 49 /** Class thrown when option name is not recognized. */ 42 50 class BOOST_PROGRAM_OPTIONS_DECL unknown_option : public error { 43 51 public: -
boost/program_options/value_semantic.hpp
44 44 */ 45 45 virtual bool is_composing() const = 0; 46 46 47 /** Returns true if value must be given. Non-optional value 48 49 */ 50 virtual bool is_required() const = 0; 51 47 52 /** Parses a group of tokens that specify a value of option. 48 53 Stores the result in 'value_store', using whatever representation 49 54 is desired. May be be called several times if value of the same … … 132 137 133 138 bool is_composing() const { return false; } 134 139 140 bool is_required() const { return false; } 141 135 142 /** If 'value_store' is already initialized, or new_tokens 136 143 has more than one elements, throws. Otherwise, assigns 137 144 the first string from 'new_tokens' to 'value_store', without … … 177 184 the value when it's known. The parameter can be NULL. */ 178 185 typed_value(T* store_to) 179 186 : m_store_to(store_to), m_composing(false), 180 m_multitoken(false), m_zero_tokens(false) 187 m_multitoken(false), m_zero_tokens(false), 188 m_required(false) 181 189 {} 182 190 183 191 /** Specifies default value, which will be used … … 265 273 m_zero_tokens = true; 266 274 return this; 267 275 } 276 277 typed_value* required() 278 { 279 m_required = true; 280 return this; 281 } 268 282 269 283 270 284 public: // value semantic overrides … … 292 306 } 293 307 } 294 308 309 bool is_required() const { return m_required; } 295 310 311 296 312 /** Creates an instance of the 'validator' class and calls 297 313 its operator() to perform the actual conversion. */ 298 314 void xparse(boost::any& value_store, … … 335 351 std::string m_default_value_as_text; 336 352 boost::any m_implicit_value; 337 353 std::string m_implicit_value_as_text; 338 bool m_composing, m_implicit, m_multitoken, m_zero_tokens ;354 bool m_composing, m_implicit, m_multitoken, m_zero_tokens, m_required; 339 355 boost::function1<void, const T&> m_notifier; 340 356 }; 341 357 -
libs/program_options/src/value_semantic.cpp
171 171 check_first_occurrence(v); 172 172 string s(get_single_string(xs)); 173 173 if (!s.empty() && ( 174 ( *s.begin() == '\'' && *s.rbegin() == '\''||175 *s.begin() == '"' && *s.rbegin() == '"')))174 ((*s.begin() == '\'' && *s.rbegin() == '\'') || 175 (*s.begin() == '"' && *s.rbegin() == '"')))) 176 176 { 177 177 v = any(s.substr(1, s.size()-2)); 178 178 } 179 179 else 180 { 180 181 v = any(s); 182 } 181 183 } 182 184 183 185 #if !defined(BOOST_NO_STD_WSTRING) … … 186 188 { 187 189 check_first_occurrence(v); 188 190 wstring s(get_single_string(xs)); 189 if ( *s.begin() == L'\'' && *s.rbegin() == L'\''||190 *s.begin() == L'"' && *s.rbegin() == L'"')191 if ((*s.begin() == L'\'' && *s.rbegin() == L'\'') || 192 (*s.begin() == L'"' && *s.rbegin() == L'"')) 191 193 v = any(s.substr(1, s.size()-2)); 192 194 else 193 195 v = any(s); -
libs/program_options/src/cmdline.cpp
309 309 } 310 310 } 311 311 result.swap(result2); 312 313 312 313 // This checks if all required options occur 314 const std::vector< shared_ptr<option_description> >& 315 all_options = m_desc->options(); 316 for (unsigned i = 0; i < all_options.size(); ++i) { 317 if (all_options[i]->semantic()->is_required()) { 318 bool found_required = false; 319 for (unsigned j = 0; !found_required && j < result.size(); ++j) { 320 if (all_options[i]->key(result[j].string_key) == result[j].string_key) { // REVISIT, is this compare ok? 321 found_required = true; 322 } 323 } 324 if (!found_required) { 325 boost::throw_exception(required_option( 326 all_options[i]->format_name())); 327 } 328 } 329 } 330 314 331 // Assign position keys to positional options. 315 332 int position_key = 0; 316 333 for(unsigned i = 0; i < result.size(); ++i) {