From 39a269657e766aaa6d6c678f95362a566173e547 Mon Sep 17 00:00:00 2001 From: Cole Helbling Date: Tue, 14 May 2024 12:13:40 -0700 Subject: [PATCH] libutil/args: warn on unknown settings after parsing all flags --- doc/manual/rl-next/fix-silent-unknown-options | 28 +++++++++++++++++++ src/libutil/args.cc | 9 +----- 2 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 doc/manual/rl-next/fix-silent-unknown-options diff --git a/doc/manual/rl-next/fix-silent-unknown-options b/doc/manual/rl-next/fix-silent-unknown-options new file mode 100644 index 000000000..0977260ac --- /dev/null +++ b/doc/manual/rl-next/fix-silent-unknown-options @@ -0,0 +1,28 @@ +--- +synopsis: Warn on unknown settings anywhere in the command line +prs: 10701 +--- + +All `nix` commands will now properly warn when an unknown option is specified anywhere in the command line. + +Before: + +```console +$ nix-instantiate --option foobar baz --expr '{}' +warning: unknown setting 'foobar' +$ nix-instantiate '{}' --option foobar baz --expr +$ nix eval --expr '{}' --option foobar baz +{ } +``` + +After: + +```console +$ nix-instantiate --option foobar baz --expr '{}' +warning: unknown setting 'foobar' +$ nix-instantiate '{}' --option foobar baz --expr +warning: unknown setting 'foobar' +$ nix eval --expr '{}' --option foobar baz +warning: unknown setting 'foobar' +{ } +``` diff --git a/src/libutil/args.cc b/src/libutil/args.cc index 243e3a5a6..c202facdf 100644 --- a/src/libutil/args.cc +++ b/src/libutil/args.cc @@ -268,8 +268,6 @@ void RootArgs::parseCmdline(const Strings & _cmdline, bool allowShebang) verbosity = lvlError; } - bool argsSeen = false; - // Heuristic to see if we're invoked as a shebang script, namely, // if we have at least one argument, it's the name of an // executable file, and it starts with "#!". @@ -336,10 +334,6 @@ void RootArgs::parseCmdline(const Strings & _cmdline, bool allowShebang) throw UsageError("unrecognised flag '%1%'", arg); } else { - if (!argsSeen) { - argsSeen = true; - initialFlagsProcessed(); - } pos = rewriteArgs(cmdline, pos); pendingArgs.push_back(*pos++); if (processArgs(pendingArgs, false)) @@ -349,8 +343,7 @@ void RootArgs::parseCmdline(const Strings & _cmdline, bool allowShebang) processArgs(pendingArgs, true); - if (!argsSeen) - initialFlagsProcessed(); + initialFlagsProcessed(); /* Now that we are done parsing, make sure that any experimental * feature required by the flags is enabled */