-
Notifications
You must be signed in to change notification settings - Fork 1
fix(cli): -s セッション名を attach / kill-session で解釈する #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,42 @@ | |
| pub const server = @import("server.zig"); | ||
| pub const session = @import("session.zig"); | ||
|
|
||
| // ─── CLI parsing ────────────────────────────────────────────────────────────── | ||
|
|
||
| const ParsedCli = struct { | ||
| /// From `-s <name>` (last wins if repeated). Not appended to `filtered`. | ||
| session_name: ?[]const u8, | ||
| /// argv with program path stripped, `-s` and its value removed. | ||
| filtered: []const []const u8, | ||
| }; | ||
|
|
||
| /// Strip the executable path, remove `-s <name>` pairs, and record the session name. | ||
| /// Caller must `allocator.free(parsed.filtered)` when done. | ||
| fn parseUserArgs(allocator: std.mem.Allocator, args: []const []const u8) error{ OutOfMemory, MissingSessionNameAfterS }!ParsedCli { | ||
| if (args.len == 0) return .{ .session_name = null, .filtered = try allocator.alloc([]const u8, 0) }; | ||
|
|
||
| var session_name: ?[]const u8 = null; | ||
| var list: std.ArrayListUnmanaged([]const u8) = .{}; | ||
| errdefer list.deinit(allocator); | ||
|
|
||
| var i: usize = 1; | ||
| while (i < args.len) : (i += 1) { | ||
| const arg = args[i]; | ||
| if (std.mem.eql(u8, arg, "-s")) { | ||
| if (i + 1 >= args.len) return error.MissingSessionNameAfterS; | ||
| session_name = args[i + 1]; | ||
| i += 1; | ||
| continue; | ||
| } | ||
| try list.append(allocator, arg); | ||
| } | ||
|
|
||
| return .{ | ||
| .session_name = session_name, | ||
| .filtered = try list.toOwnedSlice(allocator), | ||
| }; | ||
| } | ||
|
|
||
| // ─── Socket path helpers ────────────────────────────────────────────────────── | ||
|
|
||
| /// Resolve `socket_dir` from config, replacing `{uid}` with the real UID. | ||
|
|
@@ -268,10 +304,10 @@ | |
| const envp_buf = try allocator.alloc(?[*:0]const u8, env_slice.len + 1); | ||
| defer allocator.free(envp_buf); | ||
| for (env_slice, 0..) |ptr, i| { | ||
| envp_buf[i] = @ptrCast(ptr); | ||
| } | ||
| envp_buf[env_slice.len] = null; | ||
| const envp: [*:null]const ?[*:0]const u8 = @ptrCast(envp_buf.ptr); | ||
|
|
||
| const pid = try posix.fork(); | ||
| if (pid == 0) { | ||
|
|
@@ -403,27 +439,23 @@ | |
| var cfg = try config.loadFromFile(allocator); | ||
| defer cfg.deinit(); | ||
|
|
||
| // Parse args. | ||
| // Parse args (handles `-s <name>` anywhere; value is not treated as a subcommand arg). | ||
| const args = try std.process.argsAlloc(allocator); | ||
| defer std.process.argsFree(allocator, args); | ||
|
|
||
| // Scan for -s <name> flag anywhere in args. | ||
| var session_name: ?[]const u8 = null; | ||
| var filtered_args: std.ArrayListUnmanaged([]const u8) = .{}; | ||
| defer filtered_args.deinit(allocator); | ||
|
|
||
| for (args, 0..) |arg, i| { | ||
| if (std.mem.eql(u8, arg, "-s") and i + 1 < args.len) { | ||
| session_name = args[i + 1]; | ||
| } else if (i > 0 and i < args.len) { | ||
| // Check if this arg is the value for -s (skip it). | ||
| if (i >= 2 and std.mem.eql(u8, args[i - 1], "-s")) continue; | ||
| try filtered_args.append(allocator, arg); | ||
| } | ||
| } | ||
| const parsed = parseUserArgs(allocator, args) catch |err| switch (err) { | ||
| error.MissingSessionNameAfterS => { | ||
| std.debug.print("error: -s requires a session name\n", .{}); | ||
| return; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| }, | ||
| else => |e| return e, | ||
| }; | ||
| defer allocator.free(parsed.filtered); | ||
| const session_name = parsed.session_name; | ||
| const filtered_args = parsed.filtered; | ||
|
|
||
| const has_subcmd = filtered_args.items.len > 0; | ||
| const subcmd = if (has_subcmd) filtered_args.items[0] else ""; | ||
| const has_subcmd = filtered_args.len > 0; | ||
| const subcmd = if (has_subcmd) filtered_args[0] else ""; | ||
|
|
||
| // No subcommand: create a new session. | ||
| if (!has_subcmd) { | ||
|
|
@@ -437,14 +469,18 @@ | |
|
|
||
| // ── Internal ── | ||
| if (std.mem.eql(u8, subcmd, "--server")) { | ||
| const name = if (filtered_args.items.len > 1) filtered_args.items[1] else "0"; | ||
| const name = if (filtered_args.len > 1) filtered_args[1] else "0"; | ||
| return runServer(allocator, &cfg, name); | ||
| } | ||
|
|
||
| // ── attach / a ── | ||
| if (std.mem.eql(u8, subcmd, "attach") or std.mem.eql(u8, subcmd, "a")) { | ||
| if (filtered_args.items.len > 1) { | ||
| return attachSession(allocator, &cfg, filtered_args.items[1]); | ||
| const name_arg: ?[]const u8 = if (filtered_args.len > 1) | ||
| filtered_args[1] | ||
| else | ||
| session_name; | ||
| if (name_arg) |n| { | ||
| return attachSession(allocator, &cfg, n); | ||
| } | ||
| return attachAuto(allocator, &cfg); | ||
| } | ||
|
|
@@ -459,8 +495,12 @@ | |
|
|
||
| // ── kill-session / k ── | ||
| if (std.mem.eql(u8, subcmd, "kill-session") or std.mem.eql(u8, subcmd, "k")) { | ||
| if (filtered_args.items.len > 1) { | ||
| return killSession(allocator, &cfg, filtered_args.items[1]); | ||
| const name_arg: ?[]const u8 = if (filtered_args.len > 1) | ||
| filtered_args[1] | ||
| else | ||
| session_name; | ||
| if (name_arg) |n| { | ||
| return killSession(allocator, &cfg, n); | ||
| } | ||
| std.debug.print("Usage: zplit kill-session <name>\n", .{}); | ||
| return; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
@@ -553,3 +593,32 @@ | |
|
|
||
| try std.testing.expectEqualStrings("0", name); | ||
| } | ||
|
|
||
| test "parseUserArgs strips -s and keeps subcommand args" { | ||
| const allocator = std.testing.allocator; | ||
| const argv = [_][]const u8{ "/bin/zplit", "attach", "-s", "foo", "extra" }; | ||
| const p = try parseUserArgs(allocator, &argv); | ||
| defer allocator.free(p.filtered); | ||
|
|
||
| try std.testing.expectEqualStrings("foo", p.session_name.?); | ||
| try std.testing.expectEqual(@as(usize, 2), p.filtered.len); | ||
| try std.testing.expectEqualStrings("attach", p.filtered[0]); | ||
| try std.testing.expectEqualStrings("extra", p.filtered[1]); | ||
| } | ||
|
|
||
| test "parseUserArgs attach -s name has positional name for attach" { | ||
| const allocator = std.testing.allocator; | ||
| const argv = [_][]const u8{ "zplit", "attach", "-s", "mysession" }; | ||
| const p = try parseUserArgs(allocator, &argv); | ||
| defer allocator.free(p.filtered); | ||
|
|
||
| try std.testing.expectEqualStrings("mysession", p.session_name.?); | ||
| try std.testing.expectEqual(@as(usize, 1), p.filtered.len); | ||
| try std.testing.expectEqualStrings("attach", p.filtered[0]); | ||
| } | ||
|
|
||
| test "parseUserArgs trailing -s without value errors" { | ||
| const allocator = std.testing.allocator; | ||
| const argv = [_][]const u8{ "zplit", "list", "-s" }; | ||
| try std.testing.expectError(error.MissingSessionNameAfterS, parseUserArgs(allocator, &argv)); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
args.len == 0のチェックは冗長です。後続のwhileループの条件 (i < args.len) とlist.toOwnedSliceによって、引数が空の場合も正しく空のfilteredリストが返されます。また、std.process.argsAllocは通常、実行ファイル名を最初の要素として含むため、args.lenが 0 になることはありません。不要なチェックとアロケーションを省くことができます。