diff --git a/js/src/shell/js.cpp b/js/src/shell/js.cpp index 344e3b01b7a..9c483c8c36b 100644 --- a/js/src/shell/js.cpp +++ b/js/src/shell/js.cpp @@ -6245,7 +6245,7 @@ main(int argc, char **argv, char **envp) op.setArgCapturesRest("scriptArgs"); switch (op.parseArgs(argc, argv)) { - case OptionParser::ParseHelp: + case OptionParser::EarlyExit: return EXIT_SUCCESS; case OptionParser::ParseError: op.printHelp(argv[0]); diff --git a/js/src/shell/jsoptparse.cpp b/js/src/shell/jsoptparse.cpp index d6a9ad14491..8bec962396e 100644 --- a/js/src/shell/jsoptparse.cpp +++ b/js/src/shell/jsoptparse.cpp @@ -185,8 +185,8 @@ OptionParser::printHelp(const char *progname) putchar('\n'); } - if (ver) - printf("\nVersion: %s\n\n", ver); + if (version) + printf("\nVersion: %s\n\n", version); if (!arguments.empty()) { printf("Arguments:\n"); @@ -250,7 +250,15 @@ OptionParser::printHelp(const char *progname) } } - return ParseHelp; + return EarlyExit; +} + +OptionParser::Result +OptionParser::printVersion() +{ + MOZ_ASSERT(version); + printf("%s\n", version); + return EarlyExit; } OptionParser::Result @@ -284,6 +292,8 @@ OptionParser::handleOption(Option *opt, size_t argc, char **argv, size_t *i, boo { if (opt == &helpOption) return printHelp(argv[0]); + if (opt == &versionOption) + return printVersion(); opt->asBoolOption()->value = true; return Okay; } @@ -473,6 +483,9 @@ OptionParser::findOption(char shortflag) return *it; } + if (versionOption.shortflag == shortflag) + return &versionOption; + return helpOption.shortflag == shortflag ? &helpOption : nullptr; } @@ -503,6 +516,9 @@ OptionParser::findOption(const char *longflag) no_match:; } + if (strcmp(versionOption.longflag, longflag) == 0) + return &versionOption; + return strcmp(helpOption.longflag, longflag) ? nullptr : &helpOption; } diff --git a/js/src/shell/jsoptparse.h b/js/src/shell/jsoptparse.h index 04ef5ae265d..d68fe457d54 100644 --- a/js/src/shell/jsoptparse.h +++ b/js/src/shell/jsoptparse.h @@ -193,7 +193,8 @@ class OptionParser Okay = 0, Fail, /* As in, allocation fail. */ ParseError, /* Successfully parsed but with an error. */ - ParseHelp /* Aborted on help flag. */ + EarlyExit /* Successfully parsed but exits the program, + * for example with --help and --version. */ }; private: @@ -204,8 +205,9 @@ class OptionParser Options options; Options arguments; BoolOption helpOption; + BoolOption versionOption; const char *usage; - const char *ver; + const char *version; const char *descr; size_t descrWidth; size_t helpWidth; @@ -234,7 +236,8 @@ class OptionParser public: explicit OptionParser(const char *usage) : helpOption('h', "help", "Display help information"), - usage(usage), ver(nullptr), descr(nullptr), descrWidth(80), helpWidth(80), + versionOption('v', "version", "Display version information and exit"), + usage(usage), version(nullptr), descr(nullptr), descrWidth(80), helpWidth(80), nextArgument(0), restArgument(-1) {} @@ -242,10 +245,11 @@ class OptionParser Result parseArgs(int argc, char **argv); Result printHelp(const char *progname); + Result printVersion(); /* Metadata */ - void setVersion(const char *version) { ver = version; } + void setVersion(const char *v) { version = v; } void setHelpWidth(size_t width) { helpWidth = width; } void setDescriptionWidth(size_t width) { descrWidth = width; } void setDescription(const char *description) { descr = description; }