9

Với việc sử dụng sau đây đơn giản của Boost.Program_Options:Boost.Program_Options: Khi <bool> được chỉ định làm tùy chọn dòng lệnh, các tham số dòng lệnh hợp lệ là gì?

boost::program_options::options_description options("Options"); 

options.add_options() 

    ("my_bool_flag,b", boost::program_options::value<bool>(), "Sample boolean switch)") 

    ; 

... những gì đối số dòng lệnh sẽ đánh giá để false, và những gì để true?

(Ví dụ, giả sử chương trình được đặt tên là "foo", và thực hiện trên dòng lệnh như: foo -b ? ... với câu hỏi đánh dấu một giữ chỗ cho một số văn bản khác: tất cả các tùy chọn văn bản có thể là gì đó sẽ đúng cách đánh giá để false, và những gì để true)

Trả lời

19

Nhìn vào $ (BOOST_ROOT) /libs/program_options/src/value_semantic.cpp bạn có thể tìm thấy:?

/* Validates bool value. 
    Any of "1", "true", "yes", "on" will be converted to "1".<br> 
    Any of "0", "false", "no", "off" will be converted to "0".<br> 
    Case is ignored. The 'xs' vector can either be empty, in which 
    case the value is 'true', or can contain explicit value. 
*/ 
BOOST_PROGRAM_OPTIONS_DECL void validate(any& v, const vector<string>& xs, 
        bool*, int) 
{ 
    check_first_occurrence(v); 
    string s(get_single_string(xs, true)); 

    for (size_t i = 0; i < s.size(); ++i) 
     s[i] = char(tolower(s[i])); 

    if (s.empty() || s == "on" || s == "yes" || s == "1" || s == "true") 
     v = any(true); 
    else if (s == "off" || s == "no" || s == "0" || s == "false") 
     v = any(false); 
    else 
     boost::throw_exception(invalid_bool_value(s)); 
} 
Các vấn đề liên quan