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, ...@@ -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) { if (val < 0 || static_cast<uint64_t>(val) > max || errno != 0) {
PrintF(stderr, PrintF(stderr,
"Error: Value for flag %s of type %s is out of bounds " "Error: Value for flag %s of type %s is out of bounds "
"[0-%" PRIu64 "[0-%" PRIu64 "]\n",
"]\n"
"Try --help for options\n",
arg, Type2String(flag->type()), max); arg, Type2String(flag->type()), max);
return false; return false;
} }
...@@ -427,8 +425,7 @@ int FlagList::SetFlagsFromCommandLine(int* argc, ...@@ -427,8 +425,7 @@ int FlagList::SetFlagsFromCommandLine(int* argc,
// sense there. // sense there.
continue; continue;
} else { } else {
PrintF(stderr, "Error: unrecognized flag %s\n" PrintF(stderr, "Error: unrecognized flag %s\n", arg);
"Try --help for options\n", arg);
return_code = j; return_code = j;
break; break;
} }
...@@ -442,9 +439,8 @@ int FlagList::SetFlagsFromCommandLine(int* argc, ...@@ -442,9 +439,8 @@ int FlagList::SetFlagsFromCommandLine(int* argc,
value = argv[i++]; value = argv[i++];
} }
if (!value) { if (!value) {
PrintF(stderr, "Error: missing value for flag %s of type %s\n" PrintF(stderr, "Error: missing value for flag %s of type %s\n", arg,
"Try --help for options\n", Type2String(flag->type()));
arg, Type2String(flag->type()));
return_code = j; return_code = j;
break; break;
} }
...@@ -501,9 +497,10 @@ int FlagList::SetFlagsFromCommandLine(int* argc, ...@@ -501,9 +497,10 @@ int FlagList::SetFlagsFromCommandLine(int* argc,
flag->type() == Flag::TYPE_MAYBE_BOOL; flag->type() == Flag::TYPE_MAYBE_BOOL;
if ((is_bool_type && value != nullptr) || (!is_bool_type && negated) || if ((is_bool_type && value != nullptr) || (!is_bool_type && negated) ||
*endp != '\0') { *endp != '\0') {
PrintF(stderr, "Error: illegal value for flag %s of type %s\n" // TODO(neis): TryParseUnsigned may return with {*endp == '\0'} even in
"Try --help for options\n", // an error case.
arg, Type2String(flag->type())); PrintF(stderr, "Error: illegal value for flag %s of type %s\n", arg,
Type2String(flag->type()));
if (is_bool_type) { if (is_bool_type) {
PrintF(stderr, PrintF(stderr,
"To set or unset a boolean flag, use --flag or --no-flag.\n"); "To set or unset a boolean flag, use --flag or --no-flag.\n");
...@@ -521,19 +518,28 @@ int FlagList::SetFlagsFromCommandLine(int* argc, ...@@ -521,19 +518,28 @@ int FlagList::SetFlagsFromCommandLine(int* argc,
} }
} }
// shrink the argument list if (FLAG_help) {
PrintHelp();
exit(0);
}
if (remove_flags) { if (remove_flags) {
// shrink the argument list
int j = 1; int j = 1;
for (int i = 1; i < *argc; i++) { for (int i = 1; i < *argc; i++) {
if (argv[i] != nullptr) argv[j++] = argv[i]; if (argv[i] != nullptr) argv[j++] = argv[i];
} }
*argc = j; *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 (return_code != 0) PrintF(stderr, "Try --help for options\n");
if (FLAG_help) {
PrintHelp();
exit(0);
}
return return_code; return return_code;
} }
...@@ -580,10 +586,7 @@ int FlagList::SetFlagsFromString(const char* str, int len) { ...@@ -580,10 +586,7 @@ int FlagList::SetFlagsFromString(const char* str, int len) {
p = SkipWhiteSpace(p); p = SkipWhiteSpace(p);
} }
// set the flags return SetFlagsFromCommandLine(&argc, argv.start(), false);
int result = SetFlagsFromCommandLine(&argc, argv.start(), false);
return result;
} }
......
...@@ -29,11 +29,12 @@ class V8_EXPORT_PRIVATE FlagList { ...@@ -29,11 +29,12 @@ class V8_EXPORT_PRIVATE FlagList {
static std::vector<const char*>* argv(); static std::vector<const char*>* argv();
// Set the flag values by parsing the command line. If remove_flags is // Set the flag values by parsing the command line. If remove_flags is
// set, the flags and associated values are removed from (argc, // set, the recognized flags and associated values are removed from (argc,
// argv). Returns 0 if no error occurred. Otherwise, returns the argv // argv) and only unknown arguments remain. Returns 0 if no error occurred.
// index > 0 for the argument where an error occurred. In that case, // Otherwise, returns the argv index > 0 for the argument where an error
// (argc, argv) will remain unchanged independent of the remove_flags // occurred. In that case, (argc, argv) will remain unchanged independent of
// value, and no assumptions about flag settings should be made. // the remove_flags value, and no assumptions about flag settings should be
// made.
// //
// The following syntax for flags is accepted (both '-' and '--' are ok): // 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