Nếu bạn đang sử dụng trình phân tích cú pháp dòng lệnh, có một cách để đặt các kiểu khác nhau. Vì vậy, giải pháp sẽ chỉ sử dụng các tùy chọn dài và bật kiểu allow_long_disguise cho phép tùy chọn dài được chỉ định bằng một dấu gạch ngang (tức là "-long_option"). Dưới đây là một ví dụ:
#include <iostream>
#include <boost/program_options.hpp>
namespace options = boost::program_options;
using namespace std;
int
main (int argc, char *argv[])
{
options::options_description desc (string (argv[0]).append(" options"));
desc.add_options()
("h", "Display this message")
;
options::variables_map args;
options::store (options::command_line_parser (argc, argv).options (desc)
.style (options::command_line_style::default_style |
options::command_line_style::allow_long_disguise)
.run(), args);
options::notify (args);
if (args.count ("h"))
{
cout << desc << endl;
return 0;
}
}
Sẽ có một vấn đề nhỏ với sản lượng mô tả mặc dù:
$ ./test --h
./test options:
--h Display this message
Và đó là một trong những khó khăn để sửa chữa bởi vì đây là những gì đang được sử dụng để tạo ra này:
std::string
option_description::format_name() const
{
if (!m_short_name.empty())
return string(m_short_name).append(" [ --").
append(m_long_name).append(" ]");
else
return string("--").append(m_long_name);
}
Sửa lỗi duy nhất cho điều này đến với tâm trí là thay thế "-" bằng "-" trong chuỗi kết quả. Ví dụ:
#include <iostream>
#include <sstream>
#include <boost/program_options.hpp>
#include <boost/algorithm/string/replace.hpp>
namespace options = boost::program_options;
using namespace std;
int
main (int argc, char *argv[])
{
options::options_description desc (string (argv[0]).append(" options"));
desc.add_options()
("h", "Display this message")
;
options::variables_map args;
options::store (options::command_line_parser (argc, argv).options (desc)
.style (options::command_line_style::default_style |
options::command_line_style::allow_long_disguise)
.run(), args);
options::notify (args);
if (args.count ("h"))
{
std::stringstream stream;
stream << desc;
string helpMsg = stream.str();
boost::algorithm::replace_all (helpMsg, "--", "-");
cout << helpMsg << endl;
return 0;
}
}
Điều tốt nhất bạn có thể làm là sửa mã nơi in mô tả tùy chọn dài trống và gửi bản vá cho tác giả của thư viện.
Nguồn
2010-09-01 20:32:49
bạn muốn văn bản trợ giúp chỉ tắt tùy chọn ngắn? –
Không, tôi chỉ muốn các tùy chọn như -v thay vì --verbose hoặc xác định cả hai. Chỉ định ", v" thay vì "v" thực hiện thủ thuật nhưng làm xấu danh sách in của mô tả. – Novikov