JUCE  v5.4.1-191-g0ab5e696f
JUCE API
Looking for a senior C++ dev?
I'm looking for work. Hire me!
juce::ConsoleApplication Struct Reference

Represents a the set of commands that a console app can perform, and provides helper functions for performing them. More...

#include <juce_ConsoleApplication.h>

Collaboration diagram for juce::ConsoleApplication:

Classes

struct  Command
 Represents a command that can be executed if its command-line arguments are matched. More...
 

Public Member Functions

void addCommand (Command)
 Adds a command to the list. More...
 
void addHelpCommand (String helpArgument, String helpMessage, bool invokeIfNoOtherCommandRecognised)
 Adds a help command to the list. More...
 
void addVersionCommand (String versionArgument, String versionText)
 Adds a command that will print the given text in response to the "--version" option. More...
 
int findAndRunCommand (const ArgumentList &) const
 Looks for the first command in the list which matches the given arguments, and tries to invoke it. More...
 
int findAndRunCommand (int argc, char *argv[]) const
 Creates an ArgumentList object from the argc and argv variablrs, and invokes findAndRunCommand() using it. More...
 

Static Public Member Functions

static void fail (String errorMessage, int returnCode=1)
 Throws a failure exception to cause a command-line app to terminate. More...
 
static int invokeCatchingFailures (std::function< int()> &&functionToCall)
 Invokes a function, catching any fail() calls that it might trigger, and handling them by printing their error message and returning their error code. More...
 

Private Member Functions

void printHelp (const String &preamble, const ArgumentList &) const
 

Private Attributes

String commandIfNoOthersRecognised
 
std::vector< Commandcommands
 

Detailed Description

Represents a the set of commands that a console app can perform, and provides helper functions for performing them.

When using these helper classes to implement a console app, you probably want to do something along these lines:

int main (int argc, char* argv[])
{
ConsoleApplication app;
app.addHelpCommand ("--help|-h", "Usage:", true);
app.addVersionCommand ("--version|-v", "MyApp version 1.2.3");
app.addCommand ({ "--foo",
"--foo filename",
"Performs a foo operation on the given file",
[] (const auto& args) { doFoo (args); }});
return app.findAndRunCommand (argc, argv);
}
See also
ArgumentList

Member Function Documentation

◆ addCommand()

void juce::ConsoleApplication::addCommand ( Command  )

Adds a command to the list.

◆ addHelpCommand()

void juce::ConsoleApplication::addHelpCommand ( String  helpArgument,
String  helpMessage,
bool  invokeIfNoOtherCommandRecognised 
)

Adds a help command to the list.

This command will print the user-supplied message that's passed in here as an argument, followed by a list of all the registered commands.

◆ addVersionCommand()

void juce::ConsoleApplication::addVersionCommand ( String  versionArgument,
String  versionText 
)

Adds a command that will print the given text in response to the "--version" option.

◆ fail()

static void juce::ConsoleApplication::fail ( String  errorMessage,
int  returnCode = 1 
)
static

Throws a failure exception to cause a command-line app to terminate.

This is intended to be called from code in a Command, so that the exception will be automatically caught and turned into a printed error message and a return code which will be returned from main().

See also
ConsoleApplication::invokeCatchingFailures()

◆ findAndRunCommand() [1/2]

int juce::ConsoleApplication::findAndRunCommand ( const ArgumentList ) const

Looks for the first command in the list which matches the given arguments, and tries to invoke it.

If no command is found, it prints a help message listing the available commands. If the command calls the fail() function, this will throw an exception that gets automatically caught and handled, and this method will return the error code that was passed into the fail() call.

◆ findAndRunCommand() [2/2]

int juce::ConsoleApplication::findAndRunCommand ( int  argc,
char *  argv[] 
) const

Creates an ArgumentList object from the argc and argv variablrs, and invokes findAndRunCommand() using it.

◆ invokeCatchingFailures()

static int juce::ConsoleApplication::invokeCatchingFailures ( std::function< int()> &&  functionToCall)
static

Invokes a function, catching any fail() calls that it might trigger, and handling them by printing their error message and returning their error code.

See also
ConsoleApplication::fail()

◆ printHelp()

void juce::ConsoleApplication::printHelp ( const String preamble,
const ArgumentList  
) const
private

Member Data Documentation

◆ commandIfNoOthersRecognised

String juce::ConsoleApplication::commandIfNoOthersRecognised
private

◆ commands

std::vector<Command> juce::ConsoleApplication::commands
private

The documentation for this struct was generated from the following file: