38 namespace ArgusSamples
45 PROPAGATE_ERROR(options->
usage());
61 : m_initialized(false)
62 , m_requestExit(false)
63 , m_programName(programName)
81 "display this help and exit",
printHelp,
this)));
85 "exit from the program",
exit,
this)));
96 Option optionCopy = option;
111 for (
size_t i = 0; i < count; ++i)
113 PROPAGATE_ERROR(
addOption(options[i], userPtr));
139 std::string optString;
140 std::vector<option> options;
141 std::vector<Option>::iterator it;
150 memset(&getoptOption, 0,
sizeof(getoptOption));
152 getoptOption.name = it->m_name.c_str();
154 getoptOption.has_arg = no_argument;
156 getoptOption.has_arg = optional_argument;
158 getoptOption.has_arg = required_argument;
160 ORIGINATE_ERROR(
"Unhandled flag");
161 getoptOption.flag = NULL;
162 getoptOption.val = it->m_shortName;
164 options.push_back(getoptOption);
168 optString += it->m_shortName;
178 memset(&lastElement, 0,
sizeof(lastElement));
179 options.push_back(lastElement);
184 int c = getopt_long(argc, argv, optString.c_str(), options.data(), &optionIndex);
191 if (((c == 0) && (it->m_name == options[optionIndex].name)) ||
192 ((c != 0) && (it->m_shortName == c)))
194 PROPAGATE_ERROR(it->m_function(it->m_userPtr, optarg));
201 PROPAGATE_ERROR(
usage());
202 ORIGINATE_ERROR(
"Error parsing command line");
219 const size_t lineLength = 80;
220 const size_t optionLength = 24;
221 const size_t helpTextLength = lineLength - optionLength;
222 std::ostringstream
usage;
224 usage <<
"Usage: " <<
m_programName <<
" [OPTION]... [ACTION]... \n";
226 usage <<
"Options are set and actions are executed in the order they occur. Multiple\n";
227 usage <<
"actions can be executed.\n";
228 usage <<
"Mandatory arguments to long options are mandatory for short options too.\n";
230 for (uint32_t typeIndex = 0; typeIndex <
sizeof(types) /
sizeof(types[0]); ++typeIndex)
235 usage <<
"\nOptions:\n";
237 usage <<
"\nActions:\n";
239 ORIGINATE_ERROR(
"Internal error, unhandled type\n");
241 for (std::vector<Option>::iterator it =
m_options.begin(); it <
m_options.end(); ++it)
243 if (it->m_type == type)
245 std::ostringstream optionUsage;
249 optionUsage <<
" -" << (char)it->m_shortName <<
", ";
254 optionUsage <<
"--" << it->m_name;
257 ORIGINATE_ERROR(
"Internal error, argument string required\n");
260 optionUsage <<
"=" << it->m_argument <<
"";
262 optionUsage <<
"[=" << it->m_argument <<
"]";
265 if (optionUsage.str().length() >= optionLength)
267 optionUsage << std::endl;
268 optionUsage << std::string(optionLength,
' ');
273 optionUsage << std::string(optionLength - optionUsage.str().length(),
' ');
275 usage << optionUsage.str();
278 std::string helpText(it->m_usage);
281 while (helpText.length() > helpTextLength)
284 size_t lastSpace = helpText.find_last_of(
' ', helpTextLength);
287 usage << helpText.substr(0, lastSpace) << std::endl <<
288 std::string(optionLength,
' ');
290 helpText = helpText.substr(lastSpace + 1, std::string::npos);
293 usage << helpText << std::endl;
298 printf(
"%s", usage.str().c_str());