proxygen
|
#include <NestedCommandLineApp.h>
Classes | |
struct | CommandInfo |
Public Types | |
typedef std::function< void(const std::string &command, const boost::program_options::variables_map &options, const std::vector< std::string > &args)> | InitFunction |
typedef std::function< void(const boost::program_options::variables_map &options, const std::vector< std::string > &)> | Command |
Public Member Functions | |
NestedCommandLineApp (std::string programName=std::string(), std::string version=std::string(), std::string programHeading=std::string(), std::string programHelpFooter=std::string(), InitFunction initFunction=InitFunction()) | |
void | addGFlags (ProgramOptionsStyle style=ProgramOptionsStyle::GNU) |
boost::program_options::options_description & | globalOptions () |
boost::program_options::options_description & | addCommand (std::string name, std::string argStr, std::string shortHelp, std::string fullHelp, Command command) |
void | addAlias (std::string newName, std::string oldName) |
int | run (int argc, const char *const argv[]) |
int | run (const std::vector< std::string > &args) |
bool | isBuiltinCommand (const std::string &name) const |
Static Public Attributes | |
static constexpr StringPiece const | kHelpCommand = "help" |
static constexpr StringPiece const | kVersionCommand = "version" |
Private Member Functions | |
void | doRun (const std::vector< std::string > &args) |
const std::string & | resolveAlias (const std::string &name) const |
const std::pair< const std::string, CommandInfo > & | findCommand (const std::string &name) const |
void | displayHelp (const boost::program_options::variables_map &options, const std::vector< std::string > &args) const |
void | displayVersion () const |
Private Attributes | |
std::string | programName_ |
std::string | programHeading_ |
std::string | programHelpFooter_ |
std::string | version_ |
InitFunction | initFunction_ |
boost::program_options::options_description | globalOptions_ |
std::map< std::string, CommandInfo > | commands_ |
std::map< std::string, std::string > | aliases_ |
std::set< folly::StringPiece > | builtinCommands_ |
App that uses a nested command line, of the form:
program [–global_options...] command [–command_options...] command_args...
Definition at line 51 of file NestedCommandLineApp.h.
typedef std::function<void( const boost::program_options::variables_map& options, const std::vector<std::string>&)> folly::NestedCommandLineApp::Command |
Definition at line 62 of file NestedCommandLineApp.h.
typedef std::function<void( const std::string& command, const boost::program_options::variables_map& options, const std::vector<std::string>& args)> folly::NestedCommandLineApp::InitFunction |
Definition at line 57 of file NestedCommandLineApp.h.
|
explicit |
Initialize the app.
If programName is not set, we try to guess (readlink("/proc/self/exe")).
version is the version string printed when given the –version flag.
initFunction, if specified, is called after parsing the command line, right before executing the command.
Definition at line 51 of file NestedCommandLineApp.cpp.
References addCommand(), builtinCommands_, displayHelp(), displayVersion(), globalOptions_, kHelpCommand, kVersionCommand, and folly::Range< Iter >::str().
void folly::NestedCommandLineApp::addAlias | ( | std::string | newName, |
std::string | oldName | ||
) |
Add an alias; running the command newName will have the same effect as running oldName.
Definition at line 109 of file NestedCommandLineApp.cpp.
References aliases_, commands_, and folly::gen::move.
Referenced by main().
po::options_description & folly::NestedCommandLineApp::addCommand | ( | std::string | name, |
std::string | argStr, | ||
std::string | shortHelp, | ||
std::string | fullHelp, | ||
Command | command | ||
) |
Add a command.
name: command name argStr: description of arguments in help strings (<filename> <N>) shortHelp: one-line summary help string fullHelp: full help string command: function to run
Returns a reference to the options_description object that you can use to add options for this command.
Definition at line 90 of file NestedCommandLineApp.cpp.
References commands_, deadlock::info(), folly::gen::move, and folly::sformat().
Referenced by main(), and NestedCommandLineApp().
|
inline |
Add GFlags to the list of supported options with the given style.
Definition at line 86 of file NestedCommandLineApp.h.
References folly::getGFlags().
Referenced by main().
|
private |
Definition at line 117 of file NestedCommandLineApp.cpp.
References aliases_, commands_, findCommand(), globalOptions_, deadlock::info(), max, programHeading_, programHelpFooter_, programName_, and resolveAlias().
Referenced by doRun(), and NestedCommandLineApp().
|
private |
Definition at line 188 of file NestedCommandLineApp.cpp.
References programName_, and version_.
Referenced by doRun(), and NestedCommandLineApp().
|
private |
Definition at line 266 of file NestedCommandLineApp.cpp.
References cmd, displayHelp(), displayVersion(), findCommand(), globalOptions_, deadlock::info(), initFunction_, kHelpCommand, kVersionCommand, folly::parseNestedCommandLine(), programName_, folly::sformat(), and folly::Range< Iter >::str().
Referenced by run().
|
private |
Definition at line 205 of file NestedCommandLineApp.cpp.
References commands_, kHelpCommand, name, programName_, resolveAlias(), and folly::sformat().
Referenced by displayHelp(), and doRun().
|
inline |
Return the global options object, so you can add options.
Definition at line 93 of file NestedCommandLineApp.h.
References argv, name, folly::run(), and string.
bool folly::NestedCommandLineApp::isBuiltinCommand | ( | const std::string & | name | ) | const |
Return true if name represent known built-in command (help, version)
Definition at line 333 of file NestedCommandLineApp.cpp.
References builtinCommands_.
Referenced by folly::test::TEST().
|
private |
Definition at line 192 of file NestedCommandLineApp.cpp.
References aliases_, upload::dest, and name.
Referenced by displayHelp(), and findCommand().
int folly::NestedCommandLineApp::run | ( | int | argc, |
const char *const | argv[] | ||
) |
Run the command and return; the return code is 0 on success or non-zero on error, so it is idiomatic to call this at the end of main(): return app.run(argc, argv);
On successful exit, run() will check for errors on stdout (and flush it) to help command-line applications that need to write to stdout (failing to write to stdout is an error). If there is an error on stdout, we'll print a helpful message on stderr and return an error status (1).
Definition at line 220 of file NestedCommandLineApp.cpp.
References programName_.
Referenced by main().
int folly::NestedCommandLineApp::run | ( | const std::vector< std::string > & | args | ) |
Definition at line 227 of file NestedCommandLineApp.cpp.
References doRun(), folly::errnoStr(), folly::pushmi::operators::error(), kHelpCommand, programName_, folly::sformat(), and folly::ProgramExit::status().
|
private |
Definition at line 169 of file NestedCommandLineApp.h.
Referenced by addAlias(), displayHelp(), and resolveAlias().
|
private |
Definition at line 170 of file NestedCommandLineApp.h.
Referenced by isBuiltinCommand(), and NestedCommandLineApp().
|
private |
Definition at line 168 of file NestedCommandLineApp.h.
Referenced by addAlias(), addCommand(), displayHelp(), and findCommand().
|
private |
Definition at line 167 of file NestedCommandLineApp.h.
Referenced by displayHelp(), doRun(), and NestedCommandLineApp().
|
private |
Definition at line 166 of file NestedCommandLineApp.h.
Referenced by doRun().
|
static |
Definition at line 64 of file NestedCommandLineApp.h.
Referenced by doRun(), findCommand(), NestedCommandLineApp(), folly::ProgramExit::ProgramExit(), run(), and folly::test::TEST().
|
static |
Definition at line 65 of file NestedCommandLineApp.h.
Referenced by doRun(), NestedCommandLineApp(), folly::ProgramExit::ProgramExit(), and folly::test::TEST().
|
private |
Definition at line 163 of file NestedCommandLineApp.h.
Referenced by displayHelp().
|
private |
Definition at line 164 of file NestedCommandLineApp.h.
Referenced by displayHelp().
|
private |
Definition at line 162 of file NestedCommandLineApp.h.
Referenced by displayHelp(), displayVersion(), doRun(), findCommand(), and run().
|
private |
Definition at line 165 of file NestedCommandLineApp.h.
Referenced by displayVersion().