From b11b5871e94b732a7bf83000fb6898f07c09a31b Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 30 Oct 2024 20:56:38 -0400 Subject: [PATCH] cli: do not parse actions (+command) after -e Fixes #2506 --- src/cli/action.zig | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/cli/action.zig b/src/cli/action.zig index 950577158..1da0c0609 100644 --- a/src/cli/action.zig +++ b/src/cli/action.zig @@ -71,6 +71,13 @@ pub const Action = enum { var pending_help: bool = false; var pending: ?Action = null; while (iter.next()) |arg| { + // If we see a "-e" and we haven't seen a command yet, then + // we are done looking for commands. This special case enables + // `ghostty -e ghostty +command`. If we've seen a command we + // still want to keep looking because + // `ghostty +command -e +command` is invalid. + if (std.mem.eql(u8, arg, "-e") and pending == null) return null; + // Special case, --version always outputs the version no // matter what, no matter what other args exist. if (std.mem.eql(u8, arg, "--version")) return .version; @@ -240,3 +247,30 @@ test "parse action plus" { try testing.expect(action.? == .version); } } + +test "parse action plus ignores -e" { + const testing = std.testing; + const alloc = testing.allocator; + + { + var iter = try std.process.ArgIteratorGeneral(.{}).init( + alloc, + "--a=42 -e +version", + ); + defer iter.deinit(); + const action = try Action.detectIter(&iter); + try testing.expect(action == null); + } + + { + var iter = try std.process.ArgIteratorGeneral(.{}).init( + alloc, + "+list-fonts --a=42 -e +version", + ); + defer iter.deinit(); + try testing.expectError( + Action.Error.MultipleActions, + Action.detectIter(&iter), + ); + } +}