26 static inline File resolveFilename (
const String& name)
31 static inline File checkFileExists (
const File& f)
39 static inline File checkFolderExists (
const File& f)
41 if (! f.isDirectory())
47 static inline File resolveFilenameForOption (
const ArgumentList& args, StringRef option,
const String& filename)
49 if (filename.isEmpty())
51 args.failIfOptionIsMissing (option);
55 return resolveFilename (filename);
60 return resolveFilename (
text);
72 if (! f.isDirectory())
78 static inline bool isShortOptionFormat (
StringRef s) {
return s[0] ==
'-' && s[1] !=
'-'; }
79 static inline bool isLongOptionFormat (
StringRef s) {
return s[0] ==
'-' && s[1] ==
'-' && s[2] !=
'-'; }
80 static inline bool isOptionFormat (
StringRef s) {
return s[0] ==
'-'; }
88 if (! isLongOptionFormat (option))
90 jassert (! isShortOptionFormat (option));
112 jassert (option !=
'-');
124 if (isShortOptionFormat (o) && o.length() == 2 &&
isShortOption ((
char) o[1]))
162 if (
size() < expectedMinNumberOfArgs)
168 jassert (option ==
String (option).trim());
170 for (
int i = 0; i <
arguments.size(); ++i)
200 jassert (isOptionFormat (option));
202 for (
int i = 0; i <
arguments.size(); ++i)
208 if (arg.isShortOption())
211 return arguments.getReference (i + 1).text;
216 if (arg.isLongOption())
217 return arg.getLongOptionValue();
226 jassert (isOptionFormat (option));
228 for (
int i = 0; i <
arguments.size(); ++i)
234 if (arg.isShortOption())
238 auto result =
arguments.getReference (i + 1).text;
247 if (arg.isLongOption())
249 auto result = arg.getLongOptionValue();
311 std::cerr << error.errorMessage << std::endl;
312 returnCode = error.returnCode;
320 for (
auto& c : commands)
324 if (optionMustBeFirstArg ? (index == 0) : (index >= 0))
328 if (commandIfNoOthersRecognised >= 0)
329 return &commands[(size_t) commandIfNoOthersRecognised];
336 return invokeCatchingFailures ([&args, optionMustBeFirstArg,
this]
338 if (
auto c = findCommand (args, optionMustBeFirstArg))
341 fail (
"Unrecognised arguments");
354 commands.emplace_back (std::move (c));
359 commandIfNoOthersRecognised = (int) commands.size();
360 addCommand (std::move (c));
365 Command c { arg, arg,
"Prints the list of commands", {},
368 std::cout << helpMessage << std::endl;
369 printCommandList (args);
372 if (makeDefaultCommand)
373 addDefaultCommand (std::move (c));
375 addCommand (std::move (c));
380 addCommand ({ arg, arg,
"Prints the current version number", {},
383 std::cout << versionText << std::endl;
401 int descriptionIndent)
403 auto nameAndArgs = getExeNameAndArgs (args, command);
405 if (nameAndArgs.length() > descriptionIndent)
406 std::cout << nameAndArgs << std::endl <<
String().
paddedRight (
' ', descriptionIndent);
408 std::cout << nameAndArgs.
paddedRight (
' ', descriptionIndent);
415 int descriptionIndent = 0;
417 for (
auto& c : commands)
418 descriptionIndent = std::max (descriptionIndent, getExeNameAndArgs (args, c).length());
420 descriptionIndent = std::min (descriptionIndent + 2, 40);
422 for (
auto& c : commands)
423 printCommandDescription (args, c, descriptionIndent);
425 std::cout << std::endl;
430 auto len = getExeNameAndArgs (args, command).
length();
432 printCommandDescription (args, command, std::min (len + 3, 40));
Array< Argument > arguments
The list of arguments (not including the name of the executable that was invoked).
String getLongOptionValue() const
If this argument is a long option with a value, this returns the value.
ArgumentList(String executable, StringArray arguments)
Creates an argument list for a given executable.
bool isShortOption() const
Returns true if this argument starts with a single dash.
bool isOption() const
Returns true if this argument starts with one or more dashes.
int findAndRunCommand(const ArgumentList &, bool optionMustBeFirstArg=false) const
Looks for the first command in the list which matches the given arguments, and tries to invoke it...
bool isNotEmpty() const noexcept
Returns true if the string contains at least one character.
String text
The original text of this argument.
String getValueForOption(StringRef option) const
Looks for a given argument and returns either its assigned value (for long options) or the string tha...
A simple class for holding temporary references to a string literal or String.
bool removeOptionIfFound(StringRef option)
Returns true if the given string matches one of the arguments, and also removes the argument from the...
String removeValueForOption(StringRef option)
Looks for a given argument and returns either its assigned value (for long options) or the string tha...
int indexOfOption(StringRef option) const
Returns the index of the given string if it matches one of the arguments, or -1 if it doesn't...
void printCommandDetails(const ArgumentList &, const Command &) const
Prints out a longer description of a particular command, based on its longDescription member...
Holds a list of command-line arguments, and provides useful methods for searching and operating on th...
void removeEmptyStrings(bool removeWhitespaceStrings=true)
Removes empty strings from the array.
static void fail(String errorMessage, int returnCode=1)
Throws a failure exception to cause a command-line app to terminate.
File getFileForOption(StringRef option) const
Looks for the value of argument using getValueForOption() and tries to parse that value as a file...
bool containsChar(juce_wchar character) const noexcept
Tests whether the string contains a particular character.
String shortDescription
A short (one line) description of this command, which can be printed by ConsoleApplication::printComm...
File getExistingFileForOptionAndRemove(StringRef option)
Looks for a file argument using getFileForOption() and fails with a suitable error if the file doesn'...
A special array for holding a list of strings.
File getChildFile(StringRef relativeOrAbsolutePath) const
Returns a file that represents a relative (or absolute) sub-path of the current one.
const Command * findCommand(const ArgumentList &, bool optionMustBeFirstArg) const
Looks for the first command in the list which matches the given arguments.
File getFileForOptionAndRemove(StringRef option)
Looks for the value of argument using getValueForOption() and tries to parse that value as a file...
void trim()
Deletes any whitespace characters from the starts and ends of all the strings.
String fromLastOccurrenceOf(StringRef substringToFind, bool includeSubStringInResult, bool ignoreCase) const
Returns a section of the string starting from the last occurrence of a given substring.
String paddedRight(juce_wchar padCharacter, int minimumLength) const
Returns a copy of this string with the specified character repeatedly added to its end until the tota...
String substring(int startIndex, int endIndex) const
Returns a subsection of the string.
String argumentDescription
A description of the command-line arguments needed for this command, which will be printed as part of...
void addCommand(Command)
Adds a command to the list.
Represents a command that can be executed if its command-line arguments are matched.
void addDefaultCommand(Command)
Adds a command to the list, and marks it as one which is invoked if no other command matches...
Argument operator[](int index) const
Returns one of the arguments.
bool operator!=(StringRef stringToCompare) const
Compares this argument against a string.
File resolveAsExistingFolder() const
Resolves a user-supplied folder name into an absolute File, using the current working directory as a ...
static StringArray fromTokens(StringRef stringToTokenise, bool preserveQuotedStrings)
Returns an array containing the tokens in a given string.
const std::vector< Command > & getCommands() const
Gives read-only access to the list of registered commands.
One of the arguments in an ArgumentList.
Represents a local file or directory.
File resolveAsFile() const
Resolves this argument as an absolute File, using the current working directory as a base for resolvi...
File resolveAsExistingFile() const
Resolves this argument as an absolute File, using the current working directory as a base for resolvi...
File getExistingFileForOption(StringRef option) const
Looks for a file argument using getFileForOption() and fails with a suitable error if the file doesn'...
void checkMinNumArguments(int expectedMinNumberOfArgs) const
Throws an error unless there are at least the given number of arguments.
void addHelpCommand(String helpArgument, String helpMessage, bool makeDefaultCommand)
Adds a help command to the list.
String executableName
The name or path of the executable that was invoked, as it was specified on the command-line.
String upToFirstOccurrenceOf(StringRef substringToEndWith, bool includeSubStringInResult, bool ignoreCase) const
Returns the start of this string, up to the first occurrence of a substring.
void printCommandList(const ArgumentList &) const
Prints out the list of commands and their short descriptions in a format that's suitable for use as h...
bool operator==(StringRef stringToCompare) const
Compares this argument against a string.
void failIfOptionIsMissing(StringRef option) const
Throws an error unless the given option is found in the argument list.
int size() const
Returns the number of arguments in the list.
File getExistingFolderForOptionAndRemove(StringRef option)
Looks for a filename argument using getFileForOption() and fails with a suitable error if the file is...
bool isLongOption() const
Returns true if this argument starts with a double dash.
int indexOfChar(juce_wchar characterToLookFor) const noexcept
Searches for a character inside this string.
static File getCurrentWorkingDirectory()
Returns the current working directory.
static int invokeCatchingFailures(std::function< int()> &&functionToCall)
Invokes a function, catching any fail() calls that it might trigger, and handling them by printing th...
int length() const noexcept
Returns the number of characters in the string.
File getExistingFolderForOption(StringRef option) const
Looks for a filename argument using getFileForOption() and fails with a suitable error if the file is...
String longDescription
A longer description of this command, for use in extended help.
bool containsOption(StringRef option) const
Returns true if the given string matches one of the arguments.
void addVersionCommand(String versionArgument, String versionText)
Adds a command that will print the given text in response to the "--version" option.