1 // ***************************************************************************
2 // bamtools_options.cpp (c) 2010 Derek Barnett, Erik Garrison
3 // Marth Lab, Department of Biology, Boston College
4 // ---------------------------------------------------------------------------
5 // Last modified: 10 October 2011
6 // ---------------------------------------------------------------------------
7 // Parses command line arguments and creates a help menu
8 // ---------------------------------------------------------------------------
10 // The Mosaik suite's command line parser class: COptions
11 // (c) 2006 - 2009 Michael Str�mberg
12 // Marth Lab, Department of Biology, Boston College
13 // Re-licensed under MIT License with author's permission.
15 // * Modified slightly to fit BamTools, otherwise code is same.
16 // * (BamTools namespace, added stdin/stdout) (DB)
17 // ***************************************************************************
19 #include "utils/bamtools_options.h"
20 using namespace BamTools;
29 string Options::m_programName; // the program name
30 string Options::m_description; // the main description
31 string Options::m_exampleArguments; // the example arguments
32 vector<OptionGroup> Options::m_optionGroups; // stores the option groups
33 map<string, OptionValue> Options::m_optionsMap; // stores the options in a map
34 const string Options::m_stdin = "stdin"; // string representation of stdin
35 const string Options::m_stdout = "stdout"; // string representation of stdout
37 // adds a simple option to the parser
38 void Options::AddOption(const string& argument,
39 const string& optionDescription,
44 o.Argument = argument;
45 o.Description = optionDescription;
47 group->Options.push_back(o);
50 ov.pFoundArgument = &foundArgument;
51 ov.StoreValue = false;
53 m_optionsMap[argument] = ov;
56 // creates an option group
57 OptionGroup* Options::CreateOptionGroup(const string& groupName) {
60 m_optionGroups.push_back(og);
61 return &m_optionGroups[m_optionGroups.size() - 1];
64 // displays the help menu
65 void Options::DisplayHelp(void) {
68 char argumentBuffer[ARGUMENT_LENGTH + 1];
71 char indentBuffer[MAX_LINE_LENGTH - DESC_LENGTH + 1];
72 memset(indentBuffer, ' ', MAX_LINE_LENGTH - DESC_LENGTH);
73 indentBuffer[MAX_LINE_LENGTH - DESC_LENGTH] = 0;
76 printf("Description: %s.\n\n", m_description.c_str());
78 printf("%s", m_programName.c_str());
79 printf(" %s\n\n", m_exampleArguments.c_str());
81 vector<Option>::const_iterator optionIter;
82 vector<OptionGroup>::const_iterator groupIter;
83 for (groupIter = m_optionGroups.begin(); groupIter != m_optionGroups.end(); ++groupIter) {
85 printf("%s:\n", groupIter->Name.c_str());
87 for (optionIter = groupIter->Options.begin(); optionIter != groupIter->Options.end(); ++optionIter) {
89 if (optionIter->StoreValue)
90 snprintf(argumentBuffer, ARGUMENT_LENGTH + 1, " %s <%s>", optionIter->Argument.c_str(), optionIter->ValueDescription.c_str());
92 snprintf(argumentBuffer, ARGUMENT_LENGTH + 1, " %s", optionIter->Argument.c_str());
93 printf("%-35s ", argumentBuffer);
95 string description = optionIter->Description;
97 // handle default values
98 if (optionIter->HasDefaultValue) {
101 sb << description << " [";
103 if (optionIter->DefaultValue.is_type<unsigned int>()) {
104 sb << (unsigned int)optionIter->DefaultValue;
105 } else if (optionIter->DefaultValue.is_type<unsigned char>()) {
106 sb << (unsigned short)(unsigned char)optionIter->DefaultValue;
107 } else if (optionIter->DefaultValue.is_type<float>()) {
108 sb << std::fixed << std::setprecision(2) << (float)optionIter->DefaultValue;
109 } else if (optionIter->DefaultValue.is_type<double>()) {
110 sb << std::fixed << std::setprecision(4) << (double)optionIter->DefaultValue;
111 } else if (optionIter->DefaultValue.is_type<std::string>()) {
112 const std::string stringValue = optionIter->DefaultValue;
115 printf("ERROR: Found an unsupported data type for argument %s when casting the default value.\n",
116 optionIter->Argument.c_str());
121 description = sb.str();
124 if ( description.size() <= DESC_LENGTH_FIRST_ROW ) {
125 printf("%s\n", description.c_str());
128 // handle the first row
129 const char* pDescription = description.data();
130 unsigned int cutIndex = DESC_LENGTH_FIRST_ROW;
131 while(pDescription[cutIndex] != ' ')
133 printf("%s\n", description.substr(0, cutIndex).c_str());
134 description = description.substr(cutIndex + 1);
136 // handle subsequent rows
137 while(description.size() > DESC_LENGTH) {
138 pDescription = description.data();
139 cutIndex = DESC_LENGTH;
140 while(pDescription[cutIndex] != ' ')
142 printf("%s%s\n", indentBuffer, description.substr(0, cutIndex).c_str());
143 description = description.substr(cutIndex + 1);
147 printf("%s%s\n", indentBuffer, description.c_str());
155 printf(" --help, -h shows this help text\n");
159 // parses the command line
160 void Options::Parse(int argc, char* argv[], int offset) {
163 map<string, OptionValue>::const_iterator ovMapIter;
164 map<string, OptionValue>::const_iterator checkMapIter;
165 const int LAST_INDEX = argc - 1;
166 ostringstream errorBuilder;
167 bool foundError = false;
168 char* end_ptr = NULL;
169 const string ERROR_SPACER(7, ' ');
171 // check if we should show the help menu
172 bool showHelpMenu = false;
174 for (int i = 1; i < argc; i++) {
175 const std::string argument = argv[i];
176 if ( (argument == "-h") || (argument == "--help") || (argument == "help") )
179 } else showHelpMenu = true;
184 // check each argument
185 for (int i = offset+1; i < argc; i++) {
187 const string argument = argv[i];
188 ovMapIter = m_optionsMap.find(argument);
190 if (ovMapIter == m_optionsMap.end()) {
191 errorBuilder << ERROR_SPACER << "An unrecognized argument was found: " << argument << std::endl;
195 *ovMapIter->second.pFoundArgument = true;
198 if (ovMapIter->second.StoreValue) {
200 if (i < LAST_INDEX) {
202 // check if the next argument is really a command line option
203 const string val = argv[i + 1];
204 checkMapIter = m_optionsMap.find(val);
206 if (checkMapIter == m_optionsMap.end()) {
210 if (ovMapIter->second.VariantValue.is_type<unsigned int>()) {
211 const unsigned int uint32 = (unsigned int)strtoul(val.c_str(), &end_ptr, 10);
212 unsigned int* varValue = (unsigned int*)ovMapIter->second.pValue;
214 } else if (ovMapIter->second.VariantValue.is_type<unsigned char>()) {
215 const unsigned char uint8 = (unsigned char)strtoul(val.c_str(), &end_ptr, 10);
216 unsigned char* varValue = (unsigned char*)ovMapIter->second.pValue;
218 } else if (ovMapIter->second.VariantValue.is_type<uint64_t>()) {
219 const uint64_t uint64 = strtoui64(val.c_str(), &end_ptr, 10);
220 uint64_t* varValue = (uint64_t*)ovMapIter->second.pValue;
222 } else if (ovMapIter->second.VariantValue.is_type<double>()) {
223 const double d = strtod(val.c_str(), &end_ptr);
224 double* varValue = (double*)ovMapIter->second.pValue;
226 } else if (ovMapIter->second.VariantValue.is_type<float>()) {
227 const float f = (float)strtod(val.c_str(), &end_ptr);
228 float* varValue = (float*)ovMapIter->second.pValue;
230 } else if (ovMapIter->second.VariantValue.is_type<string>()) {
231 string* pStringValue = (string*)ovMapIter->second.pValue;
233 } else if (ovMapIter->second.VariantValue.is_type<vector<string> >()) {
234 vector<string>* pVectorValue = (vector<string>*)ovMapIter->second.pValue;
235 pVectorValue->push_back(val);
237 printf("ERROR: Found an unsupported data type for argument %s when parsing the arguments.\n",
242 errorBuilder << ERROR_SPACER << "The argument (" << argument
243 << ") expects a value, but none was found." << endl;
247 errorBuilder << ERROR_SPACER << "The argument (" << argument
248 << ") expects a value, but none was found." << endl;
255 // check if we missed any required parameters
256 for (ovMapIter = m_optionsMap.begin(); ovMapIter != m_optionsMap.end(); ++ovMapIter) {
257 if (ovMapIter->second.IsRequired && !*ovMapIter->second.pFoundArgument) {
258 errorBuilder << ERROR_SPACER << ovMapIter->second.ValueTypeDescription
259 << " was not specified. Please use the " << ovMapIter->first << " parameter." << endl;
264 // print the errors if any were found
266 printf("ERROR: Some problems were encountered when parsing the command line options:\n");
267 printf("%s\n", errorBuilder.str().c_str());
268 printf("For a complete list of command line options, type \"%s help %s\"\n", argv[0], argv[1]);
273 // sets the program info
274 void Options::SetProgramInfo(const string& programName,
275 const string& description,
276 const string& arguments)
278 m_programName = programName;
279 m_description = description;
280 m_exampleArguments = arguments;
283 // return string representations of stdin
284 const string& Options::StandardIn(void) { return m_stdin; }
286 // return string representations of stdout
287 const string& Options::StandardOut(void) { return m_stdout; }