2012-05-29 13:31:11 +00:00
|
|
|
// NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
|
|
|
|
// Copyright (C) 2010 Winch Gate Property Limited
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as
|
|
|
|
// published by the Free Software Foundation, either version 3 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
//
|
|
|
|
// Includes
|
|
|
|
//
|
|
|
|
#include "stdmisc.h"
|
|
|
|
#include "nel/misc/cmd_args.h"
|
|
|
|
|
2016-01-14 20:26:10 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2012-12-05 09:26:01 +00:00
|
|
|
#ifdef DEBUG_NEW
|
|
|
|
#define new DEBUG_NEW
|
|
|
|
#endif
|
|
|
|
|
2012-05-29 13:31:11 +00:00
|
|
|
namespace NLMISC
|
|
|
|
{
|
|
|
|
|
2016-01-14 20:26:10 +00:00
|
|
|
CCmdArgs::CCmdArgs()
|
|
|
|
{
|
2016-01-18 20:22:57 +00:00
|
|
|
#ifdef NL_VERSION
|
|
|
|
_Version = NL_VERSION;
|
|
|
|
#endif
|
|
|
|
|
2016-01-14 20:26:10 +00:00
|
|
|
// add help
|
|
|
|
addArg("h", "help", "", "Display this help");
|
|
|
|
|
|
|
|
// add version
|
|
|
|
addArg("v", "version", "", "Display version of this program");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCmdArgs::addArg(const TArg &arg)
|
|
|
|
{
|
|
|
|
_Args.push_back(arg);
|
|
|
|
}
|
|
|
|
|
2016-01-18 20:22:57 +00:00
|
|
|
void CCmdArgs::addArg(const std::string &shortName, const std::string &longName, const std::string &helpName, const std::string &helpDescription, bool onlyOnce)
|
2012-05-29 13:31:11 +00:00
|
|
|
{
|
2016-01-14 20:26:10 +00:00
|
|
|
TArg arg;
|
|
|
|
arg.shortName = shortName;
|
|
|
|
arg.longName = longName;
|
|
|
|
arg.helpName = helpName;
|
|
|
|
arg.helpDescription = helpDescription;
|
2016-01-18 20:22:57 +00:00
|
|
|
arg.onlyOnce = onlyOnce;
|
2016-01-14 20:26:10 +00:00
|
|
|
arg.found = false;
|
2016-01-18 20:22:57 +00:00
|
|
|
arg.required = false;
|
2016-01-14 20:26:10 +00:00
|
|
|
|
|
|
|
addArg(arg);
|
|
|
|
}
|
|
|
|
|
2016-01-18 20:22:57 +00:00
|
|
|
void CCmdArgs::addAdditionalArg(const std::string &helpName, const std::string &helpDescription, bool onlyOnce, bool required)
|
2016-01-14 20:26:10 +00:00
|
|
|
{
|
|
|
|
TArg arg;
|
|
|
|
arg.helpName = helpName;
|
|
|
|
arg.helpDescription = helpDescription;
|
2016-01-18 20:22:57 +00:00
|
|
|
arg.onlyOnce = onlyOnce;
|
2016-01-14 20:26:10 +00:00
|
|
|
arg.found = false;
|
2016-01-18 20:22:57 +00:00
|
|
|
arg.required = required;
|
2016-01-14 20:26:10 +00:00
|
|
|
|
|
|
|
addArg(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCmdArgs::haveArg(const std::string &argName) const
|
|
|
|
{
|
|
|
|
// process each argument
|
2016-01-16 22:48:44 +00:00
|
|
|
for(uint i = 0; i < _Args.size(); ++i)
|
2012-05-29 13:31:11 +00:00
|
|
|
{
|
2016-01-14 20:26:10 +00:00
|
|
|
const TArg &arg = _Args[i];
|
|
|
|
|
|
|
|
// return true if long arg found
|
|
|
|
if (arg.shortName == argName) return arg.found;
|
2012-05-29 13:31:11 +00:00
|
|
|
}
|
2016-01-14 20:26:10 +00:00
|
|
|
|
2012-05-29 13:31:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-14 20:26:10 +00:00
|
|
|
std::vector<std::string> CCmdArgs::getArg(const std::string &argName) const
|
2012-05-29 13:31:11 +00:00
|
|
|
{
|
2016-01-14 20:26:10 +00:00
|
|
|
// process each argument
|
|
|
|
for(uint i = 0; i < _Args.size(); ++i)
|
2012-05-29 13:31:11 +00:00
|
|
|
{
|
2016-01-14 20:26:10 +00:00
|
|
|
const TArg &arg = _Args[i];
|
|
|
|
|
|
|
|
// return values if short arg found
|
|
|
|
if (arg.shortName == argName && arg.found) return arg.values;
|
2012-05-29 13:31:11 +00:00
|
|
|
}
|
2016-01-14 20:26:10 +00:00
|
|
|
|
|
|
|
// return an empty vector
|
|
|
|
return std::vector<std::string>();
|
2012-05-29 13:31:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 20:26:10 +00:00
|
|
|
bool CCmdArgs::haveLongArg(const std::string &argName) const
|
2012-05-29 13:31:11 +00:00
|
|
|
{
|
2016-01-14 20:26:10 +00:00
|
|
|
// process each argument
|
|
|
|
for(uint i = 0; i < _Args.size(); ++i)
|
2012-05-29 13:31:11 +00:00
|
|
|
{
|
2016-01-14 20:26:10 +00:00
|
|
|
const TArg &arg = _Args[i];
|
|
|
|
|
|
|
|
// return true if long arg found
|
|
|
|
if (arg.longName == argName) return arg.found;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> CCmdArgs::getLongArg(const std::string &argName) const
|
|
|
|
{
|
|
|
|
// process each argument
|
|
|
|
for(uint i = 0; i < _Args.size(); ++i)
|
|
|
|
{
|
|
|
|
const TArg &arg = _Args[i];
|
|
|
|
|
|
|
|
// return values if long arg found
|
|
|
|
if (arg.longName == argName && arg.found) return arg.values;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return an empty vector
|
|
|
|
return std::vector<std::string>();
|
|
|
|
}
|
|
|
|
|
2016-01-18 20:22:57 +00:00
|
|
|
bool CCmdArgs::needAdditionalArg() const
|
2016-01-14 20:26:10 +00:00
|
|
|
{
|
|
|
|
// process each argument
|
|
|
|
for(uint i = 0; i < _Args.size(); ++i)
|
|
|
|
{
|
|
|
|
const TArg &arg = _Args[i];
|
|
|
|
|
|
|
|
// they don't have any short or long name, but need a name in help
|
2016-01-18 20:22:57 +00:00
|
|
|
if (arg.shortName.empty() && arg.longName.empty() && !arg.helpName.empty() && arg.required)
|
2012-05-29 13:31:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-01-14 20:26:10 +00:00
|
|
|
|
2012-05-29 13:31:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-18 20:22:57 +00:00
|
|
|
bool CCmdArgs::haveAdditionalArg() const
|
2012-05-29 13:31:11 +00:00
|
|
|
{
|
2016-01-14 20:26:10 +00:00
|
|
|
// process each argument
|
|
|
|
for(uint i = 0; i < _Args.size(); ++i)
|
|
|
|
{
|
|
|
|
const TArg &arg = _Args[i];
|
2012-05-29 13:31:11 +00:00
|
|
|
|
2016-01-14 20:26:10 +00:00
|
|
|
// they don't have any short or long name, but need a name in help
|
2016-01-18 20:22:57 +00:00
|
|
|
if (arg.shortName.empty() && arg.longName.empty() && !arg.helpName.empty() && arg.found)
|
2016-01-23 11:28:22 +00:00
|
|
|
return true;
|
2012-05-29 13:31:11 +00:00
|
|
|
}
|
2016-01-14 20:26:10 +00:00
|
|
|
|
|
|
|
return false;
|
2012-05-29 13:31:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-18 20:22:57 +00:00
|
|
|
bool CCmdArgs::haveAdditionalArg(const std::string &name) const
|
2012-05-29 13:31:11 +00:00
|
|
|
{
|
2016-01-14 20:26:10 +00:00
|
|
|
// process each argument
|
|
|
|
for(uint i = 0; i < _Args.size(); ++i)
|
|
|
|
{
|
|
|
|
const TArg &arg = _Args[i];
|
|
|
|
|
|
|
|
// they don't have any short or long name, but need a name in help
|
2016-01-18 20:22:57 +00:00
|
|
|
if (arg.shortName.empty() && arg.longName.empty() && !arg.helpName.empty() && arg.helpName == name && arg.found)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> CCmdArgs::getAdditionalArg(const std::string &name) const
|
|
|
|
{
|
|
|
|
// process each argument
|
|
|
|
for(uint i = 0; i < _Args.size(); ++i)
|
|
|
|
{
|
|
|
|
const TArg &arg = _Args[i];
|
|
|
|
|
|
|
|
// they don't have any short or long name, but need a name in help
|
|
|
|
if (arg.shortName.empty() && arg.longName.empty() && !arg.helpName.empty() && arg.helpName == name)
|
2016-01-14 20:26:10 +00:00
|
|
|
return arg.values;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return an empty vector
|
|
|
|
return std::vector<std::string>();
|
|
|
|
}
|
2012-05-29 13:31:11 +00:00
|
|
|
|
2016-01-14 20:26:10 +00:00
|
|
|
bool CCmdArgs::parse(const std::string &args)
|
|
|
|
{
|
|
|
|
std::vector<std::string> argv;
|
2016-01-18 20:22:57 +00:00
|
|
|
|
|
|
|
#ifdef NL_OS_WINDOWS
|
|
|
|
char str[4096];
|
|
|
|
uint len = GetModuleFileNameA(NULL, str, 4096);
|
|
|
|
|
|
|
|
if (len && len < 4096)
|
|
|
|
argv.push_back(str);
|
|
|
|
#endif
|
|
|
|
|
2012-05-29 13:31:11 +00:00
|
|
|
std::string::size_type pos1 = 0, pos2 = 0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// Look for the first non space character
|
2016-01-14 20:26:10 +00:00
|
|
|
pos1 = args.find_first_not_of (" ", pos2);
|
2012-05-29 13:31:11 +00:00
|
|
|
if(pos1 == std::string::npos) break;
|
|
|
|
|
|
|
|
// Look for the first space or "
|
2016-01-14 20:26:10 +00:00
|
|
|
pos2 = args.find_first_of (" \"", pos1);
|
2012-05-29 13:31:11 +00:00
|
|
|
if(pos2 != std::string::npos)
|
|
|
|
{
|
|
|
|
// " ?
|
2016-01-14 20:26:10 +00:00
|
|
|
if(args[pos2] == '"')
|
2012-05-29 13:31:11 +00:00
|
|
|
{
|
|
|
|
// Look for the final \"
|
2016-01-14 20:26:10 +00:00
|
|
|
pos2 = args.find_first_of ("\"", pos2+1);
|
2012-05-29 13:31:11 +00:00
|
|
|
if(pos2 != std::string::npos)
|
|
|
|
{
|
|
|
|
// Look for the first space
|
2016-01-14 20:26:10 +00:00
|
|
|
pos2 = args.find_first_of (" ", pos2+1);
|
2012-05-29 13:31:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the size of the string to extract
|
|
|
|
std::string::difference_type length = (pos2 != std::string::npos) ? pos2-pos1 : std::string::npos;
|
|
|
|
|
2016-01-14 20:26:10 +00:00
|
|
|
std::string tmp = args.substr (pos1, length);
|
|
|
|
argv.push_back (tmp);
|
2012-05-29 13:31:11 +00:00
|
|
|
}
|
|
|
|
while(pos2 != std::string::npos);
|
2016-01-14 20:26:10 +00:00
|
|
|
|
|
|
|
return parse(argv);
|
2012-05-29 13:31:11 +00:00
|
|
|
}
|
|
|
|
|
2016-01-14 20:26:10 +00:00
|
|
|
bool CCmdArgs::parse(int argc, char **argv)
|
2012-05-29 13:31:11 +00:00
|
|
|
{
|
2016-01-14 20:26:10 +00:00
|
|
|
// convert C strings to STL strings
|
|
|
|
std::vector<std::string> args;
|
|
|
|
|
|
|
|
for(sint i = 0; i < argc; ++i)
|
2012-05-29 13:31:11 +00:00
|
|
|
{
|
2016-01-14 20:26:10 +00:00
|
|
|
args.push_back(argv[i]);
|
2012-05-29 13:31:11 +00:00
|
|
|
}
|
2016-01-14 20:26:10 +00:00
|
|
|
|
|
|
|
return parse(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCmdArgs::parse(const std::vector<std::string> &argv)
|
|
|
|
{
|
|
|
|
// no parameters
|
|
|
|
if (argv.empty()) return false;
|
|
|
|
|
|
|
|
// first argument is always the program name
|
|
|
|
_ProgramName = CFile::getFilename(argv.front());
|
2016-01-18 20:22:57 +00:00
|
|
|
_ProgramPath = CPath::standardizePath(CFile::getPath(argv.front()));
|
2016-01-14 20:26:10 +00:00
|
|
|
|
|
|
|
// arguments count
|
|
|
|
uint argc = argv.size();
|
|
|
|
|
|
|
|
// process each argument
|
|
|
|
for (sint i = 1; i < argc; i++)
|
|
|
|
{
|
|
|
|
std::string name = argv[i];
|
|
|
|
|
|
|
|
#ifdef NL_OS_WINDOWS
|
|
|
|
// support / and - under Windows, arguments should be at least 2 characters
|
|
|
|
if (name.size() > 1 && (name[0] == '-' || name[0] == '/'))
|
|
|
|
#else
|
|
|
|
if (name.size() > 1 && name[0] == '-')
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
// it's a long name if using --
|
|
|
|
bool useLongName = name[0] == '-' && name[1] == '-';
|
|
|
|
|
|
|
|
// extract argument name
|
|
|
|
name = name.substr(useLongName ? 2:1);
|
|
|
|
|
|
|
|
std::string value;
|
|
|
|
|
|
|
|
if (useLongName)
|
|
|
|
{
|
|
|
|
// look if using = to define value
|
|
|
|
std::string::size_type pos = name.find('=');
|
|
|
|
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
// value is second part, name the first one
|
|
|
|
value = name.substr(pos+1);
|
|
|
|
name = name.substr(0, pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (name.length() > 1)
|
|
|
|
{
|
|
|
|
value = name.substr(1);
|
|
|
|
name = name.substr(0, 1);
|
|
|
|
}
|
|
|
|
|
2016-01-18 20:22:57 +00:00
|
|
|
bool found = false;
|
|
|
|
|
2016-01-14 20:26:10 +00:00
|
|
|
// process each argument definition
|
|
|
|
for(uint j = 0; j < _Args.size(); ++j)
|
|
|
|
{
|
|
|
|
TArg &arg = _Args[j];
|
|
|
|
|
|
|
|
// only process arguments of the right type
|
|
|
|
if ((useLongName && name != arg.longName) || (!useLongName && name != arg.shortName)) continue;
|
|
|
|
|
2016-01-18 20:22:57 +00:00
|
|
|
// already get the only once argument
|
|
|
|
if (arg.found && arg.onlyOnce)
|
|
|
|
{
|
|
|
|
// the last one is the only kept, so discard previous ones
|
|
|
|
arg.values.clear();
|
|
|
|
}
|
|
|
|
|
2016-01-14 20:26:10 +00:00
|
|
|
// argument is found
|
2016-01-18 20:22:57 +00:00
|
|
|
found = arg.found = true;
|
2016-01-14 20:26:10 +00:00
|
|
|
|
|
|
|
// another argument is required
|
|
|
|
if (!arg.helpName.empty())
|
|
|
|
{
|
|
|
|
// if the value hasn't be specified by =
|
|
|
|
if (value.empty() && i+1 < argc)
|
|
|
|
{
|
|
|
|
// take next argument
|
|
|
|
value = argv[++i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// add argument value if not empty
|
|
|
|
if (!value.empty())
|
|
|
|
{
|
|
|
|
arg.values.push_back(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2016-01-18 20:22:57 +00:00
|
|
|
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
printf("Warning: Argument %s not recognized, skip it!\n", name.c_str());
|
|
|
|
}
|
2016-01-14 20:26:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// process each argument definition
|
|
|
|
for(uint j = 0, len = _Args.size(); j < len; ++j)
|
|
|
|
{
|
|
|
|
TArg &arg = _Args[j];
|
|
|
|
|
|
|
|
// only process arguments that don't have a name
|
|
|
|
if (!arg.shortName.empty() || !arg.longName.empty()) continue;
|
|
|
|
|
2016-01-18 20:22:57 +00:00
|
|
|
// already get the only once argument
|
|
|
|
if (arg.found && arg.onlyOnce) continue;
|
|
|
|
|
|
|
|
arg.found = true;
|
|
|
|
|
2016-01-14 20:26:10 +00:00
|
|
|
// in fact, if there are more than one required arguments, all arguments are added in first one to simplify
|
|
|
|
arg.values.push_back(name);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-14 22:47:24 +00:00
|
|
|
// process version
|
|
|
|
if (haveLongArg("version"))
|
2016-01-14 20:26:10 +00:00
|
|
|
{
|
2016-01-14 22:47:24 +00:00
|
|
|
displayVersion();
|
2016-01-14 20:26:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-01-14 22:47:24 +00:00
|
|
|
// process help if requested or if required arguments are missing
|
2016-01-18 20:22:57 +00:00
|
|
|
if (haveLongArg("help") || (needAdditionalArg() && !haveAdditionalArg()))
|
2016-01-14 20:26:10 +00:00
|
|
|
{
|
2016-01-14 22:47:24 +00:00
|
|
|
displayHelp();
|
2016-01-14 20:26:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCmdArgs::displayHelp()
|
|
|
|
{
|
|
|
|
// display program name
|
|
|
|
printf("Usage: %s ", _ProgramName.c_str());
|
|
|
|
|
|
|
|
// display optional parameters
|
|
|
|
for(uint i = 0; i < _Args.size(); ++i)
|
|
|
|
{
|
|
|
|
const TArg &arg = _Args[i];
|
|
|
|
|
|
|
|
// only short argument is displayed
|
|
|
|
if (!arg.shortName.empty())
|
|
|
|
{
|
|
|
|
printf("[-%s", arg.shortName.c_str());
|
|
|
|
|
|
|
|
// a parameter is required
|
|
|
|
if (!arg.helpName.empty())
|
|
|
|
{
|
|
|
|
printf("<%s>", arg.helpName.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("]");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// display required arguments
|
|
|
|
for(uint i = 0; i < _Args.size(); ++i)
|
|
|
|
{
|
|
|
|
const TArg &arg = _Args[i];
|
|
|
|
|
|
|
|
// they don't have any short or long name, but need a name in help
|
|
|
|
if (arg.shortName.empty() && arg.longName.empty() && !arg.helpName.empty())
|
|
|
|
{
|
2016-01-18 20:22:57 +00:00
|
|
|
printf(" %c%s", arg.required ? '<':'[', arg.helpName.c_str());
|
2016-01-14 20:26:10 +00:00
|
|
|
|
2016-01-18 20:22:57 +00:00
|
|
|
// if support more than once argument
|
|
|
|
if (!arg.onlyOnce) printf("...");
|
|
|
|
|
|
|
|
printf("%c", arg.required ? '>':']');
|
2016-01-14 20:26:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
|
2016-01-18 20:22:57 +00:00
|
|
|
if (!_Description.empty())
|
|
|
|
{
|
|
|
|
printf("\n%s", _Description.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\nWhere options are:\n");
|
|
|
|
|
2016-01-14 20:26:10 +00:00
|
|
|
// display details on each argument
|
|
|
|
for(uint i = 0; i < _Args.size(); ++i)
|
|
|
|
{
|
|
|
|
const TArg &arg = _Args[i];
|
|
|
|
|
|
|
|
// not an optional argument
|
|
|
|
if (arg.shortName.empty() && arg.longName.empty()) continue;
|
|
|
|
|
2016-01-18 20:22:57 +00:00
|
|
|
// 2 spaces
|
|
|
|
printf(" ");
|
|
|
|
|
|
|
|
std::vector<std::string> syntaxes;
|
2016-01-14 20:26:10 +00:00
|
|
|
|
|
|
|
// display short argument
|
|
|
|
if (!arg.shortName.empty())
|
|
|
|
{
|
|
|
|
// and it's required argument
|
|
|
|
if (!arg.helpName.empty())
|
|
|
|
{
|
2016-01-18 20:22:57 +00:00
|
|
|
syntaxes.push_back(toString("-%s <%s>", arg.shortName.c_str(), arg.helpName.c_str()));
|
|
|
|
syntaxes.push_back(toString("-%s<%s>", arg.shortName.c_str(), arg.helpName.c_str()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
syntaxes.push_back(toString("-%s", arg.shortName.c_str()));
|
2016-01-14 20:26:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// display long argument
|
|
|
|
if (!arg.longName.empty())
|
|
|
|
{
|
|
|
|
if (!arg.helpName.empty())
|
|
|
|
{
|
|
|
|
// display first syntax for long argument, --arg <value>
|
2016-01-18 20:22:57 +00:00
|
|
|
syntaxes.push_back(toString("--%s <%s>", arg.longName.c_str(), arg.helpName.c_str()));
|
2016-01-14 20:26:10 +00:00
|
|
|
}
|
|
|
|
|
2016-01-18 20:22:57 +00:00
|
|
|
if (!arg.helpName.empty())
|
2016-01-14 20:26:10 +00:00
|
|
|
{
|
2016-01-18 20:22:57 +00:00
|
|
|
// display second syntax for long argument, --arg=<value>
|
|
|
|
syntaxes.push_back(toString("--%s=<%s>", arg.longName.c_str(), arg.helpName.c_str()));
|
2016-01-14 20:26:10 +00:00
|
|
|
}
|
2016-01-18 20:22:57 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
syntaxes.push_back(toString("--%s", arg.longName.c_str()));
|
|
|
|
}
|
|
|
|
}
|
2016-01-14 20:26:10 +00:00
|
|
|
|
2016-01-18 20:22:57 +00:00
|
|
|
for(uint j = 0; j < syntaxes.size(); ++j)
|
|
|
|
{
|
|
|
|
if (j > 0)
|
2016-01-14 20:26:10 +00:00
|
|
|
{
|
2016-01-18 20:22:57 +00:00
|
|
|
printf("%s ", (j == syntaxes.size() - 1) ? " or":",");
|
2016-01-14 20:26:10 +00:00
|
|
|
}
|
2016-01-18 20:22:57 +00:00
|
|
|
|
|
|
|
printf("%s", syntaxes[j].c_str());
|
2016-01-14 20:26:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// display argument description
|
|
|
|
if (!arg.helpDescription.empty())
|
|
|
|
{
|
|
|
|
printf(" : %s", arg.helpDescription.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// process each argument
|
|
|
|
for(uint i = 0; i < _Args.size(); ++i)
|
|
|
|
{
|
|
|
|
const TArg &arg = _Args[i];
|
|
|
|
|
|
|
|
// only display required arguments
|
|
|
|
if (arg.shortName.empty() && arg.longName.empty() && !arg.helpName.empty() && !arg.helpDescription.empty())
|
|
|
|
{
|
2016-01-18 20:22:57 +00:00
|
|
|
printf(" %s : %s\n", arg.helpName.c_str(), arg.helpDescription.c_str());
|
2016-01-14 20:26:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCmdArgs::displayVersion()
|
|
|
|
{
|
|
|
|
// display a verbose version string
|
|
|
|
#ifdef BUILD_DATE
|
2016-01-18 20:22:57 +00:00
|
|
|
printf("%s %s (built on %s)\nCopyright (C) %s\n", _ProgramName.c_str(), _Version.c_str(), BUILD_DATE, COPYRIGHT);
|
2016-01-14 20:26:10 +00:00
|
|
|
#endif
|
2012-05-29 13:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}; // NAMESPACE NLMISC
|
|
|
|
|