Commit c8679386 authored by Daniel Bevenius's avatar Daniel Bevenius Committed by Commit Bot

[snapshot] enable mksnapshot usage to be displayed

Currently, when specifying '--help' with mksnapshot it will only
print the v8/d8 help message and options and then exit the process.
This means that the usage message from mksnapshot will never be
displayed.

This commit suggests adding an option to SetFlagsFromCommandLine that
can disable this printing and exiting. This allows mksnapshot to display
the usage and print the options after that.

While this works, it does seems a little strange that
SetFlagsFromCommandLine prints the help message and exits the process
but I'm probably missing some background details around this.

Change-Id: I28932adf3478b88b05eed4db70bf74946f8abf2d
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2290852Reviewed-by: 's avatarDan Elphick <delphick@chromium.org>
Reviewed-by: 's avatarUlan Degenbaev <ulan@chromium.org>
Commit-Queue: Dan Elphick <delphick@chromium.org>
Cr-Commit-Position: refs/heads/master@{#68851}
parent 7c10560d
......@@ -845,7 +845,9 @@ void V8::SetFlagsFromString(const char* str, size_t length) {
}
void V8::SetFlagsFromCommandLine(int* argc, char** argv, bool remove_flags) {
i::FlagList::SetFlagsFromCommandLine(argc, argv, remove_flags);
using HelpOptions = i::FlagList::HelpOptions;
i::FlagList::SetFlagsFromCommandLine(argc, argv, remove_flags,
HelpOptions(HelpOptions::kDontExit));
}
RegisteredExtension* RegisteredExtension::first_extension_ = nullptr;
......
......@@ -371,8 +371,8 @@ bool TryParseUnsigned(Flag* flag, const char* arg, const char* value,
}
// static
int FlagList::SetFlagsFromCommandLine(int* argc, char** argv,
bool remove_flags) {
int FlagList::SetFlagsFromCommandLine(int* argc, char** argv, bool remove_flags,
HelpOptions help_options) {
int return_code = 0;
// parse arguments
for (int i = 1; i < *argc;) {
......@@ -482,8 +482,13 @@ int FlagList::SetFlagsFromCommandLine(int* argc, char** argv,
}
if (FLAG_help) {
if (help_options.HasUsage()) {
PrintF(stdout, "%s", help_options.usage());
}
PrintHelp();
exit(0);
if (help_options.ShouldExit()) {
exit(0);
}
}
if (remove_flags) {
......
......@@ -29,13 +29,33 @@ class V8_EXPORT_PRIVATE FlagList {
// as every element of it.
static std::vector<const char*>* argv();
class HelpOptions {
public:
enum ExitBehavior : bool { kExit = true, kDontExit = false };
explicit HelpOptions(ExitBehavior exit_behavior = kExit,
const char* usage = nullptr)
: exit_behavior_(exit_behavior), usage_(usage) {}
bool ShouldExit() { return exit_behavior_ == kExit; }
bool HasUsage() { return usage_ != nullptr; }
const char* usage() { return usage_; }
private:
ExitBehavior exit_behavior_;
const char* usage_;
};
// Set the flag values by parsing the command line. If remove_flags is
// set, the recognized flags and associated values are removed from (argc,
// argv) and only unknown arguments remain. Returns 0 if no error occurred.
// Otherwise, returns the argv index > 0 for the argument where an error
// occurred. In that case, (argc, argv) will remain unchanged independent of
// the remove_flags value, and no assumptions about flag settings should be
// made.
// made. If exit_behavior is set to Exit and --help has been specified on the
// command line, then the usage string will be printed, if it was specified,
// followed by the help flag and then the process will exit. Otherwise the
// flag help will be displayed but execution will continue.
//
// The following syntax for flags is accepted (both '-' and '--' are ok):
//
......@@ -44,7 +64,9 @@ class V8_EXPORT_PRIVATE FlagList {
// --flag=value (non-bool flags only, no spaces around '=')
// --flag value (non-bool flags only)
// -- (capture all remaining args in JavaScript)
static int SetFlagsFromCommandLine(int* argc, char** argv, bool remove_flags);
static int SetFlagsFromCommandLine(
int* argc, char** argv, bool remove_flags,
FlagList::HelpOptions help_options = FlagList::HelpOptions());
// Set the flag values by parsing the string str. Splits string into argc
// substrings argv[], each of which consisting of non-white-space chars,
......
......@@ -217,12 +217,17 @@ int main(int argc, char** argv) {
// Print the usage if an error occurs when parsing the command line
// flags or if the help flag is set.
int result = i::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
if (result > 0 || (argc > 3) || i::FLAG_help) {
::printf("Usage: %s --startup_src=... --startup_blob=... [extras]\n",
argv[0]);
i::FlagList::PrintHelp();
return !i::FLAG_help;
using HelpOptions = i::FlagList::HelpOptions;
std::string usage = "Usage: " + std::string(argv[0]) +
" [--startup-src=file]" + " [--startup-blob=file]" +
" [--embedded-src=file]" + " [--embedded-variant=label]" +
" [--target-arch=arch]" +
" [--target-os=os] [extras]\n\n";
int result = i::FlagList::SetFlagsFromCommandLine(
&argc, argv, true, HelpOptions(HelpOptions::kExit, usage.c_str()));
if (result > 0 || (argc > 3)) {
i::PrintF(stdout, "%s", usage.c_str());
return result;
}
i::CpuFeatures::Probe(true);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment