Commit 5330111d authored by Georg Neis's avatar Georg Neis Committed by Commit Bot

[flags] Print flags that were ignored.

Command-line flags can be parsed in two modes. In the mode used by
Chrome, an unrecognized flag causes the remaining arguments to be
ignored. This is different from how d8 parses flags.

Example:

1) d8 --enable-slow-asserts --trace-ic
2) content_shell --js-flags='--enable-slow-asserts --trace-ic'

Assuming we compiled without ENABLE_SLOW_DCHECKS, in (1) we get a
warning that --enable-slow-asserts is unknown. Nevertheless,
--trace-ic will be enabled. In (2), we get an error that
--enable-slow-asserts is unknown but --trace-ic will NOT be enabled
(and neither does content_shell abort).

This inconsistency is obviously very confusing. With this CL, we
will at least print any flags that got ignored.

Change-Id: I22bdb06d2b0accc234b3f5d596458809de364bce
Reviewed-on: https://chromium-review.googlesource.com/1066010
Commit-Queue: Georg Neis <neis@chromium.org>
Reviewed-by: 's avatarSigurd Schneider <sigurds@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53261}
parent b4ebbc57
......@@ -389,9 +389,7 @@ bool TryParseUnsigned(Flag* flag, const char* arg, const char* value,
if (val < 0 || static_cast<uint64_t>(val) > max || errno != 0) {
PrintF(stderr,
"Error: Value for flag %s of type %s is out of bounds "
"[0-%" PRIu64
"]\n"
"Try --help for options\n",
"[0-%" PRIu64 "]\n",
arg, Type2String(flag->type()), max);
return false;
}
......@@ -427,8 +425,7 @@ int FlagList::SetFlagsFromCommandLine(int* argc,
// sense there.
continue;
} else {
PrintF(stderr, "Error: unrecognized flag %s\n"
"Try --help for options\n", arg);
PrintF(stderr, "Error: unrecognized flag %s\n", arg);
return_code = j;
break;
}
......@@ -442,9 +439,8 @@ int FlagList::SetFlagsFromCommandLine(int* argc,
value = argv[i++];
}
if (!value) {
PrintF(stderr, "Error: missing value for flag %s of type %s\n"
"Try --help for options\n",
arg, Type2String(flag->type()));
PrintF(stderr, "Error: missing value for flag %s of type %s\n", arg,
Type2String(flag->type()));
return_code = j;
break;
}
......@@ -501,9 +497,10 @@ int FlagList::SetFlagsFromCommandLine(int* argc,
flag->type() == Flag::TYPE_MAYBE_BOOL;
if ((is_bool_type && value != nullptr) || (!is_bool_type && negated) ||
*endp != '\0') {
PrintF(stderr, "Error: illegal value for flag %s of type %s\n"
"Try --help for options\n",
arg, Type2String(flag->type()));
// TODO(neis): TryParseUnsigned may return with {*endp == '\0'} even in
// an error case.
PrintF(stderr, "Error: illegal value for flag %s of type %s\n", arg,
Type2String(flag->type()));
if (is_bool_type) {
PrintF(stderr,
"To set or unset a boolean flag, use --flag or --no-flag.\n");
......@@ -521,19 +518,28 @@ int FlagList::SetFlagsFromCommandLine(int* argc,
}
}
// shrink the argument list
if (FLAG_help) {
PrintHelp();
exit(0);
}
if (remove_flags) {
// shrink the argument list
int j = 1;
for (int i = 1; i < *argc; i++) {
if (argv[i] != nullptr) argv[j++] = argv[i];
}
*argc = j;
} else if (return_code != 0) {
if (return_code + 1 < *argc) {
PrintF(stderr, "The remaining arguments were ignored:");
for (int i = return_code + 1; i < *argc; ++i) {
PrintF(stderr, " %s", argv[i]);
}
PrintF(stderr, "\n");
}
}
if (FLAG_help) {
PrintHelp();
exit(0);
}
if (return_code != 0) PrintF(stderr, "Try --help for options\n");
return return_code;
}
......@@ -580,10 +586,7 @@ int FlagList::SetFlagsFromString(const char* str, int len) {
p = SkipWhiteSpace(p);
}
// set the flags
int result = SetFlagsFromCommandLine(&argc, argv.start(), false);
return result;
return SetFlagsFromCommandLine(&argc, argv.start(), false);
}
......
......@@ -29,11 +29,12 @@ class V8_EXPORT_PRIVATE FlagList {
static std::vector<const char*>* argv();
// Set the flag values by parsing the command line. If remove_flags is
// set, the flags and associated values are removed from (argc,
// argv). 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.
// 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.
//
// The following syntax for flags is accepted (both '-' and '--' are ok):
//
......
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