╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ command.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
245 options.service = val_maybe orelse return error.ServiceNeedsValue; 246 } 247 options.dir = if (cmd_args.positional_args.len == 1) cmd_args.positional_args[0] else null; 248 249 return .{ .ssh_git = options }; 250 }, 251 .ssh_tui => { 252 if (cmd_args.positional_args.len != 0) return null; 253 254 var options: ssh_tui.Options = .{}; 255 256 if (cmd_args.get("--tui-connect")) |val_maybe| { 257 options.tui_connect = val_maybe orelse return error.TuiConnectNeedsValue; 258 } 259 if (cmd_args.get("--user-key")) |val_maybe| { 260 options.user_key = val_maybe orelse return error.UserKeyNeedsValue; 261 } 262 263 return .{ .ssh_tui = options }; 264 }, 265 } 266 } 267 }; 268 269 /// parses the given args into a command if valid, and determines how it should be run 270 /// (via the TUI or CLI). 271 pub const CommandDispatch = union(enum) { 272 invalid: union(enum) { 273 command: []const u8, 274 argument: struct { 275 command: ?CommandKind, 276 value: []const u8, 277 }, 278 }, 279 help: ?CommandKind, 280 cli: Command, 281 282 pub fn init(cmd_args: *CommandArgs) !CommandDispatch { 283 const dispatch = try initIgnoreUnused(cmd_args); 284 if (cmd_args.unused_args.count() > 0) { 285 return .{ 286 .invalid = .{ 287 .argument = .{ 288 .command = switch (dispatch) { 289 .invalid => return dispatch, // if there was already an error, return it instead 290 .help => |cmd_kind_maybe| cmd_kind_maybe, 291 .cli => |command| command, 292 }, 293 .value = cmd_args.unused_args.keys()[0], 294 }, 295 }, 296 }; 297 } 298 return dispatch; 299 } 300 301 pub fn initIgnoreUnused(cmd_args: *CommandArgs) !CommandDispatch { 302 const show_help = cmd_args.contains("--help"); 303 304 if (cmd_args.command_kind) |command_kind| { 305 if (show_help) { 306 return .{ .help = command_kind }; 307 } else if (try Command.initMaybe(cmd_args)) |cmd| { 308 return .{ .cli = cmd }; 309 } else { 310 return .{ .help = command_kind }; 311 } 312 } else if (cmd_args.command_name) |command_name| { 313 return .{ .invalid = .{ .command = command_name } }; 314 } else if (show_help) { 315 return .{ .help = null }; 316 } else { 317 return .{ .help = null }; 318 } 319 } 320 }; 321