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)); + } +}