From b934ba863e3cfabb490ca6fa77d09b31aa16dcda Mon Sep 17 00:00:00 2001
From: Vaibhav Gupta <167434497+vaibhavgupta443@users.noreply.github.com>
Date: Fri, 27 Nov 2026 01:07:32 +0530
Subject: [PATCH] Cli: refuse an option that has no value
An option typed without its value was dropped, so the command ran as if it had not been given: archive carve ... --limit-mb carved without a limit, and audio export ... --codec --decode-xma took the flag as the codec.
---
src/FiveStar.Cli/Archive/CommandArguments.cs | 10 ++++---
.../Archive/CommandLineException.cs | 13 ++++++++++
src/FiveStar.Cli/Program.cs | 17 ++++++++++--
.../CommandArgumentsTests.cs | 26 +++++++++++++++++++
4 files changed, 60 insertions(+), 6 deletions(-)
create mode 100644 src/FiveStar.Cli/Archive/CommandLineException.cs
create mode 100644 tests/FiveStar.Cli.Tests/CommandArgumentsTests.cs
diff --git a/src/FiveStar.Cli/Archive/CommandArguments.cs b/src/FiveStar.Cli/Archive/CommandArguments.cs
index 799cdd5c..90c30863 100644
--- a/src/FiveStar.Cli/Archive/CommandArguments.cs
+++ b/src/FiveStar.Cli/Archive/CommandArguments.cs
@@ -7,7 +7,7 @@ namespace FiveStar.Cli.Archive;
///
/// The arguments of one command: positional values, --name value options and bare --flag switches.
-/// Every option takes one value unless it is listed as a flag.
+/// Every option takes one value unless it is listed as a flag; an option without one throws .
///
internal sealed class CommandArguments
{
@@ -37,11 +37,13 @@ public CommandArguments(IReadOnlyList args, IReadOnlyCollection
continue;
}
- if (index + 1 < args.Count)
+ if (index + 1 >= args.Count || args[index + 1].StartsWith(OptionPrefix, StringComparison.Ordinal))
{
- _options[arg] = args[index + 1];
- index++;
+ throw new CommandLineException($"{arg} needs a value");
}
+
+ _options[arg] = args[index + 1];
+ index++;
}
}
diff --git a/src/FiveStar.Cli/Archive/CommandLineException.cs b/src/FiveStar.Cli/Archive/CommandLineException.cs
new file mode 100644
index 00000000..adb2f892
--- /dev/null
+++ b/src/FiveStar.Cli/Archive/CommandLineException.cs
@@ -0,0 +1,13 @@
+// Copyright (c) 2026 Five Star Recomp contributors.
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+namespace FiveStar.Cli.Archive;
+
+/// The command line cannot be used as typed; the message says why.
+internal sealed class CommandLineException : ArgumentException
+{
+ public CommandLineException(string message)
+ : base(message)
+ {
+ }
+}
diff --git a/src/FiveStar.Cli/Program.cs b/src/FiveStar.Cli/Program.cs
index 0c0a8990..1f09c340 100644
--- a/src/FiveStar.Cli/Program.cs
+++ b/src/FiveStar.Cli/Program.cs
@@ -1,6 +1,7 @@
// Copyright (c) 2026 Five Star Recomp contributors.
// SPDX-License-Identifier: GPL-3.0-or-later
+using FiveStar.Cli.Archive;
using FiveStar.Cli.Run;
using FiveStar.Cli.Setup;
@@ -20,9 +21,21 @@ public static int Main(string[] args)
return UsageError;
}
- var rest = args[1..];
+ try
+ {
+ return Dispatch(args[0], args[1..]);
+ }
+ catch (CommandLineException exception)
+ {
+ Console.Error.WriteLine($"fivestar: {exception.Message}");
+
+ return UsageError;
+ }
+ }
- switch (args[0])
+ private static int Dispatch(string group, string[] rest)
+ {
+ switch (group)
{
case "archive":
return ArchiveCommands.Run(rest);
diff --git a/tests/FiveStar.Cli.Tests/CommandArgumentsTests.cs b/tests/FiveStar.Cli.Tests/CommandArgumentsTests.cs
new file mode 100644
index 00000000..8df25e68
--- /dev/null
+++ b/tests/FiveStar.Cli.Tests/CommandArgumentsTests.cs
@@ -0,0 +1,26 @@
+// Copyright (c) 2026 Five Star Recomp contributors.
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+using Xunit;
+
+namespace FiveStar.Cli.Tests;
+
+/// A command whose option lacks its value is refused instead of running without the option.
+public sealed class CommandArgumentsTests
+{
+ private const int UsageError = 2;
+
+ [Theory]
+ [InlineData("--limit-mb")]
+ [InlineData("--limit-mb", "--list")]
+ public void An_option_without_its_value_stops_the_command_before_it_writes(params string[] options)
+ {
+ var output = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
+ string[] arguments = ["archive", "carve", "missing.rpf", output, .. options];
+
+ var exit = Program.Main(arguments);
+
+ Assert.Equal(UsageError, exit);
+ Assert.False(Directory.Exists(output));
+ }
+}