Problem
How do I write a command help in C++?
Solution
It is preferable to use class that comes from ACE library, ACE_Get_Opt: http://www.dre.vanderbilt.edu/Doxygen/Current/html/ace/classACE__Get__Opt.html
Here there is a brief example of use:
#include <ARGV.h>
#include <ace/Arg_Shifter.h>
#include <ace/Get_Opt.h>
....
ACE_Get_Opt get_opts (argc, &argv[1], "db:");
ACE_CString m_b_arg_string("");
int m_a_arg_int = -1;
// the third parameter can be ACE_Get_Opt::ARG_REQUIRED, ACE_Get_Opt::ARG_OPTIONAL or without 3rd argument for a no argument.
if (get_opts.long_option(ACE_TEXT ("a_long_option"), 'a', ACE_Get_Opt::ARG_REQUIRED) == -1)
return -1;
if (get_opts.long_option(ACE_TEXT ("another_long_option"), 88) == -1)
return -1;
if (get_opts.long_option(ACE_TEXT ("help"), h) == -1)
return -1;
int c;
while ((c = get_opts ()) != -1)
switch (c)
{
case 'd':
//do something
break;
case 'b':
m_b_arg_string = ACE_CString(get_opts.optarg);
//do something else
break;
case 'a':
//always check if the value is numeric if that is needed
if(isNumeric(get_opts.optarg))
m_a_arg_int = atoi(get_opts.optarg);
else
showUsage(argc, argv);
break;
case 88:
//do something
break;
case '?':
//this case informs when the option, that needs an argument, doesn't have it.
ACS_SHORT_LOG((LM_INFO, MyProgram::main: Option -%c requires an argument!", get_opts.opt_opt()));
return -1;
case 'h':
default:
showUsage(argc, argv);
}
-- CarlitaParedes - 27 Jun 2006