Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/FiveStar.Cli/Archive/CommandArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace FiveStar.Cli.Archive;

/// <summary>
/// The arguments of one command: positional values, <c>--name value</c> options and bare <c>--flag</c> 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 <see cref="CommandLineException"/>.
/// </summary>
internal sealed class CommandArguments
{
Expand Down Expand Up @@ -37,11 +37,13 @@ public CommandArguments(IReadOnlyList<string> args, IReadOnlyCollection<string>
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++;
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/FiveStar.Cli/Archive/CommandLineException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) 2026 Five Star Recomp contributors.
// SPDX-License-Identifier: GPL-3.0-or-later

namespace FiveStar.Cli.Archive;

/// <summary>The command line cannot be used as typed; the message says why.</summary>
internal sealed class CommandLineException : ArgumentException
{
public CommandLineException(string message)
: base(message)
{
}
}
17 changes: 15 additions & 2 deletions src/FiveStar.Cli/Program.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
Expand Down
26 changes: 26 additions & 0 deletions tests/FiveStar.Cli.Tests/CommandArgumentsTests.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>A command whose option lacks its value is refused instead of running without the option.</summary>
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));
}
}