26static inline File resolveFilename (
const String& name)
31static inline void checkFileExists (
const File& f)
37static inline void checkFolderExists (
const File& f)
39 if (! f.isDirectory())
45 return resolveFilename (
text);
50 auto f = resolveAsFile();
57 auto f = resolveAsFile();
59 if (! f.isDirectory())
65static inline bool isShortOptionFormat (
StringRef s) {
return s[0] ==
'-' &&
s[1] !=
'-'; }
66static inline bool isLongOptionFormat (
StringRef s) {
return s[0] ==
'-' &&
s[1] ==
'-' &&
s[2] !=
'-'; }
67static inline bool isOptionFormat (StringRef s) {
return s[0] ==
'-'; }
75 if (! isLongOptionFormat (
option))
77 jassert (! isShortOptionFormat (
option));
78 return isLongOption (
"--" +
option);
81 return text.upToFirstOccurrenceOf (
"=",
false,
false) ==
option;
97 return isShortOption() && text.containsChar (
option);
107 if (isShortOptionFormat (
o) &&
o.length() == 2 && isShortOption ((
char)
o[1]))
110 if (isLongOptionFormat (
o) && isLongOption (
o))
124 args.removeEmptyStrings();
173 jassert (isOptionFormat (
option));
181 if (
arg.isShortOption())
189 if (
arg.isLongOption())
190 return arg.getLongOptionValue();
207 return resolveFilename (text);
213 checkFileExists (file);
220 checkFolderExists (file);
246 std::cout << error.errorMessage << std::endl;
247 returnCode = error.returnCode;
255 for (
auto&
c : commands)
257 auto index =
args.indexOfOption (
c.commandOption);
263 if (commandIfNoOthersRecognised >= 0)
264 return &commands[(
size_t) commandIfNoOthersRecognised];
274 fail (
"Unrecognised arguments");
285 commands.emplace_back (std::move (
c));
290 commandIfNoOthersRecognised = (
int) commands.size();
325 auto exeName =
args.executableName.fromLastOccurrenceOf (
"/",
false,
false)
326 .fromLastOccurrenceOf (
"\\",
false,
false);
331 for (
auto&
c : commands)
340 for (
size_t i = 0; i < commands.size(); ++i)
350 std::cout << commands[i].shortDescription << std::endl;
353 std::cout << std::endl;
Holds a resizable array of primitive or copy-by-value objects.
int size() const noexcept
Returns the current number of elements in the array.
void add(const ElementType &newElement)
Appends a new element at the end of the array.
ElementType & getReference(int index) const noexcept
Returns a direct reference to one of the elements in the array, without checking the index passed in.
Represents a local file or directory.
static File getCurrentWorkingDirectory()
Returns the current working directory.
A special array for holding a list of strings.
static StringArray fromTokens(StringRef stringToTokenise, bool preserveQuotedStrings)
Returns an array containing the tokens in a given string.
A simple class for holding temporary references to a string literal or String.
static String repeatedString(StringRef stringToRepeat, int numberOfTimesToRepeat)
Creates a string which is a version of a string repeated and joined together.
String substring(int startIndex, int endIndex) const
Returns a subsection of the string.
Represents a command that can be executed if its command-line arguments are matched.
One of the arguments in an ArgumentList.
bool operator!=(StringRef stringToCompare) const
Compares this argument against a string.
File resolveAsExistingFile() const
Resolves this argument as an absolute File, using the current working directory as a base for resolvi...
String text
The original text of this argument.
bool isLongOption() const
Returns true if this argument starts with a double dash.
File resolveAsExistingFolder() const
Resolves a user-supplied folder name into an absolute File, using the current working directory as a ...
bool isShortOption() const
Returns true if this argument starts with a single dash.
File resolveAsFile() const
Resolves this argument as an absolute File, using the current working directory as a base for resolvi...
bool operator==(StringRef stringToCompare) const
Compares this argument against a string.
String getLongOptionValue() const
If this argument is a long option with a value, this returns the value.
bool isOption() const
Returns true if this argument starts with one or more dashes.
Holds a list of command-line arguments, and provides useful methods for searching and operating on th...
File getExistingFileForOption(StringRef option) const
Looks for a file argument using getFileForOption() and fails with a suitable error if the file doesn'...
File getExistingFolderForOption(StringRef option) const
Looks for a filename argument using getFileForOption() and fails with a suitable error if the file is...
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.
String executableName
The name or path of the executable that was invoked, as it was specified on the command-line.
File getFileForOption(StringRef option) const
Looks for the value of argument using getValueForOption() and tries to parse that value as a file.
void failIfOptionIsMissing(StringRef option) const
Throws an error unless the given option is found in the argument list.
Array< Argument > arguments
The list of arguments (not including the name of the executable that was invoked).
bool containsOption(StringRef option) const
Returns true if the given string matches one of the arguments.
void checkMinNumArguments(int expectedMinNumberOfArgs) const
Throws an error unless there are at least the given number of arguments.
String getValueForOption(StringRef option) const
Looks for a given argument and returns either its assigned value (for long options) or the string tha...
Argument operator[](int index) const
Returns one of the arguments.
int size() const
Returns the number of arguments in the list.
ArgumentList(String executable, StringArray arguments)
Creates an argument list for a given executable.
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.
void addDefaultCommand(Command)
Adds a command to the list, and marks it as one which is invoked if no other command matches.
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...
const Command * findCommand(const ArgumentList &, bool optionMustBeFirstArg) const
Looks for the first command in the list which matches the given arguments.
void addCommand(Command)
Adds a command to the list.
static int invokeCatchingFailures(std::function< int()> &&functionToCall)
Invokes a function, catching any fail() calls that it might trigger, and handling them by printing th...
void addHelpCommand(String helpArgument, String helpMessage, bool makeDefaultCommand)
Adds a help command to the list.
void addVersionCommand(String versionArgument, String versionText)
Adds a command that will print the given text in response to the "--version" option.
static void fail(String errorMessage, int returnCode=1)
Throws a failure exception to cause a command-line app to terminate.
const std::vector< Command > & getCommands() const
Gives read-only access to the list of registered commands.