/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- * vim: set ts=8 sts=2 et sw=2 tw=80: * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// We want to use the shell's option parser before initializing the JS engine. // The JS malloc arena isn't available yet at this point, so we use a custom // allocation policy that uses the system malloc instead. class OptionAllocPolicy { public: template <typename T>
T* pod_malloc(size_t numElems) {
size_t bytes; if (MOZ_UNLIKELY(!js::CalculateAllocSize<T>(numElems, &bytes))) { return nullptr;
} returnstatic_cast<T*>(malloc(bytes));
}
/* * Builder for describing a command line interface and parsing the resulting * specification. * * - A multi-option is an option that can appear multiple times and still * parse as valid command line arguments. * - An "optional argument" is supported for backwards compatibility with prior * command line interface usage. Once one optional argument has been added, * *only* optional arguments may be added.
*/ class OptionParser { public: enum Result {
Okay = 0,
Fail, /* As in, allocation fail. */
ParseError, /* Successfully parsed but with an error. */
EarlyExit /* Successfully parsed but exits the program,
* for example with --help and --version. */
};
private: using Options = Vector<detail::Option*, 0, detail::OptionAllocPolicy>; using Option = detail::Option; using BoolOption = detail::BoolOption;
// If '--' is passed, all remaining arguments should be interpreted as the // argument at index 'restArgument'. Defaults to the next unassigned // argument. int restArgument;
Result error(constchar* fmt, ...) MOZ_FORMAT_PRINTF(2, 3);
Result extractValue(size_t argc, char** argv, size_t* i, char** value);
Result handleArg(size_t argc, char** argv, size_t* i, bool* optsAllowed);
Result handleOption(Option* opt, size_t argc, char** argv, size_t* i, bool* optsAllowed);
public: explicit OptionParser(constchar* usage)
: helpOption('h', "help", "Display help information"),
versionOption('v', "version", "Display version information and exit"),
usage(usage),
version(nullptr),
descr(nullptr),
descrWidth(80),
helpWidth(80),
nextArgument(0),
restArgument(-1) {}
~OptionParser();
Result parseArgs(int argc, char** argv);
Result printHelp(constchar* progname);
Result printVersion();
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.