| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef shell_jsoptparse_h |
| 8 |
#define shell_jsoptparse_h |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
|
| 12 |
#include "js/AllocPolicy.h" |
| 13 |
#include "js/Utility.h" |
| 14 |
#include "js/Vector.h" |
| 15 |
|
| 16 |
namespace js { |
| 17 |
namespace cli { |
| 18 |
|
| 19 |
namespace detail { |
| 20 |
|
| 21 |
// We want to use the shell's option parser before initializing the JS engine. |
| 22 |
// The JS malloc arena isn't available yet at this point, so we use a custom |
| 23 |
// allocation policy that uses the system malloc instead. |
| 24 |
class OptionAllocPolicy { |
| 25 |
public: |
| 26 |
template <typename T> |
| 27 |
T* pod_malloc(size_t numElems) { |
| 28 |
size_t bytes; |
| 29 |
if (MOZ_UNLIKELY(!js::CalculateAllocSize<T>(numElems, &bytes))) { |
| 30 |
return nullptr; |
| 31 |
} |
| 32 |
return static_cast<T*>(malloc(bytes)); |
| 33 |
} |
| 34 |
|
| 35 |
template <typename T> |
| 36 |
T* pod_realloc(T* p, size_t oldSize, size_t newSize) { |
| 37 |
size_t bytes; |
| 38 |
if (MOZ_UNLIKELY(!js::CalculateAllocSize<T>(newSize, &bytes))) { |
| 39 |
return nullptr; |
| 40 |
} |
| 41 |
return static_cast<T*>(realloc(p, bytes)); |
| 42 |
} |
| 43 |
|
| 44 |
void reportAllocOverflow() const {} |
| 45 |
bool checkSimulatedOOM() const { return !js::oom::ShouldFailWithOOM(); } |
| 46 |
|
| 47 |
template <typename T> |
| 48 |
void free_(T* p, size_t numElems = 0) { |
| 49 |
free(p); |
| 50 |
} |
| 51 |
}; |
| 52 |
|
| 53 |
struct BoolOption; |
| 54 |
struct MultiStringOption; |
| 55 |
struct ValuedOption; |
| 56 |
struct StringOption; |
| 57 |
struct IntOption; |
| 58 |
|
| 59 |
enum OptionKind { |
| 60 |
OptionKindBool, |
| 61 |
OptionKindString, |
| 62 |
OptionKindInt, |
| 63 |
OptionKindMultiString, |
| 64 |
OptionKindInvalid |
| 65 |
}; |
| 66 |
|
| 67 |
struct Option { |
| 68 |
const char* longflag; |
| 69 |
const char* help; |
| 70 |
OptionKind kind; |
| 71 |
char shortflag; |
| 72 |
bool terminatesOptions = false; |
| 73 |
bool ignoresUnknownOptions = false; |
| 74 |
|
| 75 |
Option(OptionKind kind, char shortflag, const char* longflag, |
| 76 |
const char* help) |
| 77 |
: longflag(longflag), help(help), kind(kind), shortflag(shortflag) {} |
| 78 |
|
| 79 |
virtual ~Option() = 0; |
| 80 |
|
| 81 |
void setTerminatesOptions(bool enabled) { terminatesOptions = enabled; } |
| 82 |
bool getTerminatesOptions() const { return terminatesOptions; } |
| 83 |
|
| 84 |
void setIgnoresUnknownOptions(bool enabled) { |
| 85 |
ignoresUnknownOptions = enabled; |
| 86 |
} |
| 87 |
bool getIgnoresUnknownOptions() const { return ignoresUnknownOptions; } |
| 88 |
|
| 89 |
virtual bool isValued() const { return false; } |
| 90 |
|
| 91 |
/* Only some valued options are variadic (like MultiStringOptions). */ |
| 92 |
virtual bool isVariadic() const { return false; } |
| 93 |
|
| 94 |
/* |
| 95 |
* For arguments, the shortflag field is used to indicate whether the |
| 96 |
* argument is optional. |
| 97 |
*/ |
| 98 |
bool isOptional() { return shortflag; } |
| 99 |
|
| 100 |
void setFlagInfo(char shortflag, const char* longflag, const char* help) { |
| 101 |
this->shortflag = shortflag; |
| 102 |
this->longflag = longflag; |
| 103 |
this->help = help; |
| 104 |
} |
| 105 |
|
| 106 |
ValuedOption* asValued(); |
| 107 |
const ValuedOption* asValued() const; |
| 108 |
|
| 109 |
#define OPTION_CONVERT_DECL(__cls) \ |
| 110 |
bool is##__cls##Option() const; \ |
| 111 |
__cls##Option* as##__cls##Option(); \ |
| 112 |
const __cls##Option* as##__cls##Option() const; |
| 113 |
|
| 114 |
OPTION_CONVERT_DECL(Bool) |
| 115 |
OPTION_CONVERT_DECL(String) |
| 116 |
OPTION_CONVERT_DECL(Int) |
| 117 |
OPTION_CONVERT_DECL(MultiString) |
| 118 |
}; |
| 119 |
|
| 120 |
inline Option::~Option() {} |
| 121 |
|
| 122 |
struct BoolOption : public Option { |
| 123 |
size_t argno; |
| 124 |
bool value; |
| 125 |
|
| 126 |
BoolOption(char shortflag, const char* longflag, const char* help) |
| 127 |
: Option(OptionKindBool, shortflag, longflag, help), value(false) {} |
| 128 |
|
| 129 |
virtual ~BoolOption() {} |
| 130 |
}; |
| 131 |
|
| 132 |
struct ValuedOption : public Option { |
| 133 |
const char* metavar; |
| 134 |
|
| 135 |
ValuedOption(OptionKind kind, char shortflag, const char* longflag, |
| 136 |
const char* help, const char* metavar) |
| 137 |
: Option(kind, shortflag, longflag, help), metavar(metavar) {} |
| 138 |
|
| 139 |
virtual ~ValuedOption() = 0; |
| 140 |
virtual bool isValued() const override { return true; } |
| 141 |
}; |
| 142 |
|
| 143 |
inline ValuedOption::~ValuedOption() {} |
| 144 |
|
| 145 |
struct StringOption : public ValuedOption { |
| 146 |
const char* value; |
| 147 |
|
| 148 |
StringOption(char shortflag, const char* longflag, const char* help, |
| 149 |
const char* metavar) |
| 150 |
: ValuedOption(OptionKindString, shortflag, longflag, help, metavar), |
| 151 |
value(nullptr) {} |
| 152 |
|
| 153 |
virtual ~StringOption() {} |
| 154 |
}; |
| 155 |
|
| 156 |
struct IntOption : public ValuedOption { |
| 157 |
int value; |
| 158 |
|
| 159 |
IntOption(char shortflag, const char* longflag, const char* help, |
| 160 |
const char* metavar, int defaultValue) |
| 161 |
: ValuedOption(OptionKindInt, shortflag, longflag, help, metavar), |
| 162 |
value(defaultValue) {} |
| 163 |
|
| 164 |
virtual ~IntOption() {} |
| 165 |
}; |
| 166 |
|
| 167 |
struct StringArg { |
| 168 |
char* value; |
| 169 |
size_t argno; |
| 170 |
|
| 171 |
StringArg(char* value, size_t argno) : value(value), argno(argno) {} |
| 172 |
}; |
| 173 |
|
| 174 |
struct MultiStringOption : public ValuedOption { |
| 175 |
Vector<StringArg, 0, detail::OptionAllocPolicy> strings; |
| 176 |
|
| 177 |
MultiStringOption(char shortflag, const char* longflag, const char* help, |
| 178 |
const char* metavar) |
| 179 |
: ValuedOption(OptionKindMultiString, shortflag, longflag, help, |
| 180 |
metavar) {} |
| 181 |
|
| 182 |
virtual ~MultiStringOption() {} |
| 183 |
|
| 184 |
virtual bool isVariadic() const override { return true; } |
| 185 |
}; |
| 186 |
|
| 187 |
} /* namespace detail */ |
| 188 |
|
| 189 |
class MultiStringRange { |
| 190 |
using StringArg = detail::StringArg; |
| 191 |
const StringArg* cur; |
| 192 |
const StringArg* end; |
| 193 |
|
| 194 |
public: |
| 195 |
explicit MultiStringRange(const StringArg* cur, const StringArg* end) |
| 196 |
: cur(cur), end(end) { |
| 197 |
MOZ_ASSERT(end - cur >= 0); |
| 198 |
} |
| 199 |
|
| 200 |
bool empty() const { return cur == end; } |
| 201 |
void popFront() { |
| 202 |
MOZ_ASSERT(!empty()); |
| 203 |
++cur; |
| 204 |
} |
| 205 |
char* front() const { |
| 206 |
MOZ_ASSERT(!empty()); |
| 207 |
return cur->value; |
| 208 |
} |
| 209 |
size_t argno() const { |
| 210 |
MOZ_ASSERT(!empty()); |
| 211 |
return cur->argno; |
| 212 |
} |
| 213 |
}; |
| 214 |
|
| 215 |
/* |
| 216 |
* Builder for describing a command line interface and parsing the resulting |
| 217 |
* specification. |
| 218 |
* |
| 219 |
* - A multi-option is an option that can appear multiple times and still |
| 220 |
* parse as valid command line arguments. |
| 221 |
* - An "optional argument" is supported for backwards compatibility with prior |
| 222 |
* command line interface usage. Once one optional argument has been added, |
| 223 |
* *only* optional arguments may be added. |
| 224 |
*/ |
| 225 |
class OptionParser { |
| 226 |
public: |
| 227 |
enum Result { |
| 228 |
Okay = 0, |
| 229 |
Fail, /* As in, allocation fail. */ |
| 230 |
ParseError, /* Successfully parsed but with an error. */ |
| 231 |
EarlyExit /* Successfully parsed but exits the program, |
| 232 |
* for example with --help and --version. */ |
| 233 |
}; |
| 234 |
|
| 235 |
private: |
| 236 |
using Options = Vector<detail::Option*, 0, detail::OptionAllocPolicy>; |
| 237 |
using Option = detail::Option; |
| 238 |
using BoolOption = detail::BoolOption; |
| 239 |
|
| 240 |
Options options; |
| 241 |
Options arguments; |
| 242 |
BoolOption helpOption; |
| 243 |
BoolOption versionOption; |
| 244 |
const char* usage; |
| 245 |
const char* version; |
| 246 |
const char* descr; |
| 247 |
size_t descrWidth; |
| 248 |
size_t helpWidth; |
| 249 |
size_t nextArgument; |
| 250 |
|
| 251 |
// If '--' is passed, all remaining arguments should be interpreted as the |
| 252 |
// argument at index 'restArgument'. Defaults to the next unassigned |
| 253 |
// argument. |
| 254 |
int restArgument; |
| 255 |
|
| 256 |
Option* findOption(char shortflag); |
| 257 |
const Option* findOption(char shortflag) const; |
| 258 |
const Option* tryFindOption(char shortflag) const; |
| 259 |
Option* findOption(const char* longflag); |
| 260 |
const Option* findOption(const char* longflag) const; |
| 261 |
const Option* tryFindOption(const char* longflag) const; |
| 262 |
int findArgumentIndex(const char* name) const; |
| 263 |
Option* findArgument(const char* name); |
| 264 |
const Option* findArgument(const char* name) const; |
| 265 |
|
| 266 |
Result error(const char* fmt, ...) MOZ_FORMAT_PRINTF(2, 3); |
| 267 |
Result extractValue(size_t argc, char** argv, size_t* i, char** value); |
| 268 |
Result handleArg(size_t argc, char** argv, size_t* i, bool* optsAllowed); |
| 269 |
Result handleOption(Option* opt, size_t argc, char** argv, size_t* i, |
| 270 |
bool* optsAllowed); |
| 271 |
|
| 272 |
public: |
| 273 |
explicit OptionParser(const char* usage) |
| 274 |
: helpOption('h', "help", "Display help information"), |
| 275 |
versionOption('v', "version", "Display version information and exit"), |
| 276 |
usage(usage), |
| 277 |
version(nullptr), |
| 278 |
descr(nullptr), |
| 279 |
descrWidth(80), |
| 280 |
helpWidth(80), |
| 281 |
nextArgument(0), |
| 282 |
restArgument(-1) {} |
| 283 |
|
| 284 |
~OptionParser(); |
| 285 |
|
| 286 |
Result parseArgs(int argc, char** argv); |
| 287 |
Result printHelp(const char* progname); |
| 288 |
Result printVersion(); |
| 289 |
|
| 290 |
/* Metadata */ |
| 291 |
|
| 292 |
void setVersion(const char* v) { version = v; } |
| 293 |
void setHelpWidth(size_t width) { helpWidth = width; } |
| 294 |
void setDescriptionWidth(size_t width) { descrWidth = width; } |
| 295 |
void setDescription(const char* description) { descr = description; } |
| 296 |
void setHelpOption(char shortflag, const char* longflag, const char* help); |
| 297 |
void setArgTerminatesOptions(const char* name, bool enabled); |
| 298 |
void setIgnoresUnknownOptions(const char* name, bool enabled); |
| 299 |
void setArgCapturesRest(const char* name); |
| 300 |
|
| 301 |
/* Arguments: no further arguments may be added after a variadic argument. */ |
| 302 |
|
| 303 |
bool addOptionalStringArg(const char* name, const char* help); |
| 304 |
bool addOptionalMultiStringArg(const char* name, const char* help); |
| 305 |
|
| 306 |
const char* getStringArg(const char* name) const; |
| 307 |
MultiStringRange getMultiStringArg(const char* name) const; |
| 308 |
|
| 309 |
/* Options */ |
| 310 |
|
| 311 |
bool addBoolOption(char shortflag, const char* longflag, const char* help); |
| 312 |
bool addStringOption(char shortflag, const char* longflag, const char* help, |
| 313 |
const char* metavar); |
| 314 |
bool addIntOption(char shortflag, const char* longflag, const char* help, |
| 315 |
const char* metavar, int defaultValue); |
| 316 |
bool addMultiStringOption(char shortflag, const char* longflag, |
| 317 |
const char* help, const char* metavar); |
| 318 |
bool addOptionalVariadicArg(const char* name); |
| 319 |
|
| 320 |
int getIntOption(char shortflag) const; |
| 321 |
int getIntOption(const char* longflag) const; |
| 322 |
const char* getStringOption(char shortflag) const; |
| 323 |
const char* getStringOption(const char* longflag) const; |
| 324 |
bool getBoolOption(char shortflag) const; |
| 325 |
bool getBoolOption(const char* longflag) const; |
| 326 |
MultiStringRange getMultiStringOption(char shortflag) const; |
| 327 |
MultiStringRange getMultiStringOption(const char* longflag) const; |
| 328 |
|
| 329 |
/* |
| 330 |
* Return whether the help option was present (and thus help was already |
| 331 |
* displayed during parse_args). |
| 332 |
*/ |
| 333 |
bool getHelpOption() const; |
| 334 |
}; |
| 335 |
|
| 336 |
} /* namespace cli */ |
| 337 |
} /* namespace js */ |
| 338 |
|
| 339 |
#endif /* shell_jsoptparse_h */ |