╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ command.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
248 const digits = if (shift == 0) value else value[0 .. value.len - 1]; 249 const count = std.fmt.parseInt(u64, digits, 10) catch return error.InvalidSize; 250 return std.math.shlExact(u64, count, shift) catch error.InvalidSize; 251 } 252 253 /// parses the given args into a command if valid, and determines how it should be run 254 /// (via the TUI or CLI). 255 pub const CommandDispatch = union(enum) { 256 invalid: union(enum) { 257 command: []const u8, 258 argument: struct { 259 command: ?CommandKind, 260 value: []const u8, 261 }, 262 }, 263 help: ?CommandKind, 264 local, 265 cli: Command, 266 267 pub fn init(cmd_args: *CommandArgs) !CommandDispatch { 268 const dispatch = try initIgnoreUnused(cmd_args); 269 if (cmd_args.unused_args.count() > 0) { 270 return .{ 271 .invalid = .{ 272 .argument = .{ 273 .command = switch (dispatch) { 274 .invalid => return dispatch, // if there was already an error, return it instead 275 .help => |cmd_kind_maybe| cmd_kind_maybe, 276 .local => null, 277 .cli => |command| command, 278 }, 279 .value = cmd_args.unused_args.keys()[0], 280 }, 281 }, 282 }; 283 } 284 return dispatch; 285 } 286 287 pub fn initIgnoreUnused(cmd_args: *CommandArgs) !CommandDispatch { 288 const show_help = cmd_args.contains("--help"); 289 290 if (cmd_args.command_kind) |command_kind| { 291 if (show_help) { 292 return .{ .help = command_kind }; 293 } else if (try Command.initMaybe(cmd_args)) |cmd| { 294 return .{ .cli = cmd }; 295 } else { 296 return .{ .help = command_kind }; 297 } 298 } else if (cmd_args.command_name) |command_name| { 299 return .{ .invalid = .{ .command = command_name } }; 300 } else if (show_help) { 301 return .{ .help = null }; 302 } else { 303 return .local; 304 } 305 } 306 }; 307