本文整理汇总了C++中ProgramOptions::parse方法的典型用法代码示例。如果您正苦于以下问题:C++ ProgramOptions::parse方法的具体用法?C++ ProgramOptions::parse怎么用?C++ ProgramOptions::parse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ProgramOptions
的用法示例。
在下文中一共展示了ProgramOptions::parse方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
TEST(ProgramOptions, ParseErrors)
{
ProgramOptions po;
ProgramOptions::Option& help = po.add("help").mnemonic('?');
ProgramOptions::Option& input = po.add("input").mnemonic('i').requiresValue("INPUT");
ProgramOptions::Option& output = po.add("output").mnemonic('o').requiresValue("OUTPUT");
const char* argv1[] = { "program.exe", "--input" };
EXPECT_THROW(po.parse(2, argv1), ProgramOptionNeedsValue);
const char* argv2[] = { "program.exe", "-i" };
EXPECT_THROW(po.parse(2, argv2), ProgramOptionNeedsValue);
const char* argv3[] = { "program.exe", "--test" };
EXPECT_THROW(po.parse(2, argv3), InvalidProgramOption);
const char* argv4[] = { "program.exe", "-?a" };
po.reset();
EXPECT_FALSE(help.enabled());
EXPECT_THROW(po.parse(2, argv4), InvalidProgramOption);
EXPECT_TRUE(help.enabled()); // -? is parsed anyway, -a is the invalid option
const char* argv5[] = { "program.exe", "-io", "input-and-output.txt" };
po.reset();
EXPECT_THROW(po.parse(2, argv5), ProgramOptionNeedsValue);
po.reset();
EXPECT_THROW(po.parse(3, argv5), InvalidProgramOptionsCombination);
EXPECT_TRUE(input.enabled());
EXPECT_TRUE(output.enabled());
EXPECT_EQ("input-and-output.txt", input.value());
EXPECT_EQ("", output.value());
}
示例2: main
int main(int argc, const char* argv[])
{
ProgramOptions opts;
if( opts.parse(argc, argv) ) {
return 1;
}
{
Debug_DisablePhase("Load Repository");
Debug_DisablePhase("Load Root");
Debug_DisablePhase("Load Dependencies");
Debug_DisablePhase("Enumerate Build");
Debug_DisablePhase("Run Build");
if( const char* e = getenv("MINICARGO_DEBUG") )
{
while( *e )
{
const char* colon = ::std::strchr(e, ':');
size_t len = colon ? colon - e : ::std::strlen(e);
Debug_EnablePhase(::std::string(e, len).c_str());
if( colon )
e = colon + 1;
else
e = e + len;
}
}
}
try
{
Debug_SetPhase("Load Repository");
// Load package database
Repository repo;
// TODO: load repository from a local cache
if( opts.vendor_dir )
{
repo.load_vendored(opts.vendor_dir);
}
auto bs_override_dir = opts.override_directory ? ::helpers::path(opts.override_directory) : ::helpers::path();
// 1. Load the Cargo.toml file from the passed directory
Debug_SetPhase("Load Root");
auto dir = ::helpers::path(opts.directory ? opts.directory : ".");
auto m = PackageManifest::load_from_toml( dir / "Cargo.toml" );
// 2. Load all dependencies
Debug_SetPhase("Load Dependencies");
m.load_dependencies(repo, !bs_override_dir.is_valid());
// 3. Build dependency tree and build program.
BuildOptions build_opts;
build_opts.build_script_overrides = ::std::move(bs_override_dir);
build_opts.output_dir = opts.output_directory ? ::helpers::path(opts.output_directory) : ::helpers::path("output");
build_opts.lib_search_dirs.reserve(opts.lib_search_dirs.size());
build_opts.emit_mmir = opts.emit_mmir;
build_opts.target_name = opts.target;
for(const auto* d : opts.lib_search_dirs)
build_opts.lib_search_dirs.push_back( ::helpers::path(d) );
Debug_SetPhase("Enumerate Build");
auto build_list = BuildList(m, build_opts);
Debug_SetPhase("Run Build");
if( !build_list.build(::std::move(build_opts), opts.build_jobs) )
{
::std::cerr << "BUILD FAILED" << ::std::endl;
if(opts.pause_before_quit) {
::std::cout << "Press enter to exit..." << ::std::endl;
::std::cin.get();
}
return 1;
}
}
catch(const ::std::exception& e)
{
::std::cerr << "EXCEPTION: " << e.what() << ::std::endl;
if(opts.pause_before_quit) {
::std::cout << "Press enter to exit..." << ::std::endl;
::std::cin.get();
}
return 1;
}
if(opts.pause_before_quit) {
::std::cout << "Press enter to exit..." << ::std::endl;
::std::cin.get();
}
return 0;
}
示例3: parse
void ArangoClient::parse (ProgramOptions& options,
ProgramOptionsDescription& description,
string const& example,
int argc,
char* argv[],
string const& initFilename) {
// if options are invalid, exit directly
if (! options.parse(description, argc, argv)) {
LOG_FATAL_AND_EXIT("%s", options.lastError().c_str());
}
// setup the logging
TRI_SetLogLevelLogging(_logLevel.c_str());
TRI_CreateLogAppenderFile("-", 0, TRI_LOG_SEVERITY_UNKNOWN, false);
TRI_SetLineNumberLogging(false);
TRI_SetThreadIdentifierLogging(false);
// parse config file
string configFile = "";
bool allowLocal = false;
if (! _configFile.empty()) {
if (StringUtils::tolower(_configFile) == string("none")) {
LOG_DEBUG("using no init file at all");
}
else {
configFile = _configFile;
}
}
else {
char* d = TRI_LocateConfigDirectory();
if (d != 0) {
string sysDir = string(d) + initFilename;
TRI_FreeString(TRI_CORE_MEM_ZONE, d);
if (FileUtils::exists(sysDir)) {
configFile = sysDir;
allowLocal = true;
}
else {
LOG_DEBUG("no system init file '%s'", sysDir.c_str());
}
}
}
if (! configFile.empty()) {
if (allowLocal) {
string localConfigFile = configFile + ".local";
if (FileUtils::exists(localConfigFile)) {
LOG_DEBUG("using init override file '%s'", localConfigFile.c_str());
if (! options.parse(description, localConfigFile)) {
LOG_FATAL_AND_EXIT("cannot parse config file '%s': %s", localConfigFile.c_str(), options.lastError().c_str());
}
}
}
LOG_DEBUG("using init file '%s'", configFile.c_str());
if (! options.parse(description, configFile)) {
LOG_FATAL_AND_EXIT("cannot parse config file '%s': %s", configFile.c_str(), options.lastError().c_str());
}
}
// configuration is parsed and valid if we got to this point
// check for --help
set<string> help = options.needHelp("help");
if (! help.empty()) {
if (! example.empty()) {
cout << "USAGE: " << argv[0] << " " << example << endl << endl;
}
cout << description.usage(help) << endl;
// --help always returns success
TRI_EXIT_FUNCTION(EXIT_SUCCESS, NULL);
}
// set temp path
if (options.has("temp-path")) {
TRI_SetUserTempPath((char*) _tempPath.c_str());
}
if (options.has("server.username")) {
// if a username is specified explicitly, assume authentication is desired
_disableAuthentication = false;
}
// check if have a password
_hasPassword = options.has("server.password")
|| _disableAuthentication
|| options.has("jslint")
|| options.has("javascript.unit-tests");
// set colors
//.........这里部分代码省略.........
示例4: parse
void ArangoClient::parse (ProgramOptions& options,
ProgramOptionsDescription& description,
int argc,
char* argv[],
string const& initFilename) {
if (! options.parse(description, argc, argv)) {
cerr << options.lastError() << "\n";
exit(EXIT_FAILURE);
}
// check for help
set<string> help = options.needHelp("help");
if (! help.empty()) {
cout << description.usage(help) << endl;
exit(EXIT_SUCCESS);
}
// setup the logging
TRI_SetLogLevelLogging(_logLevel.c_str());
TRI_CreateLogAppenderFile("-");
TRI_SetLineNumberLogging(false);
TRI_SetThreadIdentifierLogging(false);
// parse config file
string configFile = "";
if (! _configFile.empty()) {
if (StringUtils::tolower(_configFile) == string("none")) {
LOGGER_INFO << "using no init file at all";
}
else {
configFile = _configFile;
}
}
#ifdef _SYSCONFDIR_
else {
string sysDir = string(_SYSCONFDIR_);
string systemConfigFile = initFilename;
if (! sysDir.empty()) {
if (sysDir[sysDir.size() - 1] != '/') {
sysDir += "/" + systemConfigFile;
}
else {
sysDir += systemConfigFile;
}
if (FileUtils::exists(sysDir)) {
configFile = sysDir;
}
else {
LOGGER_DEBUG << "no system init file '" << sysDir << "'";
}
}
}
#endif
if (! configFile.empty()) {
LOGGER_DEBUG << "using init file '" << configFile << "'";
if (! options.parse(description, configFile)) {
cout << "cannot parse config file '" << configFile << "': " << options.lastError() << endl;
exit(EXIT_FAILURE);
}
}
// check if have a password
_hasPassword = options.has("server.password") || options.has("server.disable-authentication");
// set colors
if (options.has("colors")) {
_noColors = false;
}
if (options.has("no-colors")) {
_noColors = true;
}
// set auto-completion
if (options.has("auto-complete")) {
_noAutoComplete = false;
}
if (options.has("no-auto-complete")) {
_noAutoComplete = true;
}
// set pretty print
if (options.has("pretty-print")) {
_prettyPrint = true;
}
if (options.has("no-pretty-print")) {
_prettyPrint = false;
}
//.........这里部分代码省略.........