int main(int argc, char* argv[]) { // The command line options parser GetOpt::Parser options; // Option : --text="<X>" or -t "<X>" String optText = "A default value"; options.add(optText, 't', "text", "An example with the type `Yuni::String`"); // Option : --int="<X>" or -i "<X>" int optInt = 42; options.add(optInt, 'i', "int", "An example with the type `int`"); // Ask to the parser to parse the command line if (!options(argc, argv)) { // The program should not continue here return (options.errors() ? 1 /* error */ : 0); } // Displaying the values std::cout << "Value for `optText` : `" << optText << "`" << std::endl; std::cout << "Value for `optInt` : `" << optInt << "`" << std::endl; return 0; }
bool LibConfigProgram::parseCommandLine(int argc, char** argv) { // The Command line parser GetOpt::Parser opts; // Compiler opts.addParagraph("\nTypical usage example :\n $ yuni-config -c gcc -m script --cxxflags\n\nCompiler settings:"); opts.addFlag(pOptCxxFlags, ' ', "cxxflags", "Print the CXX flags (*)"); opts.addFlag(pOptLibFlags, ' ', "libs", "Print the Libs flags (*)"); opts.add(pOptCompiler, 'c', "compiler", "Set the target compiler (gcc,msvc,mingw,icc,...)"); opts.addFlag(pOptPrintCompilerByDefault, ' ', "compiler-default", "Print the compiler used by default and exit"); // All required modules opts.addParagraph("\nModules:"); opts.add(pOptModules, 'm', "modules", "Add one or several requested modules (*)"); opts.addFlag(pOptModuleList, ' ', "module-list", "Print all available modules of the selected version and exit (*)"); opts.addFlag(pOptPrintModulesDeps, ' ', "module-deps", "Print all modules and the dependencies required and exit (*)"); // Prefix opts.addParagraph("\nSearching paths:"); opts.add(pOptPrefix, 'p', "prefix", "Add a prefix folder for searching available versions of libyuni"); opts.addFlag(pOptNoDefaultPath, ' ', "no-default-path", "Do not use the default paths to find available versions of libyuni"); opts.addFlag(pOptDefaultPathList, ' ', "default-path-list", "Print the default paths that would be used and exit (empty if 'no-default-path' is enabled)"); opts.addParagraph("\nVersions:"); opts.addFlag(pOptList, 'l', "list", "Print the list of all versions of libyuni which have been found (with complete informations) and exit"); opts.addFlag(pOptListOnlyVersions, ' ', "list-only-versions", "Print the list of all versions of libyuni which have been found and exit"); // Help opts.addParagraph("\nHelp\n * : Option related to the selected version of libyuni"); bool quiet = false; opts.addFlag(quiet, ' ', "quiet", "Suppress error messages"); opts.addFlag(pOptDebug, ' ', "debug", "Print debug messages"); opts.addFlag(pOptVersion, 'v', "version", "Print the version and exit"); if (!opts(argc, argv)) { pExitStatus = opts.errors() ? 1 /*error*/ : 0; if ((pOptPrintErrors and not quiet) || pOptDebug) std::cout << "Error when parsing the command line\n"; return false; } if (quiet) pOptPrintErrors = false; if (pOptPrintCompilerByDefault) { std::cout << YUNI_LIBCONFIG_DEFAULT_COMPILER << "\n"; return false; } // Clean the prefix paths (make them absolute) pVersionList.debug(pOptDebug); cleanPrefixPaths(); normalizeCompiler(); return true; }
static inline void ParseCommandLine(int argc, char** argv, Settings& settings) { GetOpt::Parser options; String::Vector optFilenames; ShortString16 format; options.add(optFilenames, 'i', "input", "The input grammar"); options.add(settings.namespaceName, 'n', "namespace", "The target namespace (mandatory)"); options.add(format, 'f', "format", "Output format [cpp]"); options.remainingArguments(optFilenames); if (not options(argc, argv)) { if (options.errors()) { std::cout << "Abort due to error" << std::endl; exit(EXIT_FAILURE); } exit(0); } if (optFilenames.empty()) { logs.error() << "please provide a grammar file"; exit(EXIT_FAILURE); } settings.namespaceName.trim(" \t\r\n.:"); if (settings.namespaceName.empty()) { logs.error() << "no namespace provided"; exit(EXIT_FAILURE); } format.trim(); format.toLower(); if (format == "cpp") { settings.format = Settings::sfCPP; } else { logs.error() << "invalid output format"; exit(EXIT_FAILURE); } settings.filenames.resize((uint) optFilenames.size()); for (uint i = 0; i != (uint) optFilenames.size(); ++i) IO::Canonicalize(settings.filenames[i], optFilenames[i]); }
int main(int argc, char* argv[]) { // The command line options parser GetOpt::Parser options; // A simple option : --text String optText; options.add(optText, 't', "text"); // For the help, we want to display _our_ help usage // For that, we have to override the option `--help` bool optHelp = false; options.addFlag(optHelp, 'h', "help"); // But, do not forget to do the same thing for `?` on // Windows, for compatibility with other programs. // Example: getopt_04_customhelpusage.exe /? # ifdef YUNI_OS_WINDOWS // It is safe to use the same variable `optFlag` options.addFlag(optHelp, '?'); # endif // Ask the parser to parse the command line if (!options(argc, argv)) { // The program should not continue here return (options.errors() ? 1 /* error */ : 0); } if (optHelp) { std::cout << "This is our custom help usage.\n" "Here is the list of options :\n\n" " -t, --text=VALUE : A text to display\n\n"; return 0; } std::cout << "Text : `" << optText << "`" << std::endl; return 0; }
int main(int argc, char* argv[]) { // The command line options parser GetOpt::Parser options; // A simple option : --title="<X>" or -t "<X>" // We have to provide the variable where the given value on the command line // will be written String optText = "A default value"; // Adding the option to the parser : // <variable> : the variable where the user data will be written // 't' : The short name of the option (./getopt_00_simple -t "my value") // "text" : The long name of the option (./getopt_00_simple --text "my value") // <description> : The description of the option, that will be displayed in the help usage options.add(optText, 't', "title", "An option with a parameter"); // A simple flag, enabled when the option is present on the command line // Example: ./getopt_00_simple --verbose bool optVerbose = false; options.addFlag(optVerbose, 'v', "verbose", "A simple flag"); // Ask to the parser to parse the command line if (not options(argc, argv)) { // The program should not continue here // The user may have requested the help or an error has happened // If an error has happened, the exit status should be different from 0 if (options.errors()) { std::cout << "Abort due to error" << std::endl; return 1; } return 0; } // Displaying the value of our variable on the standard output std::cout << "Value for `optText` : `" << optText << "`" << std::endl; std::cout << "Verbose : " << optVerbose << std::endl; return 0; }