我在寫一個涉及boost :: program_options的C++程序,在這裏我遇到了一些問題。我的一些代碼在這裏給出。使用boost program_options處理幫助消息,刪除默認值或重新格式化幫助消息
int main(int argc, char* argv[]) {
options_description desc("useage: filterfq", options_description::m_default_line_length * 2, options_description::m_default_line_length);
options_description generic("Gerneric options", options_description::m_default_line_length * 2, options_description::m_default_line_length);
generic.add_options()
("help,h", "produce help message")
;
options_description param("Parameters", options_description::m_default_line_length * 2, options_description::m_default_line_length);
param.add_options()
("checkQualitySystem,c", bool_switch(), "only check quality system of the fastq file")
("baseNrate,N", value<float>() -> default_value(0.05), "maximum rate of \'N\' base allowed along a read")
("averageQuality,Q", value<float>() -> default_value(0), "minimum average quality allowed along a read")
("perBaseQuality,q", value<int>() -> default_value(5), "minimum quality per base allowed along a read")
("lowQualityRate,r", value<float>() -> default_value(0.5), "maximum low quality rate along a read")
("rawQualitySystem,s", value<int>(), "specify quality system of raw fastq\n0: Sanger\n1: Solexa\n2: Illumina 1.3+\n3: Illumina 1.5+\n4: Illumina 1.8+")
("preferSpecifiedRawQualitySystem,p", bool_switch(), "indicate that user prefers the given quality system to process")
;
options_description input("Input", options_description::m_default_line_length * 2, options_description::m_default_line_length);
input.add_options()
("rawFastq,f", value< vector<path> >() -> required() -> multitoken(), "raw fastq file(s) that need cleaned, required")
;
options_description output("Output", options_description::m_default_line_length * 2, options_description::m_default_line_length);
output.add_options()
("cleanQualitySystem,S", value<int>() -> default_value(4), "specify quality system of cleaned fastq, the same as rawQualitySystem")
("outDir,O", value<path>() -> default_value(current_path()), "specify output directory, not used if cleanFastq is specified")
("outBasename,o", value<string>(), "specify the basename for output file(s), required if outDir is specified")
("cleanFastq,F", value< vector<path> >() -> multitoken(), "cleaned fastq file name(s), not used if outDir or outBasename is specified")
("droppedFastq,D", value< vector<path> >() -> multitoken(), "fastq file(s) containing reads that are filtered out")
;
desc.add(generic).add(param).add(input).add(output);
variables_map vm;
store(command_line_parser(argc, argv).options(desc).run(), vm);
if (vm.count("help")) {
cout << desc << "\n";
return 0;
}
...
}
#include using namespace parts are not given here。當我輸入的命令來查看幫助信息,它表明我下面
useage: filterfq:
Gerneric options:
-h [ --help ] produce help message
Parameters:
-c [ --checkQualitySystem ] only check quality system of the fastq file
-N [ --baseNrate ] arg (=0.0500000007) maximum rate of 'N' base allowed along a read
-Q [ --averageQuality ] arg (=0) minimum average quality allowed along a read
-q [ --perBaseQuality ] arg (=5) minimum quality per base allowed along a read
-r [ --lowQualityRate ] arg (=0.5) maximum low quality rate along a read
-s [ --rawQualitySystem ] arg specify quality system of raw fastq
0: Sanger
1: Solexa
2: Illumina 1.3+
3: Illumina 1.5+
4: Illumina 1.8+
-p [ --preferSpecifiedRawQualitySystem ] indicate that user prefers the given quality system to process
Input:
-f [ --rawFastq ] arg raw fastq file(s) that need cleaned, required
Output:
-S [ --cleanQualitySystem ] arg (=4) specify quality system of cleaned fastq, the same as rawQualitySystem
-O [ --outDir ] arg (="/home/tanbowen/filterfq") specify output directory, not used if cleanFastq is specified
-o [ --outBasename ] arg specify the basename for output file(s), required if outDir is specified
-F [ --cleanFastq ] arg cleaned fastq file name(s), not used if outDir or outBasename is specified
-D [ --droppedFastq ] arg fastq file(s) containing reads that are filtered out
幫助消息看起來有點醜,尤其是「0.0500000007」,我想改善它。但我搜索了很長時間,我找不到解決方案。所以我在這裏尋求幫助解決以下問題:
- 有什麼方法可以重新格式化幫助信息嗎?
- 如果1不可能,如何刪除「arg」部分和默認值?
- 是否有任何方法來對齊右側的描述?
一個額外的問題:我如何才能防止被執行
filter -f <some file> -f <some file>
即不允許被多次指定相同的選項下面的命令?
非常感謝!
如果你program_options文檔中仔細搜尋它提到製作自定義類接收選項值。你爲這個類編寫operator <<和operator >>。然後你可以按照你的意願格式化它。 –