File size: 79,898 Bytes
6648d93
1
2
{"repo_name": "zli", "file_name": "/zli/src/zli.zig", "inference_info": {"prefix_code": "const std = @import(\"std\");\nconst Spinner = @import(\"lib/spin.zig\");\npub const SpinnerStyles = Spinner.SpinnerStyles;\nconst builtin = @import(\"lib/builtin.zig\");\npub const styles = builtin.styles;\n\nconst Writer = @TypeOf(std.io.getStdOut().writer());\n\n/// FlagType represents the type of a flag, can be a boolean, integer, or string.\npub const FlagType = enum {\n    Bool,\n    Int,\n    String,\n};\n\n/// FlagValue represents the value of a flag, can be a boolean, integer, or string.\npub const FlagValue = union(FlagType) {\n    Bool: bool,\n    Int: i32,\n    String: []const u8,\n};\n\n/// Flag represents a flag for a command, example: \"--verbose\". Can be used such as \"--verbose true\" or \"--verbose=22\", or just \"--verbose\".\npub const Flag = struct {\n    name: []const u8,\n    shortcut: ?[]const u8 = null,\n    description: []const u8,\n    type: FlagType,\n    default_value: FlagValue,\n    hidden: bool = false,\n\n    fn evaluateValueType(self: *const Flag, value: []const u8) !FlagValue {\n        return switch (self.type) {\n            .Bool => {\n                if (std.mem.eql(u8, value, \"true\")) return FlagValue{ .Bool = true };\n                if (std.mem.eql(u8, value, \"false\")) return FlagValue{ .Bool = false };\n                return error.InvalidBooleanValue;\n            },\n            .Int => FlagValue{ .Int = try std.fmt.parseInt(i32, value, 10) },\n            .String => FlagValue{ .String = value },\n        };\n    }\n\n    fn safeEvaluate(self: *const Flag, value: []const u8) !FlagValue {\n        return self.evaluateValueType(value) catch {\n            return error.InvalidFlagValue;\n        };\n    }\n};\n\n/// PositionalArg represents a positional argument for a command, example:\"cli open file.txt\".\npub const PositionalArg = struct {\n    name: []const u8,\n    description: []const u8,\n    required: bool,\n    variadic: bool = false,\n};\n\n/// CommandContext represents the context of a command execution. Powerful!\npub const CommandContext = struct {\n    root: *Command,\n    direct_parent: *Command,\n    command: *Command,\n    allocator: std.mem.Allocator,\n    positional_args: []const []const u8,\n    spinner: *Spinner,\n    data: ?*anyopaque = null,\n\n    // TODO: fix panic: integer cast truncated bits - later im tired\n    pub fn flag(self: *const CommandContext, flag_name: []const u8, comptime T: type) T {\n        if (self.command.flag_values.get(flag_name)) |val| {\n            return switch (val) {\n                .Bool => |b| if (T == bool) b else getDefaultValue(T),\n                .Int => |i| if (@typeInfo(T) == .int) @as(T, @intCast(i)) else getDefaultValue(T),\n                .String => |s| if (T == []const u8) s else getDefaultValue(T),\n            };\n        }\n\n        if (self.command.findFlag(flag_name)) |found_flag| {\n            return switch (found_flag.default_value) {\n                .Bool => |b| if (T == bool) b else getDefaultValue(T),\n                .Int => |i| if (@typeInfo(T) == .int) @as(T, @intCast(i)) else getDefaultValue(T),\n                .String => |s| if (T == []const u8) s else getDefaultValue(T),\n            };\n        }\n\n        // Should be unreachable if all flags have defaults and validation is correct.\n        unreachable;\n    }\n\n    fn getDefaultValue(comptime T: type) T {\n        return switch (@typeInfo(T)) {\n            .bool => false,\n            .int => 0,\n            .pointer => |ptr_info| if (ptr_info.child == u8) \"\" else @compileError(\"Unsupported pointer type\"),\n            else => @compileError(\"Unsupported type for flag\"),\n        };\n    }\n\n    pub fn getArg(self: *const CommandContext, name: []const u8) ?[]const u8 {\n        const spec = self.command.positional_args.items;\n        for (spec, 0..) |arg, i| {\n            if (std.mem.eql(u8, arg.name, name)) {\n                if (i < self.positional_args.len) return self.positional_args[i];\n                return null; // not provided\n            }\n        }\n        return null; // not defined\n    }\n\n    pub fn getContextData(self: *const CommandContext, comptime T: type) *T {\n        return @alignCast(@ptrCast(self.data.?));\n    }\n};\n\n/// ExecFn is the type of function that is executed when the command is executed.\nconst ExecFn = *const fn (ctx: CommandContext) anyerror!void;\n\n/// This is needed to fool the compiler that we are not doing dependency loop\n/// common error would error: dependency loop detected if this function is not passed to the init function.\nconst ExecFnToPass = *const fn (ctx: CommandContext) anyerror!void;\n\n/// CommandOptions represents the metadata for a command, such as the name, description, version, and more.\npub const CommandOptions = struct {\n    section_title: []const u8 = \"General\",\n    name: []const u8,\n    description: []const u8,\n    version: ?std.SemanticVersion = null,\n    commands_title: []const u8 = \"Available commands\",\n    shortcut: ?[]const u8 = null,\n    aliases: ?[]const []const u8 = null,\n    short_description: ?[]const u8 = null,\n    help: ?[]const u8 = null,\n    usage: ?[]const u8 = null,\n    deprecated: bool = false,\n    replaced_by: ?[]const u8 = null,\n};\n\n/// Represents a single command in the Command Line Interface (CLI),\n/// such as \"run\", \"version\", or any other user-invoked operation.\n/// Each command encapsulates specific functionality or behavior\n/// that the CLI can execute.\n/// Command represents a single command in the CLI, such as \"run\" or \"version\".\npub const Command = struct {\n    options: CommandOptions,\n\n    flags_by_name: std.StringHashMap(Flag),\n    flags_by_shortcut: std.StringHashMap(Flag),\n    flag_values: std.StringHashMap(FlagValue),\n\n    positional_args: std.ArrayList(PositionalArg),\n\n    execFn: ExecFn,\n\n    commands_by_name: std.StringHashMap(*Command),\n    commands_by_shortcut: std.StringHashMap(*Command),\n    command_by_aliases: std.StringHashMap(*Command),\n\n    parent: ?*Command = null,\n    allocator: std.mem.Allocator,\n    stdout: Writer = std.io.getStdOut().writer(),\n    stderr: Writer = std.io.getStdErr().writer(),\n\n    pub fn init(allocator: std.mem.Allocator, options: CommandOptions, execFn: ExecFnToPass) !*Command {\n        const cmd = try allocator.create(Command);\n        cmd.* = Command{\n            .options = options,\n            .positional_args = std.ArrayList(PositionalArg).init(allocator),\n            .execFn = execFn,\n            .flags_by_name = std.StringHashMap(Flag).init(allocator),\n            .flags_by_shortcut = std.StringHashMap(Flag).init(allocator),\n            .flag_values = std.StringHashMap(FlagValue).init(allocator),\n            .commands_by_name = std.StringHashMap(*Command).init(allocator),\n            .commands_by_shortcut = std.StringHashMap(*Command).init(allocator),\n            .command_by_aliases = std.StringHashMap(*Command).init(allocator),\n            .allocator = allocator,\n        };\n\n        const helpFlag: Flag = .{\n            .name = \"help\",\n            .description = \"Shows the help for a command\",\n            .shortcut = \"h\",\n            .type = .Bool,\n            .default_value = .{ .Bool = false },\n        };\n\n        try cmd.addFlag(helpFlag);\n\n        return cmd;\n    }\n\n    pub fn deinit(self: *Command) void {\n        self.positional_args.deinit();\n\n        self.flags_by_name.deinit();\n        self.flags_by_shortcut.deinit();\n        self.flag_values.deinit();\n\n        var it = self.commands_by_name.iterator();\n        while (it.next()) |entry| {\n            const cmd = entry.value_ptr.*;\n            cmd.deinit();\n        }\n        self.commands_by_name.deinit();\n        self.commands_by_shortcut.deinit();\n        self.command_by_aliases.deinit();\n        self.allocator.destroy(self);\n    }\n\n    pub fn listCommands(self: *const Command) !void {\n        if (self.commands_by_name.count() == 0) {\n            return;\n        }\n\n        try self.stdout.print(\"{s}:\\n\", .{self.options.commands_title});\n\n        var commands = std.ArrayList(*Command).init(self.allocator);\n        defer commands.deinit();\n\n        var it = self.commands_by_name.iterator();\n        while (it.next()) |entry| {\n            try commands.append(entry.value_ptr.*);\n        }\n\n        std.sort.insertion(*Command, commands.items, {}, struct {\n            pub fn lessThan(_: void, a: *Command, b: *Command) bool {\n                return std.mem.order(u8, a.options.name, b.options.name) == .lt;\n            }\n        }.lessThan);\n\n        try printAlignedCommands(commands.items);\n    }\n\n    pub fn listCommandsBySection(self: *const Command) !void {\n        if (self.commands_by_name.count() == 0) {\n            return;\n        }\n\n        // Map to group commands by their section title.\n        var section_map = std.StringHashMap(std.ArrayList(*Command)).init(self.allocator);\n        defer {\n            var it = section_map.iterator();\n            while (it.next()) |entry| {\n                entry.value_ptr.*.deinit();\n            }\n            section_map.deinit();\n        }\n\n        // Populate the map.\n        var it = self.commands_by_name.iterator();\n        while (it.next()) |entry| {\n            const cmd = entry.value_ptr.*;\n            const section = cmd.options.section_title;\n\n            const list = try section_map.getOrPut(section);\n            if (!list.found_existing) {\n                list.value_ptr.* = std.ArrayList(*Command).init(self.allocator);\n            }\n            try list.value_ptr.*.append(cmd);\n        }\n\n        // --- START: MODIFIED SECTION ---\n\n        // 1. Collect all section titles (keys) from the map.\n        var section_keys = std.ArrayList([]const u8).init(self.allocator);\n        defer section_keys.deinit();\n\n        var key_it = section_map.keyIterator();\n        while (key_it.next()) |key| {\n            try section_keys.append(key.*);\n        }\n\n        std.sort.insertion([]const u8, section_keys.items, {}, struct {\n            pub fn lessThan(_: void, a: []const u8, b: []const u8) bool {\n                // Sort by command name, not section title\n                return std.mem.order(u8, a, b) == .lt;\n            }\n        }.lessThan);\n\n        for (section_keys.items) |section_name| {\n            // We know the key exists, so we can use .?\n            const cmds_list = section_map.get(section_name).?;\n\n            try self.stdout.print(\"{s}{s}{s}:\\n\", .{ styles.BOLD, section_name, styles.RESET });\n\n            // 4. FIX: Sort the commands *within* this section by their name.\n            std.sort.insertion(*Command, cmds_list.items, {}, struct {\n                pub fn lessThan(_: void, a: *Command, b: *Command) bool {\n                    // Sort by command name, not section title\n                    return std.mem.order(u8, a.options.name, b.options.name) == .lt;\n                }\n            }.lessThan);\n\n            try printAlignedCommands(cmds_list.items);\n            try self.stdout.print(\"\\n\", .{});\n        }\n        // --- END: MODIFIED SECTION ---\n    }\n\n    pub fn listFlags(self: *const Command) !void {\n        if (self.flags_by_name.count() == 0) {\n            return;\n        }\n\n        try self.stdout.print(\"Flags:\\n\", .{});\n\n        // Collect all flags into a list for processing\n        var flags = std.ArrayList(Flag).init(self.allocator);\n        defer flags.deinit();\n\n        var it = self.flags_by_name.iterator();\n        while (it.next()) |entry| {\n            const flag = entry.value_ptr.*;\n            if (!flag.hidden) {\n                try flags.append(flag);\n            }\n        }\n\n        try printAlignedFlags(flags.items);\n    }\n\n    pub fn listPositionalArgs(self: *const Command) !void {\n        if (self.positional_args.items.len == 0) return;\n\n        try self.stdout.print(\"Arguments:\\n\", .{});\n\n        var max_width: usize = 0;\n        for (self.positional_args.items) |arg| {\n            const name_len = arg.name.len;\n            if (name_len > max_width) max_width = name_len;\n        }\n\n        for (self.positional_args.items) |arg| {\n            const padding = max_width - arg.name.len;\n            try self.stdout.print(\"  {s}\", .{arg.name});\n            try self.stdout.writeByteNTimes(' ', padding + 4); // Align to column\n            try self.stdout.print(\"{s}\", .{arg.description});\n            if (arg.required) {\n                try self.stdout.print(\" (required)\", .{});\n            }\n            if (arg.variadic) {\n                try self.stdout.print(\" (variadic)\", .{});\n            }\n            try self.stdout.print(\"\\n\", .{});\n        }\n\n        try self.stdout.print(\"\\n\", .{});\n    }\n\n    pub fn listAliases(self: *Command) !void {\n        if (self.options.aliases) |aliases| {\n            if (aliases.len == 0) return;\n            try self.stdout.print(\"Aliases: \", .{});\n            for (aliases, 0..) |alias, i| {\n                try self.stdout.print(\"{s}\", .{alias});\n                if (i < aliases.len - 1) {\n                    try self.stdout.print(\", \", .{});\n                }\n            }\n        }\n    }\n\n    pub fn printUsageLine(self: *Command) !void {\n        const parents = try self.getParents(self.allocator);\n        defer parents.deinit();\n\n        try self.stdout.print(\"Usage: \", .{});\n\n        for (parents.items) |p| {\n            try self.stdout.print(\"{s} \", .{p.options.name});\n        }\n\n        try self.stdout.print(\"{s} [options]\", .{self.options.name});\n\n        for (self.positional_args.items) |arg| {\n            if (arg.required) {\n                try self.stdout.print(\" <{s}>\", .{arg.name});\n            } else {\n                try self.stdout.print(\" [{s}]\", .{arg.name});\n            }\n            if (arg.variadic) {\n                try self.stdout.print(\"...\", .{});\n            }\n        }\n    }\n\n    pub fn showInfo(self: *const Command) !void {\n        try self.stdout.print(\"{s}{s}{s}\\n\", .{ styles.BOLD, self.options.description, styles.RESET });\n        if (self.options.version) |version| try self.stdout.print(\"{s}v{}{s}\\n\", .{ styles.DIM, version, styles.RESET });\n    }\n\n    pub fn showVersion(self: *const Command) !void {\n        if (self.options.version) |version| try self.stdout.print(\"{}\\n\", .{version});\n    }\n\n    /// Prints traditional help with commands NOT organized by sections\n    pub fn printHelp(self: *Command) !void {\n        if (!self.options.deprecated) {\n            try self.showInfo();\n            try self.stdout.print(\"\\n\", .{});\n\n            if (self.options.help) |help| {\n                try self.stdout.print(\"{s}\\n\\n\", .{help});\n            }\n\n            const parents = try self.getParents(self.allocator);\n            defer parents.deinit();\n\n            // Usage\n            if (self.options.usage) |usage| {\n                try self.stdout.print(\"Usage: {s}\\n\", .{usage});\n            } else {\n                try self.printUsageLine();\n            }\n\n            if (self.options.aliases) |aliases| {\n                if (aliases.len > 0) {\n                    try self.stdout.print(\"\\n\\n\", .{});\n                }\n            }\n\n            // Aliases\n            try self.listAliases();\n\n            // Sub commands\n            try self.stdout.print(\"\\n\", .{});\n\n            if (self.commands_by_name.count() > 0) try self.stdout.print(\"\\n\", .{});\n            try self.listCommands();\n            try self.stdout.print(\"\\n\", .{});\n\n            // Flags\n            try self.listFlags();\n            if (self.flags_by_name.count() > 0) try self.stdout.print(\"\\n\", .{});\n\n            // Arguments\n            try self.listPositionalArgs();\n\n            const has_subcommands = self.commands_by_name.count() > 0;\n\n            try self.stdout.print(\"Use \\\"\", .{});\n            for (parents.items) |p| {\n                try self.stdout.print(\"{s} \", .{p.options.name});\n            }\n            try self.stdout.print(\"{s}\", .{self.options.name});\n\n            if (has_subcommands) {\n                try self.stdout.print(\" [command]\", .{});\n            }\n            try self.stdout.print(\" --help\\\" for more information.\\n\", .{});\n        }\n    }\n\n    /// Prints help with commands organized by sections\n    pub fn printStructuredHelp(self: *Command) !void {\n        if (!self.options.deprecated) {\n            try self.showInfo();\n            try self.stdout.print(\"\\n\", .{});\n\n            if (self.options.help) |help| {\n                try self.stdout.print(\"{s}\\n\\n\", .{help});\n            }\n\n            const parents = try self.getParents(self.allocator);\n            defer parents.deinit();\n\n            // Usage\n            if (self.options.usage) |usage| {\n                try self.stdout.print(\"Usage: {s}\\n\", .{usage});\n            } else {\n                try self.printUsageLine();\n            }\n\n            if (self.options.aliases) |aliases| {\n                if (aliases.len > 0) {\n                    try self.stdout.print(\"\\n\\n\", .{});\n                }\n            }\n\n            // Aliases\n            try self.listAliases();\n\n            // Sub commands\n            try self.stdout.print(\"\\n\", .{});\n\n            if (self.commands_by_name.count() > 0) try self.stdout.print(\"\\n\", .{});\n            try self.listCommandsBySection();\n            try self.stdout.print(\"\\n\", .{});\n\n            // Flags\n            try self.listFlags();\n            if (self.flags_by_name.count() > 0) try self.stdout.print(\"\\n\", .{});\n\n            // Arguments\n            try self.listPositionalArgs();\n\n            const has_subcommands = self.commands_by_name.count() > 0;\n\n            try self.stdout.print(\"Use \\\"\", .{});\n            for (parents.items) |p| {\n                try self.stdout.print(\"{s} \", .{p.options.name});\n            }\n            try self.stdout.print(\"{s}\", .{self.options.name});\n\n            if (has_subcommands) {\n                try self.stdout.print(\" [command]\", .{});\n            }\n            try self.stdout.print(\" --help\\\" for more information.\\n\", .{});\n        }\n    }\n\n    pub fn getParents(self: *Command, allocator: std.mem.Allocator) !std.ArrayList(*Command) {\n        var list = std.ArrayList(*Command).init(allocator);\n\n        var cmd = self;\n        while (cmd.parent) |p| {\n            try list.append(p);\n            cmd = p;\n        }\n\n        std.mem.reverse(*Command, list.items);\n        return list;\n    }\n\n    pub fn addCommand(self: *Command, command: *Command) !void {\n        command.parent = self;\n        try self.commands_by_name.put(command.options.name, command);\n        if (command.options.aliases) |aliases| {\n            for (aliases) |alias| {\n                try self.command_by_aliases.put(alias, command);\n            }\n        }\n        if (command.options.shortcut) |shortcut| try self.commands_by_shortcut.put(shortcut, command);\n    }\n\n    pub fn addCommands(self: *Command, commands: []const *Command) !void {\n        for (commands) |cmd| try self.addCommand(cmd);\n    }\n\n    pub fn addPositionalArg(self: *Command, pos_arg: PositionalArg) !void {\n        if (self.positional_args.items.len > 0) {\n            const last_arg = self.positional_args.items[self.positional_args.items.len - 1];\n            if (last_arg.variadic) {\n                try self.stderr.print(\"Variadic args should only appear at the end.\\n\", .{});\n                std.process.exit(1);\n            }\n        }\n        try self.positional_args.append(pos_arg);\n    }\n\n    pub fn addFlag(self: *Command, flag: Flag) !void {\n        try self.flags_by_name.put(flag.name, flag);\n        if (flag.shortcut) |shortcut| try self.flags_by_shortcut.put(shortcut, flag);\n\n        try self.flag_values.put(flag.name, flag.default_value);\n    }\n\n    pub fn addFlags(self: *Command, flags: []const Flag) !void {\n        for (flags) |flag| {\n            try self.addFlag(flag);\n        }\n    }\n\n    // cli run --faas pp --me --op=77 -p -abc xxxx yyyy zzzz\n    fn parseArgsAndFlags(self: *Command, args: *std.ArrayList([]const u8), out_positionals: *std.ArrayList([]const u8)) !void {\n        while (args.items.len > 0) {\n            const arg = args.items[0];\n\n            if (args.items.len > 0 and\n                (std.mem.eql(u8, arg, \"--help\") or std.mem.eql(u8, arg, \"-h\")))\n            {\n                try self.printHelp();\n                std.process.exit(0);\n            }\n\n            // Handle flags (all the existing parseFlags logic)\n            if (std.mem.startsWith(u8, arg, \"--\")) {\n                // --flag=value\n                if (std.mem.indexOf(u8, arg[2..], \"=\")) |eql_index| {\n                    const flag_name = arg[2..][0..eql_index];\n                    const value = arg[2 + eql_index + 1 ..];\n                    const flag = self.findFlag(flag_name);\n                    if (flag == null) {\n                        try self.stderr.print(\"Unknown flag: --{s}\\n\", .{flag_name});\n                        try self.displayCommandError();\n                        std.process.exit(1);\n                    }\n                    const flag_value = flag.?.safeEvaluate(value) catch {\n                        try self.stderr.print(\"Invalid value for flag --{s}: '{s}'\\n\", .{ flag_name, value });\n                        try self.stderr.print(\"Expected a value of type: {s}\\n\", .{@tagName(flag.?.type)});\n                        try self.displayCommandError();\n                        std.process.exit(1);\n                    };\n                    try self.flag_values.put(flag.?.name, flag_value);\n                    _ = try popFront([]const u8, args);\n                }\n                // --flag [value] or boolean\n                else {\n                    const flag_name = arg[2..];\n                    const flag = self.findFlag(flag_name);\n                    if (flag == null) {\n                        try self.stderr.print(\"Unknown flag: --{s}\\n\", .{flag_name});\n                        try self.displayCommandError();\n                        std.process.exit(1);\n                    }\n                    const has_next = args.items.len > 1;\n                    const next_value = if (has_next) args.items[1] else null;\n\n                    if (flag.?.type == .Bool) {\n                        if (next_value) |val| {\n                            const is_true = std.mem.eql(u8, val, \"true\");\n                            const is_false = std.mem.eql(u8, val, \"false\");\n                            if (is_true or is_false) {\n                                try self.flag_values.put(flag.?.name, .{ .Bool = is_true });\n                                _ = try popFront([]const u8, args); // --flag\n                                _ = try popFront([]const u8, args); // true/false\n                                continue;\n                            }\n                        }\n                        try self.flag_values.put(flag.?.name, .{ .Bool = true });\n                        _ = try popFront([]const u8, args);\n                    } else {\n                        if (!has_next) {\n                            try self.stderr.print(\"Missing value for flag --{s}\\n\", .{flag_name});\n                            try self.displayCommandError();\n                            std.process.exit(1);\n                        }\n                        const value = args.items[1];\n                        const flag_value = flag.?.safeEvaluate(value) catch {\n                            try self.stderr.print(\"Invalid value for flag --{s}: '{s}'\\n\", .{ flag_name, value });\n                            try self.stderr.print(\"Expected a value of type: {s}\\n\", .{@tagName(flag.?.type)});\n                            try self.displayCommandError();\n                            std.process.exit(1);\n                        };\n                        try self.flag_values.put(flag.?.name, flag_value);\n                        _ = try popFront([]const u8, args); // --flag\n                        _ = try popFront([]const u8, args); // value\n                    }\n                }\n            }\n            // -abc short flags\n            else if (std.mem.startsWith(u8, arg, \"-\") and arg.len > 1 and !std.mem.eql(u8, arg, \"-\")) {\n                const shortcuts = arg[1..];\n                var j: usize = 0;\n                while (j < shortcuts.len) : (j += 1) {\n                    const shortcut = shortcuts[j .. j + 1];\n                    const flag = self.findFlag(shortcut);\n                    if (flag == null) {\n                        try self.stderr.print(\"Unknown flag: -{c}\\n\", .{shortcuts[j]});\n                        std.process.exit(1);\n                    }\n                    if (flag.?.type == .Bool) {\n                        try self.flag_values.put(flag.?.name, .{ .Bool = true });\n                    } else {\n                        if (j < shortcuts.len - 1) {\n                            try self.stderr.print(\"Flag -{c} ({s}) must be last in group since it expects a value\\n\", .{ shortcuts[j], flag.?.name });\n                            std.process.exit(1);\n                        }\n                        if (args.items.len < 2) {\n                            try self.stderr.print(\"Missing value for flag -{c} ({s})\\n\", .{ shortcuts[j], flag.?.name });\n                            std.process.exit(1);\n                        }\n                        const value = args.items[1];\n                        const flag_value = flag.?.safeEvaluate(value) catch {\n                            try self.stderr.print(\"Invalid value for flag -{c} ({s}): '{s}'\\n\", .{ shortcuts[j], flag.?.name, value });\n                            try self.stderr.print(\"Expected a value of type: {s}\\n\", .{@tagName(flag.?.type)});\n                            std.process.exit(1);\n                        };\n                        try self.flag_values.put(flag.?.name, flag_value);\n                        _ = try popFront([]const u8, args); // value\n                    }\n                }\n                _ = try popFront([]const u8, args); // -abc\n            }\n            // Positional argument\n            else {\n                const val = try popFront([]const u8, args);\n                try out_positionals.append(val);\n            }\n        }\n    }\n\n    fn findFlag(self: *Command, name_or_shortcut: []const u8) ?Flag {\n        if (self.flags_by_name.get(name_or_shortcut)) |flag| return flag;\n        if (self.flags_by_shortcut.get(name_or_shortcut)) |flag| return flag;\n        return null;\n    }\n\n    fn parsePositionalArgs(self: *Command, args: *std.ArrayList([]const u8)) !void {\n        const expected = self.positional_args.items;\n\n        var required_count: u8 = 0;\n        for (expected) |value| {\n            if (value.required) required_count += 1;\n        }\n\n        if (args.items.len < required_count) {\n            try self.stderr.print(\"Missing {d} positional argument(s).\\n\\nExpected: \", .{required_count});\n\n            var first = true;\n            for (expected) |arg| {\n                if (arg.required) {\n                    if (!first) try self.stderr.print(\", \", .{});\n                    try self.stderr.print(\"{s}\", .{arg.name});\n                    first = false;\n                }\n            }\n\n            try self.stderr.print(\"\\n\", .{});\n            try self.displayCommandError();\n            return error.MissingArgs;\n        }\n\n        if (expected.len > 0) {\n            const last_arg = expected[expected.len - 1];\n            if (!last_arg.variadic and args.items.len > expected.len) {\n                try self.stderr.print(\"Too many positional arguments. Expected at most {}.\\n\", .{expected.len});\n                try self.displayCommandError();\n                return error.TooManyArgs;\n            }\n        }\n    }\n\n    pub fn findCommand(self: *const Command, name_or_shortcut: []const u8) ?*Command {\n        if (self.commands_by_name.get(name_or_shortcut)) |cmd| return cmd;\n        if (self.command_by_aliases.get(name_or_shortcut)) |cmd| return cmd;\n        if (self.commands_by_shortcut.get(name_or_shortcut)) |cmd| return cmd;\n        return null;\n    }\n\n    ", "suffix_code": "\n\n    // Traverse the commands to find the last one in the user input\n    fn findLeaf(self: *Command, args: *std.ArrayList([]const u8)) !*Command {\n        var current = self;\n\n        while (args.items.len > 0 and !std.mem.startsWith(u8, args.items[0], \"-\")) {\n            const name = args.items[0];\n            const maybe_next = current.findCommand(name);\n\n            if (maybe_next == null) {\n                // Check if the current command expects positional arguments\n                const expects_pos_args = current.positional_args.items.len > 0;\n                if (!expects_pos_args) {\n                    try current.stderr.print(\"Unknown command: '{s}'\\n\", .{name});\n                    try current.displayCommandError();\n                    return error.UnknownCommand;\n                }\n                break;\n            }\n\n            _ = try popFront([]const u8, args);\n            current = maybe_next.?;\n        }\n\n        return current;\n    }\n\n    // Need to make find command, parse flags and parse pos_args execution in parallel\n    pub fn execute(self: *Command, context: struct { data: ?*anyopaque = null }) !void {\n        var bw = std.io.bufferedWriter(self.stdout);\n        defer bw.flush() catch {};\n\n        var input = try std.process.argsWithAllocator(self.allocator);\n        defer input.deinit();\n        _ = input.skip(); // skip program name\n\n        var args = std.ArrayList([]const u8).init(self.allocator);\n        defer args.deinit();\n        while (input.next()) |arg| {\n            try args.append(arg);\n        }\n\n        var pos_args = std.ArrayList([]const u8).init(self.allocator);\n        defer pos_args.deinit();\n\n        var cmd = self.findLeaf(&args) catch |err| {\n            if (err == error.UnknownCommand) {\n                std.process.exit(1);\n            }\n            return err;\n        };\n\n        cmd.checkDeprecated() catch std.process.exit(1);\n\n        try cmd.parseArgsAndFlags(&args, &pos_args);\n        cmd.parsePositionalArgs(&pos_args) catch std.process.exit(1);\n\n        const spinner = try Spinner.init(cmd.allocator, .{});\n        defer spinner.deinit();\n\n        const root = self;\n        const ctx = CommandContext{\n            .root = root,\n            .direct_parent = cmd.parent orelse root,\n            .command = cmd,\n            .allocator = cmd.allocator,\n            .positional_args = pos_args.items,\n            .spinner = spinner,\n            .data = context.data,\n        };\n\n        try cmd.execFn(ctx);\n    }\n\n    fn displayCommandError(self: *Command) !void {\n        const parents = try self.getParents(self.allocator);\n        defer parents.deinit();\n\n        try self.stderr.print(\"\\nRun: '\", .{});\n        for (parents.items) |p| {\n            try self.stderr.print(\"{s} \", .{p.options.name});\n        }\n        try self.stderr.print(\"{s} --help'\\n\", .{self.options.name});\n    }\n};\n\n// HELPER FUNCTIONS\n\n/// Prints a list of commands aligned to the maximum width of the commands.\nfn printAlignedCommands(commands: []*Command) !void {\n    // Step 1: determine the maximum width of name + shortcut\n    var max_width: usize = 0;\n    for (commands) |cmd| {\n        const name_len = cmd.options.name.len;\n        const shortcut_len = if (cmd.options.shortcut) |s| s.len + 3 else 0; // \" ({s})\"\n        const total_len = name_len + shortcut_len;\n        if (total_len > max_width) max_width = total_len;\n    }\n\n    // Step 2: print each command with aligned description\n    for (commands) |cmd| {\n        const desc = cmd.options.short_description orelse cmd.options.description;\n\n        // Print name\n        try cmd.stdout.print(\"   {s}\", .{cmd.options.name});\n\n        // Print shortcut directly if exists\n        if (cmd.options.shortcut) |s| {\n            try cmd.stdout.print(\" ({s})\", .{s});\n        }\n\n        // Compute padding\n        const name_len = cmd.options.name.len;\n        var shortcut_len: usize = 0;\n        var extra_parens: usize = 0;\n\n        if (cmd.options.shortcut) |s| {\n            shortcut_len = s.len;\n            extra_parens = 3; // space + parentheses\n        }\n\n        const printed_width = name_len + shortcut_len + extra_parens;\n\n        const padding = max_width - printed_width;\n\n        try cmd.stdout.writeByteNTimes(' ', padding + 4); // 4-space gap between name and desc\n        try cmd.stdout.print(\"{s}\\n\", .{desc});\n    }\n}\n\n/// Prints a list of flags aligned to the maximum width of the flags.\nfn printAlignedFlags(flags: []const Flag) !void {\n    if (flags.len == 0) return;\n\n    // Get stdout from the first flag's command context\n    const stdout = std.io.getStdOut().writer();\n\n    // Calculate maximum width for the flag name + shortcut part\n    var max_width: usize = 0;\n    for (flags) |flag| {\n        var flag_width: usize = 0;\n\n        // Add shortcut width if present: \" -x, \"\n        if (flag.shortcut) |shortcut| {\n            flag_width += 1 + shortcut.len + 2; // \" -\" + shortcut + \", \"\n        } else {\n            flag_width += 5; // \"     \" (5 spaces for alignment)\n        }\n\n        // Add flag name width: \"--flagname\"\n        flag_width += 2 + flag.name.len; // \"--\" + name\n\n        if (flag_width > max_width) {\n            max_width = flag_width;\n        }\n    }\n\n    // Print each flag with proper alignment\n    for (flags) |flag| {\n        var current_width: usize = 0;\n\n        // Print shortcut if available\n        if (flag.shortcut) |shortcut| {\n            try stdout.print(\" -{s}, \", .{shortcut});\n            current_width += 1 + shortcut.len + 2;\n        } else {\n            try stdout.print(\"     \", .{});\n            current_width += 5;\n        }\n\n        // Print flag name\n        try stdout.print(\"--{s}\", .{flag.name});\n        current_width += 2 + flag.name.len;\n\n        // Calculate and add padding\n        const padding = max_width - current_width;\n        try stdout.writeByteNTimes(' ', padding + 4); // 4-space gap\n\n        // Print description and type\n        try stdout.print(\"{s} [{s}]\", .{\n            flag.description,\n            @tagName(flag.type),\n        });\n\n        // Print default value\n        switch (flag.type) {\n            .Bool => try stdout.print(\" (default: {s})\", .{if (flag.default_value.Bool) \"true\" else \"false\"}),\n            .Int => try stdout.print(\" (default: {})\", .{flag.default_value.Int}),\n            .String => if (flag.default_value.String.len > 0) {\n                try stdout.print(\" (default: \\\"{s}\\\")\", .{flag.default_value.String});\n            },\n        }\n        try stdout.print(\"\\n\", .{});\n    }\n}\n\n/// Pop the first element from the list and shift the rest\n// A more efficient popFront\nfn popFront(comptime T: type, list: *std.ArrayList(T)) !T {\n    if (list.items.len == 0) return error.Empty;\n    return list.orderedRemove(0);\n}\n\n// Test suite for CLI library\nconst testing = std.testing;\n\n// HELPER FUNCTIONS TESTS\ntest \"popFront: shifts elements correctly\" {\n    const allocator = testing.allocator;\n    var list = std.ArrayList(i32).init(allocator);\n    defer list.deinit();\n\n    try list.appendSlice(&[_]i32{ 1, 2, 3, 4 });\n\n    const first = try popFront(i32, &list);\n    try testing.expectEqual(@as(i32, 1), first);\n    try testing.expectEqual(@as(usize, 3), list.items.len);\n    try testing.expectEqualSlices(i32, &[_]i32{ 2, 3, 4 }, list.items);\n}\n\ntest \"popFront: single element\" {\n    const allocator = testing.allocator;\n    var list = std.ArrayList(i32).init(allocator);\n    defer list.deinit();\n\n    try list.append(42);\n\n    const first = try popFront(i32, &list);\n    try testing.expectEqual(@as(i32, 42), first);\n    try testing.expectEqual(@as(usize, 0), list.items.len);\n}\n\ntest \"popFront: empty list returns error\" {\n    const allocator = testing.allocator;\n    var list = std.ArrayList(i32).init(allocator);\n    defer list.deinit();\n\n    try testing.expectError(error.Empty, popFront(i32, &list));\n}\n\n// FLAG VALUE EVALUATION TESTS\ntest \"Flag.evaluateValueType: boolean values\" {\n    const flag = Flag{\n        .name = \"verbose\",\n        .description = \"Enable verbose output\",\n        .type = .Bool,\n        .default_value = .{ .Bool = false },\n    };\n\n    // Test valid boolean values\n    const val_true = try flag.evaluateValueType(\"true\");\n    try testing.expect(val_true == .Bool and val_true.Bool == true);\n\n    const val_false = try flag.evaluateValueType(\"false\");\n    try testing.expect(val_false == .Bool and val_false.Bool == false);\n\n    // Test invalid boolean values\n    try testing.expectError(error.InvalidBooleanValue, flag.evaluateValueType(\"yes\"));\n    try testing.expectError(error.InvalidBooleanValue, flag.evaluateValueType(\"1\"));\n    try testing.expectError(error.InvalidBooleanValue, flag.evaluateValueType(\"True\"));\n    try testing.expectError(error.InvalidBooleanValue, flag.evaluateValueType(\"\"));\n}\n\ntest \"Flag.evaluateValueType: integer values\" {\n    const flag = Flag{\n        .name = \"port\",\n        .description = \"Port number\",\n        .type = .Int,\n        .default_value = .{ .Int = 8080 },\n    };\n\n    // Test valid integers\n    const positive = try flag.evaluateValueType(\"1234\");\n    try testing.expectEqual(@as(i32, 1234), positive.Int);\n\n    const negative = try flag.evaluateValueType(\"-42\");\n    try testing.expectEqual(@as(i32, -42), negative.Int);\n\n    const zero = try flag.evaluateValueType(\"0\");\n    try testing.expectEqual(@as(i32, 0), zero.Int);\n\n    // Test invalid integers\n    try testing.expectError(error.InvalidCharacter, flag.evaluateValueType(\"abc\"));\n    try testing.expectError(error.InvalidCharacter, flag.evaluateValueType(\"12.34\"));\n    try testing.expectError(error.InvalidCharacter, flag.evaluateValueType(\"\"));\n}\n\ntest \"Flag.evaluateValueType: string values\" {\n    const flag = Flag{\n        .name = \"output\",\n        .description = \"Output file\",\n        .type = .String,\n        .default_value = .{ .String = \"output.txt\" },\n    };\n\n    // Test various string values\n    const normal = try flag.evaluateValueType(\"hello\");\n    try testing.expectEqualStrings(\"hello\", normal.String);\n\n    const empty = try flag.evaluateValueType(\"\");\n    try testing.expectEqualStrings(\"\", empty.String);\n\n    const with_spaces = try flag.evaluateValueType(\"hello world\");\n    try testing.expectEqualStrings(\"hello world\", with_spaces.String);\n\n    const special_chars = try flag.evaluateValueType(\"test@#$%\");\n    try testing.expectEqualStrings(\"test@#$%\", special_chars.String);\n}\n\ntest \"Flag.safeEvaluate: error handling\" {\n    const int_flag = Flag{\n        .name = \"count\",\n        .description = \"Count value\",\n        .type = .Int,\n        .default_value = .{ .Int = 0 },\n    };\n\n    // Should map specific errors to InvalidFlagValue\n    try testing.expectError(error.InvalidFlagValue, int_flag.safeEvaluate(\"not_a_number\"));\n    try testing.expectError(error.InvalidFlagValue, int_flag.safeEvaluate(\"12.34\"));\n\n    // Valid values should work\n    const valid = try int_flag.safeEvaluate(\"123\");\n    try testing.expectEqual(@as(i32, 123), valid.Int);\n}\n\n// COMMAND INITIALIZATION TESTS\ntest \"Command.init: creates command with default help flag\" {\n    const allocator = testing.allocator;\n    const cmd = try Command.init(allocator, .{\n        .name = \"test-cmd\",\n        .description = \"Test command\",\n    }, dummyExec);\n    defer cmd.deinit();\n\n    // Should have help flag by default\n    try testing.expect(cmd.findFlag(\"help\") != null);\n    try testing.expect(cmd.findFlag(\"h\") != null);\n\n    const help_flag = cmd.findFlag(\"help\").?;\n    try testing.expectEqualStrings(\"help\", help_flag.name);\n    try testing.expect(help_flag.type == .Bool);\n    try testing.expect(help_flag.default_value.Bool == false);\n}\n\ntest \"Command.init: proper initialization of all fields\" {\n    const allocator = testing.allocator;\n    const options = CommandOptions{\n        .name = \"mycmd\",\n        .description = \"My command\",\n        .version = std.SemanticVersion{ .major = 1, .minor = 0, .patch = 0 },\n        .shortcut = \"mc\",\n    };\n\n    const cmd = try Command.init(allocator, options, dummyExec);\n    defer cmd.deinit();\n\n    try testing.expectEqualStrings(\"mycmd\", cmd.options.name);\n    try testing.expectEqualStrings(\"My command\", cmd.options.description);\n    try testing.expect(cmd.options.version != null);\n    try testing.expectEqualStrings(\"mc\", cmd.options.shortcut.?);\n    try testing.expect(cmd.parent == null);\n    try testing.expectEqual(@as(usize, 0), cmd.positional_args.items.len);\n}\n\n// FLAG MANAGEMENT TESTS\ntest \"Command.addFlag: adds flag correctly\" {\n    const allocator = testing.allocator;\n    const cmd = try Command.init(allocator, .{\n        .name = \"test\",\n        .description = \"Test\",\n    }, dummyExec);\n    defer cmd.deinit();\n\n    const flag = Flag{\n        .name = \"verbose\",\n        .shortcut = \"v\",\n        .description = \"Enable verbose output\",\n        .type = .Bool,\n        .default_value = .{ .Bool = false },\n    };\n\n    try cmd.addFlag(flag);\n\n    // Should be findable by name and shortcut\n    const by_name = cmd.findFlag(\"verbose\");\n    try testing.expect(by_name != null);\n    try testing.expectEqualStrings(\"verbose\", by_name.?.name);\n\n    const by_shortcut = cmd.findFlag(\"v\");\n    try testing.expect(by_shortcut != null);\n    try testing.expectEqualStrings(\"verbose\", by_shortcut.?.name);\n\n    // Should have default value set\n    const default_val = cmd.flag_values.get(\"verbose\");\n    try testing.expect(default_val != null);\n    try testing.expect(default_val.?.Bool == false);\n}\n\ntest \"Command.addFlag: flag without shortcut\" {\n    const allocator = testing.allocator;\n    const cmd = try Command.init(allocator, .{\n        .name = \"test\",\n        .description = \"Test\",\n    }, dummyExec);\n    defer cmd.deinit();\n\n    const flag = Flag{\n        .name = \"no-shortcut\",\n        .shortcut = null,\n        .description = \"Flag without shortcut\",\n        .type = .String,\n        .default_value = .{ .String = \"default\" },\n    };\n\n    try cmd.addFlag(flag);\n\n    try testing.expect(cmd.findFlag(\"no-shortcut\") != null);\n    // Shortcut lookup should return null since there's no shortcut\n    try testing.expect(cmd.flags_by_shortcut.count() == 1); // only 'h' from help\n}\n\ntest \"Command.addFlags: adds multiple flags\" {\n    const allocator = testing.allocator;\n    const cmd = try Command.init(allocator, .{\n        .name = \"test\",\n        .description = \"Test\",\n    }, dummyExec);\n    defer cmd.deinit();\n\n    const flags = [_]Flag{\n        .{\n            .name = \"flag1\",\n            .shortcut = \"1\",\n            .description = \"First flag\",\n            .type = .Bool,\n            .default_value = .{ .Bool = false },\n        },\n        .{\n            .name = \"flag2\",\n            .shortcut = \"2\",\n            .description = \"Second flag\",\n            .type = .Int,\n            .default_value = .{ .Int = 42 },\n        },\n    };\n\n    try cmd.addFlags(&flags);\n\n    try testing.expect(cmd.findFlag(\"flag1\") != null);\n    try testing.expect(cmd.findFlag(\"flag2\") != null);\n    try testing.expect(cmd.findFlag(\"1\") != null);\n    try testing.expect(cmd.findFlag(\"2\") != null);\n}\n\n// POSITIONAL ARGUMENT TESTS\ntest \"Command.addPositionalArg: adds argument correctly\" {\n    const allocator = testing.allocator;\n    const cmd = try Command.init(allocator, .{\n        .name = \"test\",\n        .description = \"Test\",\n    }, dummyExec);\n    defer cmd.deinit();\n\n    const arg = PositionalArg{\n        .name = \"input\",\n        .description = \"Input file\",\n        .required = true,\n    };\n\n    try cmd.addPositionalArg(arg);\n\n    try testing.expectEqual(@as(usize, 1), cmd.positional_args.items.len);\n    try testing.expectEqualStrings(\"input\", cmd.positional_args.items[0].name);\n    try testing.expect(cmd.positional_args.items[0].required);\n}\n\ntest \"Command.addPositionalArg: variadic arg validation\" {\n    const allocator = testing.allocator;\n    const cmd = try Command.init(allocator, .{\n        .name = \"test\",\n        .description = \"Test\",\n    }, dummyExec);\n    defer cmd.deinit();\n\n    // Add regular arg first\n    try cmd.addPositionalArg(.{\n        .name = \"input\",\n        .description = \"Input file\",\n        .required = true,\n    });\n\n    // Add variadic arg\n    try cmd.addPositionalArg(.{\n        .name = \"files\",\n        .description = \"Multiple files\",\n        .required = false,\n        .variadic = true,\n    });\n\n    // This should fail in actual implementation - testing error case would need process.exit handling\n    // For now, just verify the args were added\n    try testing.expectEqual(@as(usize, 2), cmd.positional_args.items.len);\n}\n\n// COMMAND HIERARCHY TESTS\ntest \"Command.addCommand: establishes parent-child relationship\" {\n    const allocator = testing.allocator;\n    const root = try Command.init(allocator, .{\n        .name = \"root\",\n        .description = \"Root command\",\n    }, dummyExec);\n    defer root.deinit();\n\n    const child = try Command.init(allocator, .{\n        .name = \"child\",\n        .description = \"Child command\",\n        .shortcut = \"c\",\n    }, dummyExec);\n\n    try root.addCommand(child);\n\n    // Child should have parent set\n    try testing.expect(child.parent == root);\n\n    // Root should be able to find child\n    try testing.expect(root.findCommand(\"child\") == child);\n    try testing.expect(root.findCommand(\"c\") == child);\n}\n\ntest \"Command.addCommand: with aliases\" {\n    const allocator = testing.allocator;\n    const root = try Command.init(allocator, .{\n        .name = \"root\",\n        .description = \"Root\",\n    }, dummyExec);\n    defer root.deinit();\n\n    const aliases = [_][]const u8{ \"alias1\", \"alias2\", \"alias3\" };\n    const child = try Command.init(allocator, .{\n        .name = \"child\",\n        .description = \"Child with aliases\",\n        .aliases = &aliases,\n    }, dummyExec);\n\n    try root.addCommand(child);\n\n    // Should be findable by all aliases\n    for (aliases) |alias| {\n        try testing.expect(root.findCommand(alias) == child);\n    }\n}\n\ntest \"Command.findLeaf: traverses command hierarchy\" {\n    const allocator = testing.allocator;\n    const root = try Command.init(allocator, .{\n        .name = \"root\",\n        .description = \"Root\",\n    }, dummyExec);\n    defer root.deinit();\n\n    const level1 = try Command.init(allocator, .{\n        .name = \"level1\",\n        .description = \"Level 1\",\n    }, dummyExec);\n\n    const level2 = try Command.init(allocator, .{\n        .name = \"level2\",\n        .description = \"Level 2\",\n    }, dummyExec);\n\n    try root.addCommand(level1);\n    try level1.addCommand(level2);\n\n    var args = std.ArrayList([]const u8).init(allocator);\n    defer args.deinit();\n    try args.appendSlice(&[_][]const u8{ \"level1\", \"level2\" });\n\n    const leaf = try root.findLeaf(&args);\n    try testing.expect(leaf == level2);\n\n    // Args should be consumed\n    try testing.expectEqual(@as(usize, 0), args.items.len);\n}\n\ntest \"Command.findLeaf: stops at unknown command\" {\n    const allocator = testing.allocator;\n    const root = try Command.init(allocator, .{\n        .name = \"root\",\n        .description = \"Root\",\n    }, dummyExec);\n    defer root.deinit();\n\n    const child = try Command.init(allocator, .{\n        .name = \"child\",\n        .description = \"Child\",\n    }, dummyExec);\n\n    try root.addCommand(child);\n\n    var args = std.ArrayList([]const u8).init(allocator);\n    defer args.deinit();\n    try args.appendSlice(&[_][]const u8{ \"child\", \"unknown\" });\n\n    const result = root.findLeaf(&args);\n    try testing.expectError(error.UnknownCommand, result);\n}\n\n// FLAG PARSING TESTS\ntest \"parseArgsAndFlags: long flag with equals\" {\n    const allocator = testing.allocator;\n    const cmd = try Command.init(allocator, .{\n        .name = \"test\",\n        .description = \"Test\",\n    }, dummyExec);\n    defer cmd.deinit();\n\n    try cmd.addFlag(.{\n        .name = \"output\",\n        .description = \"Output file\",\n        .type = .String,\n        .default_value = .{ .String = \"\" },\n    });\n\n    var args = std.ArrayList([]const u8).init(allocator);\n    defer args.deinit();\n    try args.append(\"--output=file.txt\");\n\n    var positionals = std.ArrayList([]const u8).init(allocator);\n    defer positionals.deinit();\n\n    try cmd.parseArgsAndFlags(&args, &positionals);\n\n    const value = cmd.flag_values.get(\"output\").?;\n    try testing.expectEqualStrings(\"file.txt\", value.String);\n    try testing.expectEqual(@as(usize, 0), positionals.items.len);\n}\n\ntest \"parseArgsAndFlags: boolean flag variations\" {\n    const allocator = testing.allocator;\n    const cmd = try Command.init(allocator, .{\n        .name = \"test\",\n        .description = \"Test\",\n    }, dummyExec);\n    defer cmd.deinit();\n\n    try cmd.addFlags(&[_]Flag{\n        .{\n            .name = \"verbose\",\n            .shortcut = \"v\",\n            .description = \"Verbose\",\n            .type = .Bool,\n            .default_value = .{ .Bool = false },\n        },\n        .{\n            .name = \"quiet\",\n            .description = \"Quiet\",\n            .type = .Bool,\n            .default_value = .{ .Bool = false },\n        },\n    });\n\n    var args = std.ArrayList([]const u8).init(allocator);\n    defer args.deinit();\n    try args.appendSlice(&[_][]const u8{ \"--verbose\", \"--quiet=true\", \"-v\" });\n\n    var positionals = std.ArrayList([]const u8).init(allocator);\n    defer positionals.deinit();\n\n    try cmd.parseArgsAndFlags(&args, &positionals);\n\n    try testing.expect(cmd.flag_values.get(\"verbose\").?.Bool);\n    try testing.expect(cmd.flag_values.get(\"quiet\").?.Bool);\n}\n\ntest \"parseArgsAndFlags: short flag grouping\" {\n    const allocator = testing.allocator;\n    const cmd = try Command.init(allocator, .{\n        .name = \"test\",\n        .description = \"Test\",\n    }, dummyExec);\n    defer cmd.deinit();\n\n    try cmd.addFlags(&[_]Flag{\n        .{\n            .name = \"all\",\n            .shortcut = \"a\",\n            .description = \"All\",\n            .type = .Bool,\n            .default_value = .{ .Bool = false },\n        },\n        .{\n            .name = \"long\",\n            .shortcut = \"l\",\n            .description = \"Long\",\n            .type = .Bool,\n            .default_value = .{ .Bool = false },\n        },\n        .{\n            .name = \"human\",\n            .shortcut = \"h\",\n            .description = \"Human readable\",\n            .type = .Bool,\n            .default_value = .{ .Bool = false },\n        },\n    });\n\n    var args = std.ArrayList([]const u8).init(allocator);\n    defer args.deinit();\n    try args.append(\"-alh\");\n\n    var positionals = std.ArrayList([]const u8).init(allocator);\n    defer positionals.deinit();\n\n    try cmd.parseArgsAndFlags(&args, &positionals);\n\n    try testing.expect(cmd.flag_values.get(\"all\").?.Bool);\n    try testing.expect(cmd.flag_values.get(\"long\").?.Bool);\n    // Note: 'h' conflicts with help flag, this test shows the issue\n}\n\ntest \"parseArgsAndFlags: mixed flags and positionals\" {\n    const allocator = testing.allocator;\n    const cmd = try Command.init(allocator, .{\n        .name = \"test\",\n        .description = \"Test\",\n    }, dummyExec);\n    defer cmd.deinit();\n\n    try cmd.addFlag(.{\n        .name = \"count\",\n        .shortcut = \"c\",\n        .description = \"Count\",\n        .type = .Int,\n        .default_value = .{ .Int = 0 },\n    });\n\n    var args = std.ArrayList([]const u8).init(allocator);\n    defer args.deinit();\n    try args.appendSlice(&[_][]const u8{ \"file1\", \"--count\", \"5\", \"file2\" });\n\n    var positionals = std.ArrayList([]const u8).init(allocator);\n    defer positionals.deinit();\n\n    try cmd.parseArgsAndFlags(&args, &positionals);\n\n    try testing.expectEqual(@as(i32, 5), cmd.flag_values.get(\"count\").?.Int);\n    try testing.expectEqual(@as(usize, 2), positionals.items.len);\n    try testing.expectEqualStrings(\"file1\", positionals.items[0]);\n    try testing.expectEqualStrings(\"file2\", positionals.items[1]);\n}\n\n// COMMAND CONTEXT TESTS\ntest \"CommandContext.flag: retrieves set flag value\" {\n    const allocator = testing.allocator;\n    const cmd = try Command.init(allocator, .{\n        .name = \"test\",\n        .description = \"Test\",\n    }, dummyExec);\n    defer cmd.deinit();\n\n    try cmd.addFlag(.{\n        .name = \"port\",\n        .description = \"Port number\",\n        .type = .Int,\n        .default_value = .{ .Int = 8080 },\n    });\n\n    // Simulate setting a flag value\n    try cmd.flag_values.put(\"port\", .{ .Int = 3000 });\n\n    const spinner = try Spinner.init(cmd.allocator, .{});\n    defer spinner.deinit();\n\n    const ctx = CommandContext{\n        .root = cmd,\n        .direct_parent = cmd,\n        .command = cmd,\n        .allocator = allocator,\n        .spinner = spinner,\n        .positional_args = &[_][]const u8{},\n    };\n\n    try testing.expectEqual(@as(i32, 3000), ctx.flag(\"port\", i32));\n}\n\ntest \"CommandContext.flag: fallback to default value\" {\n    const allocator = testing.allocator;\n    const cmd = try Command.init(allocator, .{\n        .name = \"test\",\n        .description = \"Test\",\n    }, dummyExec);\n    defer cmd.deinit();\n\n    try cmd.addFlag(.{\n        .name = \"debug\",\n        .description = \"Debug mode\",\n        .type = .Bool,\n        .default_value = .{ .Bool = true },\n    });\n\n    const spinner = try Spinner.init(cmd.allocator, .{});\n    defer spinner.deinit();\n\n    const ctx = CommandContext{\n        .root = cmd,\n        .direct_parent = cmd,\n        .command = cmd,\n        .allocator = allocator,\n        .spinner = spinner,\n        .positional_args = &[_][]const u8{},\n    };\n\n    // Should return default since flag_values doesn't have \"debug\" set to a different value\n    try testing.expect(ctx.flag(\"debug\", bool));\n}\n\ntest \"CommandContext.getArg: retrieves positional argument\" {\n    const allocator = testing.allocator;\n    const cmd = try Command.init(allocator, .{\n        .name = \"test\",\n        .description = \"Test\",\n    }, dummyExec);\n    defer cmd.deinit();\n\n    try cmd.addPositionalArg(.{\n        .name = \"username\",\n        .description = \"Username\",\n        .required = true,\n    });\n\n    try cmd.addPositionalArg(.{\n        .name = \"password\",\n        .description = \"Password\",\n        .required = false,\n    });\n\n    const args = [_][]const u8{ \"john\", \"secret123\" };\n    const spinner = try Spinner.init(cmd.allocator, .{});\n    defer spinner.deinit();\n    const ctx = CommandContext{\n        .root = cmd,\n        .direct_parent = cmd,\n        .command = cmd,\n        .allocator = allocator,\n        .spinner = spinner,\n        .positional_args = &args,\n    };\n\n    try testing.expectEqualStrings(\"john\", ctx.getArg(\"username\").?);\n    try testing.expectEqualStrings(\"secret123\", ctx.getArg(\"password\").?);\n    try testing.expect(ctx.getArg(\"nonexistent\") == null);\n}\n\ntest \"CommandContext.getArg: missing optional argument\" {\n    const allocator = testing.allocator;\n    const cmd = try Command.init(allocator, .{\n        .name = \"test\",\n        .description = \"Test\",\n    }, dummyExec);\n    defer cmd.deinit();\n\n    try cmd.addPositionalArg(.{\n        .name = \"required\",\n        .description = \"Required arg\",\n        .required = true,\n    });\n\n    try cmd.addPositionalArg(.{\n        .name = \"optional\",\n        .description = \"Optional arg\",\n        .required = false,\n    });\n\n    const args = [_][]const u8{\"onlyRequired\"};\n\n    const spinner = try Spinner.init(cmd.allocator, .{});\n    defer spinner.deinit();\n\n    const ctx = CommandContext{\n        .root = cmd,\n        .direct_parent = cmd,\n        .command = cmd,\n        .allocator = allocator,\n        .spinner = spinner,\n        .positional_args = &args,\n    };\n\n    try testing.expectEqualStrings(\"onlyRequired\", ctx.getArg(\"required\").?);\n    try testing.expect(ctx.getArg(\"optional\") == null);\n}\n\ntest \"CommandContext.getContextData: type casting\" {\n    const allocator = testing.allocator;\n    const cmd = try Command.init(allocator, .{\n        .name = \"test\",\n        .description = \"Test\",\n    }, dummyExec);\n    defer cmd.deinit();\n\n    const TestData = struct {\n        value: i32,\n        name: []const u8,\n    };\n\n    var data = TestData{ .value = 42, .name = \"test\" };\n\n    const spinner = try Spinner.init(cmd.allocator, .{});\n    defer spinner.deinit();\n\n    const ctx = CommandContext{\n        .root = cmd,\n        .direct_parent = cmd,\n        .command = cmd,\n        .allocator = allocator,\n        .spinner = spinner,\n        .positional_args = &[_][]const u8{},\n        .data = &data,\n    };\n\n    const retrieved = ctx.getContextData(TestData);\n    try testing.expectEqual(@as(i32, 42), retrieved.value);\n    try testing.expectEqualStrings(\"test\", retrieved.name);\n}\n\n// INTEGRATION TESTS\ntest \"full command parsing workflow\" {\n    const allocator = testing.allocator;\n\n    // Create root command\n    const root = try Command.init(allocator, .{\n        .name = \"myapp\",\n        .description = \"My application\",\n        .version = std.SemanticVersion{ .major = 1, .minor = 2, .patch = 3 },\n    }, dummyExec);\n    defer root.deinit();\n\n    // Add flags to root\n    try root.addFlags(&[_]Flag{\n        .{\n            .name = \"verbose\",\n            .shortcut = \"v\",\n            .description = \"Verbose output\",\n            .type = .Bool,\n            .default_value = .{ .Bool = false },\n        },\n        .{\n            .name = \"config\",\n            .shortcut = \"c\",\n            .description = \"Config file\",\n            .type = .String,\n            .default_value = .{ .String = \"config.json\" },\n        },\n    });\n\n    // Create subcommand\n    const deploy_cmd = try Command.init(allocator, .{\n        .name = \"deploy\",\n        .description = \"Deploy application\",\n        .shortcut = \"d\",\n    }, dummyExec);\n\n    try deploy_cmd.addPositionalArg(.{\n        .name = \"environment\",\n        .description = \"Target environment\",\n        .required = true,\n    });\n\n    try deploy_cmd.addFlag(.{\n        .name = \"force\",\n        .shortcut = \"f\",\n        .description = \"Force deployment\",\n        .type = .Bool,\n        .default_value = .{ .Bool = false },\n    });\n\n    try root.addCommand(deploy_cmd);\n\n    // Test command finding\n    try testing.expect(root.findCommand(\"deploy\") == deploy_cmd);\n    try testing.expect(root.findCommand(\"d\") == deploy_cmd);\n\n    // Test flag inheritance and parsing would need more complex setup\n    // This demonstrates the structure for integration testing\n}\n\n// ERROR HANDLING TESTS\ntest \"error handling: unknown flag\" {\n    const allocator = testing.allocator;\n    const cmd = try Command.init(allocator, .{\n        .name = \"test\",\n        .description = \"Test\",\n    }, dummyExec);\n    defer cmd.deinit();\n\n    // In a real scenario, this would trigger process.exit(1)\n    // For testing, we'd need to capture stderr or modify the error handling\n    try testing.expect(cmd.findFlag(\"nonexistent\") == null);\n}\n\ntest \"error handling: invalid flag value type\" {\n    const int_flag = Flag{\n        .name = \"count\",\n        .description = \"Count\",\n        .type = .Int,\n        .default_value = .{ .Int = 0 },\n    };\n\n    try testing.expectError(error.InvalidFlagValue, int_flag.safeEvaluate(\"not_a_number\"));\n}\n\n// UTILITY FUNCTIONS FOR TESTS\nfn dummyExec(_: CommandContext) !void {\n    // Do nothing - just for testing\n}\n", "middle_code": "fn checkDeprecated(self: *const Command) !void {\n        if (self.options.deprecated) {\n            if (self.options.version) |version| {\n                try self.stdout.print(\"'{s}' v{} is deprecated\\n\", .{ self.options.name, version });\n            } else {\n                try self.stdout.print(\"'{s}' is deprecated\\n\", .{self.options.name});\n            }\n            if (self.options.replaced_by) |new_cmd_name| {\n                try self.stdout.print(\"\\nUse '{s}' instead.\\n\", .{new_cmd_name});\n            }\n            return error.CommandDeprecated;\n        }\n    }", "code_description": null, "fill_type": "FUNCTION_TYPE", "language_type": "zig", "sub_task_type": null}, "context_code": [["/zli/src/lib/spin.zig", "const std = @import(\"std\");\nconst builtin = @import(\"builtin.zig\");\npub const styles = builtin.styles;\n\nvar g_active_spinner: ?*Spinner = null;\n\n/// Progress indicator for long running operations\nconst Spinner = @This();\n\nconst Writer = @TypeOf(std.io.getStdOut().writer());\nconst Allocator = std.mem.Allocator;\n\n/// The state of an individual line managed by the spinner.\nconst LineState = enum {\n    spinning,\n    succeeded,\n    failed,\n    info,\n    /// A static line (e.g., a log) that is not part of the active spinner.\n    preserved,\n};\n\nconst SpinnerLine = struct {\n    message: []const u8,\n    state: LineState,\n};\n\npub const SpinnerStyles = struct {\n    pub const dots = &.{ \"β ‹\", \"β ™\", \"β Ή\", \"β Έ\", \"β Ό\", \"β ΄\", \"β ¦\", \"β §\", \"β ‡\", \"⠏\" };\n    pub const dots2 = &.{ \"β£Ύ\", \"β£½\", \"β£»\", \"β’Ώ\", \"β‘Ώ\", \"⣟\", \"β£―\", \"β£·\" };\n    pub const circle = &.{ \"◐\", \"β—“\", \"β—‘\", \"β—’\" };\n    pub const line = &.{ \"-\", \"\\\\\", \"|\", \"/\" };\n    pub const simple_dots_scrolling = &.{ \".  \", \".. \", \"...\", \" ..\", \"  .\", \"   \" };\n    pub const star = &.{ \"✢\", \"✸\", \"✹\", \"✺\", \"✹\", \"✷\" };\n    pub const clock = &.{ \"πŸ•›\", \"πŸ•\", \"πŸ•‘\", \"πŸ•’\", \"πŸ•“\", \"πŸ•”\", \"πŸ••\", \"πŸ•–\", \"πŸ•—\", \"πŸ•˜\", \"πŸ•™\", \"πŸ•š\" };\n    pub const triangle = &.{ \"β—’\", \"β—£\", \"β—€\", \"β—₯\" };\n    pub const bouncing_bar = &.{ \"[    ]\", \"[=   ]\", \"[==  ]\", \"[=== ]\", \"[ ===]\", \"[  ==]\", \"[   =]\", \"[    ]\", \"[   =]\", \"[  ==]\", \"[ ===]\", \"[====]\", \"[=== ]\", \"[==  ]\", \"[=   ]\" };\n    pub const grow_vertical = &.{ \" \", \"β–ƒ\", \"β–„\", \"β–…\", \"β–†\", \"β–‡\", \"β–†\", \"β–…\", \"β–„\", \"β–ƒ\" };\n};\n\npub const SpinnerOptions = struct {\n    frames: []const []const u8 = SpinnerStyles.dots,\n    interval_ms: u64 = 80,\n};\n\nframes: []const []const u8,\ninterval: u64,\n\nlines: std.ArrayList(SpinnerLine),\nis_running: std.atomic.Value(bool),\nthread: ?std.Thread = null,\nframe_index: usize = 0,\nlines_drawn: usize = 0,\n\nwriter: Writer,\nmutex: std.Thread.Mutex = .{},\nallocator: Allocator,\n\n// For signal handling\nprev_handler: std.posix.Sigaction,\nhandler_installed: bool,\n\n/// Initialize a new spinner. Does not start it.\npub fn init(allocator: Allocator, options: SpinnerOptions) !*Spinner {\n    const spinner = try allocator.create(Spinner);\n\n    const owned_frames = try allocator.dupe([]const u8, options.frames);\n    errdefer allocator.free(owned_frames);\n\n    spinner.* = Spinner{\n        .allocator = allocator,\n        .writer = std.io.getStdOut().writer(),\n        .frames = owned_frames,\n        .interval = options.interval_ms * std.time.ns_per_ms,\n        .is_running = std.atomic.Value(bool).init(false),\n        .lines = std.ArrayList(SpinnerLine).init(allocator),\n        .prev_handler = undefined,\n        .handler_installed = false,\n    };\n\n    if (g_active_spinner == null) {\n        g_active_spinner = spinner;\n        var new_action: std.posix.Sigaction = .{\n            .handler = .{ .handler = handleInterrupt },\n            .mask = std.posix.empty_sigset, // Use std.posix\n            .flags = 0,\n        };\n        std.posix.sigaction(std.posix.SIG.INT, &new_action, &spinner.prev_handler);\n        spinner.handler_installed = true;\n    }\n\n    return spinner;\n}\n\n/// Stops the spinner if it's running and frees allocated memory.\npub fn deinit(self: *Spinner) void {\n    if (self.handler_installed) {\n        std.posix.sigaction(std.posix.SIG.INT, &self.prev_handler, null);\n        g_active_spinner = null;\n        self.handler_installed = false;\n    }\n    if (self.is_running.load(.monotonic)) {\n        self.stop() catch {};\n    }\n    for (self.lines.items) |line| self.allocator.free(line.message);\n    self.lines.deinit();\n    self.allocator.free(self.frames);\n    self.allocator.destroy(self);\n}\n\n/// Starts the spinner animation in a background thread.\npub fn start(self: *Spinner, options: SpinnerOptions, comptime format: []const u8, args: anytype) !void {\n    if (self.is_running.load(.monotonic)) return; // Already running\n\n    self.mutex.lock();\n    defer self.mutex.unlock();\n\n    // Free the old frames and duplicate the new ones\n    self.allocator.free(self.frames);\n    self.frames = try self.allocator.dupe([]const u8, options.frames);\n    self.interval = options.interval_ms * std.time.ns_per_ms;\n\n    // Clear any previous state\n    for (self.lines.items) |line| self.allocator.free(line.message);\n    self.lines.clearRetainingCapacity();\n    self.lines_drawn = 0;\n    self.frame_index = 0;\n\n    const message = try std.fmt.allocPrint(self.allocator, format, args);\n    errdefer self.allocator.free(message);\n\n    try self.lines.append(.{ .message = message, .state = .spinning });\n\n    self.is_running.store(true, .release);\n    self.thread = try std.Thread.spawn(.{}, spinLoop, .{self});\n}\n\n/// Stops the spinner animation and waits for the background thread to exit.\npub fn stop(self: *Spinner) !void {\n    if (!self.is_running.load(.monotonic)) return;\n\n    self.is_running.store(false, .release);\n\n    if (self.thread) |t| {\n        t.join();\n        self.thread = null;\n    }\n\n    // Final redraw to leave terminal clean\n    try self.render(false);\n    // Show cursor\n    try self.writer.print(\"\\x1b[?25h\", .{});\n}\n\n/// Stops the spinner and marks the final step as successful.\npub fn succeed(self: *Spinner, comptime format: []const u8, args: anytype) !void {\n    try self.finalize(.succeeded, format, args);\n}\n\n/// Stops the spinner and marks the final step as failed.\npub fn fail(self: *Spinner, comptime format: []const u8, args: anytype) !void {\n    try self.finalize(.failed, format, args);\n}\n\n/// Stops the spinner and marks the final step with an info icon.\npub fn info(self: *Spinner, comptime format: []const u8, args: anytype) !void {\n    try self.finalize(.info, format, args);\n}\n\n/// Updates the text of the current spinning line.\npub fn updateText(self: *Spinner, comptime format: []const u8, args: anytype) !void {\n    const new_text = try std.fmt.allocPrint(self.allocator, format, args);\n\n    self.mutex.lock();\n    defer self.mutex.unlock();\n\n    if (self.findLastSpinningLine()) |line| {\n        self.allocator.free(line.message);\n        line.message = new_text;\n    } else {\n        // If no spinning line, just free the new text as there's nothing to update.\n        self.allocator.free(new_text);\n    }\n}\n\n/// Marks the current step as successful and starts a new spinning step.\npub fn nextStep(self: *Spinner, comptime format: []const u8, args: anytype) !void {\n    const new_text = try std.fmt.allocPrint(self.allocator, format, args);\n    errdefer self.allocator.free(new_text);\n\n    self.mutex.lock();\n    defer self.mutex.unlock();\n\n    if (self.findLastSpinningLine()) |line| {\n        line.state = .succeeded;\n    }\n\n    try self.lines.append(.{ .message = new_text, .state = .spinning });\n}\n\n/// Adds a static, preserved line of text (like a log) above the spinner.\n/// It will be printed on the next frame and will not be cleared.\npub fn addLine(self: *Spinner, comptime format: []const u8, args: anytype) !void {\n    const message = try std.fmt.allocPrint(self.allocator, format, args);\n\n    self.mutex.lock();\n    defer self.mutex.unlock();\n\n    // To preserve a line, we insert it *before* the first spinning line.\n    var i: usize = 0;\n    while (i < self.lines.items.len) : (i += 1) {\n        if (self.lines.items[i].state == .spinning) break;\n    }\n\n    try self.lines.insert(i, .{ .message = message, .state = .preserved });\n}\n\nfn finalize(self: *Spinner, final_state: LineState, comptime format: []const u8, args: anytype) !void {\n    self.mutex.lock();\n    const new_text = try std.fmt.allocPrint(self.allocator, format, args);\n    if (self.findLastSpinningLine()) |line| {\n        self.allocator.free(line.message);\n        line.message = new_text;\n        line.state = final_state;\n    } else {\n        // If there was no spinning line, create one with the final state.\n        try self.lines.append(.{ .message = new_text, .state = final_state });\n    }\n    self.mutex.unlock();\n    try self.stop();\n}\n\n/// Finds the last line that is currently in a 'spinning' state.\nfn findLastSpinningLine(self: *Spinner) ?*SpinnerLine {\n    // Use a while loop for safe backward iteration.\n    var i = self.lines.items.len;\n    while (i > 0) {\n        i -= 1;\n        if (self.lines.items[i].state == .spinning) {\n            return &self.lines.items[i];\n        }\n    }\n    return null;\n}\n\n/// Erases the lines drawn in the previous frame.\nfn erase(self: *Spinner) !void {\n    if (self.lines_drawn == 0) return;\n    // Move cursor up N lines\n    try self.writer.print(\"\\r\\x1b[{d}A\", .{self.lines_drawn});\n    // Clear from cursor to end of screen\n    try self.writer.print(\"\\x1b[J\", .{});\n}\n\n/// Renders all lines based on their current state.\nfn render(self: *Spinner, is_spinning: bool) !void {\n    self.mutex.lock();\n    defer self.mutex.unlock();\n\n    // Erase previous spinner output\n    try self.erase();\n\n    const lines_to_draw = self.lines.items;\n    var drawn_count: usize = 0;\n\n    for (lines_to_draw) |line| {\n        // The .spinning state is the only one with a dynamic (runtime) prefix.\n        // We must handle it separately to avoid illegal comptime concatenation.\n        if (line.state == .spinning and is_spinning) {\n            const frame = self.frames[self.frame_index];\n            // Use runtime formatting to combine the parts.\n            try self.writer.print(\"{s}{s}{s} {s}\\n\", .{ styles.CYAN, frame, styles.RESET, line.message });\n        } else {\n            // All other cases use static prefixes that can be concatenated at compile-time.\n            // This includes the \"paused\" spinning state.\n            const prefix = switch (line.state) {\n                .spinning => styles.CYAN ++ \" \" ++ styles.RESET, // Paused state icon\n                .succeeded => styles.GREEN ++ \"βœ”\" ++ styles.RESET,\n                .failed => styles.RED ++ \"βœ–\" ++ styles.RESET,\n                .info => styles.BLUE ++ \"β„Ή\" ++ styles.RESET,\n                .preserved => styles.DIM ++ \"Β»\" ++ styles.RESET,\n            };\n            try self.writer.print(\"{s} {s}\\n\", .{ prefix, line.message });\n        }\n        drawn_count += 1;\n    }\n\n    self.lines_drawn = drawn_count;\n\n    if (is_spinning) {\n        self.frame_index = (self.frame_index + 1) % self.frames.len;\n    }\n}\n\n/// The main loop for the background thread.\nfn spinLoop(self: *Spinner) void {\n    // Hide cursor\n    self.writer.print(\"\\x1b[?25l\", .{}) catch return;\n    defer self.writer.print(\"\\x1b[?25h\", .{}) catch {};\n\n    while (self.is_running.load(.acquire)) {\n        self.render(true) catch {\n            // If rendering fails, stop the spinner to prevent broken output.\n            self.is_running.store(false, .release);\n            break;\n        };\n        std.time.sleep(self.interval);\n        // try std.Thread.yield();\n    }\n}\n\nfn handleInterrupt(signum: c_int) callconv(.c) void {\n    _ = signum; // We know it's SIGINT but don't need to use the value.\n\n    if (g_active_spinner) |spinner| {\n        spinner.stop() catch {};\n    }\n\n    // After cleanup, exit the program with an error code\n    // indicating it was interrupted. 130 is the standard for this.\n    std.process.exit(130);\n}\n"], ["/zli/src/lib/builtin.zig", "pub const styles = struct {\n    // Reset\n    pub const RESET = \"\\x1b[0m\";\n\n    // Text Styles\n    pub const BOLD = \"\\x1b[1m\";\n    pub const DIM = \"\\x1b[2m\";\n    pub const ITALIC = \"\\x1b[3m\";\n    pub const UNDERLINE = \"\\x1b[4m\";\n    pub const INVERSE = \"\\x1b[7m\";\n    pub const HIDDEN = \"\\x1b[8m\";\n    pub const STRIKETHROUGH = \"\\x1b[9m\";\n\n    // Foreground Colors\n    pub const BLACK = \"\\x1b[30m\";\n    pub const RED = \"\\x1b[31m\";\n    pub const GREEN = \"\\x1b[32m\";\n    pub const YELLOW = \"\\x1b[33m\";\n    pub const BLUE = \"\\x1b[34m\";\n    pub const MAGENTA = \"\\x1b[35m\";\n    pub const CYAN = \"\\x1b[36m\";\n    pub const WHITE = \"\\x1b[37m\";\n\n    // Bright Foreground Colors\n    pub const BRIGHT_BLACK = \"\\x1b[90m\";\n    pub const BRIGHT_RED = \"\\x1b[91m\";\n    pub const BRIGHT_GREEN = \"\\x1b[92m\";\n    pub const BRIGHT_YELLOW = \"\\x1b[93m\";\n    pub const BRIGHT_BLUE = \"\\x1b[94m\";\n    pub const BRIGHT_MAGENTA = \"\\x1b[95m\";\n    pub const BRIGHT_CYAN = \"\\x1b[96m\";\n    pub const BRIGHT_WHITE = \"\\x1b[97m\";\n\n    // Background Colors\n    pub const BG_BLACK = \"\\x1b[40m\";\n    pub const BG_RED = \"\\x1b[41m\";\n    pub const BG_GREEN = \"\\x1b[42m\";\n    pub const BG_YELLOW = \"\\x1b[43m\";\n    pub const BG_BLUE = \"\\x1b[44m\";\n    pub const BG_MAGENTA = \"\\x1b[45m\";\n    pub const BG_CYAN = \"\\x1b[46m\";\n    pub const BG_WHITE = \"\\x1b[47m\";\n\n    // Bright Background Colors\n    pub const BG_BRIGHT_BLACK = \"\\x1b[100m\";\n    pub const BG_BRIGHT_RED = \"\\x1b[101m\";\n    pub const BG_BRIGHT_GREEN = \"\\x1b[102m\";\n    pub const BG_BRIGHT_YELLOW = \"\\x1b[103m\";\n    pub const BG_BRIGHT_BLUE = \"\\x1b[104m\";\n    pub const BG_BRIGHT_MAGENTA = \"\\x1b[105m\";\n    pub const BG_BRIGHT_CYAN = \"\\x1b[106m\";\n    pub const BG_BRIGHT_WHITE = \"\\x1b[107m\";\n};\n"]], "task_instance_info": {"created_time": "2025-08-21 03:48:33", "created_task_model": "DeepSeek-R1", "function_skeleton": "fn checkDeprecated(self: *const Command) !void { }", "code_description": "This code segment functions to: Verify if a command has been marked as obsolete, notify the user with configured deprecation messages, and provide migration guidance when alternative commands are specified."}, "inference_content": {"inference_model": "DeepSeek-R1", "inference_result": "```zig\n[TASK_BEGIN]\n    fn checkDeprecated(self: *const Command) !void {\n        if (self.options.deprecated) {\n            const stderr = self.stderr;\n\n            // Collect parent command names\n            var parents = std.ArrayList([]const u8).init(self.allocator);\n            defer parents.deinit();\n\n            var current = self.parent;\n            while (current) |cmd| {\n                try parents.append(cmd.options.name);\n                current = cmd.parent;\n            }\n\n            // Reverse to get root -> parent order\n            std.mem.reverse([]const u8, parents.items);\n\n            // Build full command path string\n            var full_cmd = std.ArrayList(u8).init(self.allocator);\n            defer full_cmd.deinit();\n            const writer = full_cmd.writer();\n\n            for (parents.items) |name| {\n                try writer.print(\"{s} \", .{name});\n            }\n            try writer.print(\"{s}\", .{self.options.name});\n\n            // Print styled warning message\n            try stderr.print(\"{s}Warning: Command '{s}' is deprecated{s}\", .{\n                styles.YELLOW,\n                full_cmd.items,\n                styles.RESET,\n            });\n\n            // Add replacement hint if available\n            if (self.options.replaced_by) |replaced| {\n                try stderr.print(\" - please use '{s}{s}{s}' instead\", .{\n                    styles.BOLD, replaced, styles.RESET\n                });\n            }\n\n            try stderr.print(\"\\n\", .{});\n        }\n    }\n[TASK_END]\n```", "inference_time": "2025-08-21 03-49-47"}, "editdistance_info": {"edit_distance": 25.098, "calculate_time": "2025-08-21 03:49:47", "true_code_clean": "fn checkDeprecated(self: *const Command) !void {\n        if (self.options.deprecated) {\n            if (self.options.version) |version| {\n                try self.stdout.print(\"'{s}' v{} is deprecated\\n\", .{ self.options.name, version });\n            } else {\n                try self.stdout.print(\"'{s}' is deprecated\\n\", .{self.options.name});\n            }\n            if (self.options.replaced_by) |new_cmd_name| {\n                try self.stdout.print(\"\\nUse '{s}' instead.\\n\", .{new_cmd_name});\n            }\n            return error.CommandDeprecated;\n        }\n    }", "predict_code_clean": "fn checkDeprecated(self: *const Command) !void {\n        if (self.options.deprecated) {\n            const stderr = self.stderr;\n            var parents = std.ArrayList([]const u8).init(self.allocator);\n            defer parents.deinit();\n            var current = self.parent;\n            while (current) |cmd| {\n                try parents.append(cmd.options.name);\n                current = cmd.parent;\n            }\n            std.mem.reverse([]const u8, parents.items);\n            var full_cmd = std.ArrayList(u8).init(self.allocator);\n            defer full_cmd.deinit();\n            const writer = full_cmd.writer();\n            for (parents.items) |name| {\n                try writer.print(\"{s} \", .{name});\n            }\n            try writer.print(\"{s}\", .{self.options.name});\n            try stderr.print(\"{s}Warning: Command '{s}' is deprecated{s}\", .{\n                styles.YELLOW,\n                full_cmd.items,\n                styles.RESET,\n            });\n            if (self.options.replaced_by) |replaced| {\n                try stderr.print(\" - please use '{s}{s}{s}' instead\", .{\n                    styles.BOLD, replaced, styles.RESET\n                });\n            }\n            try stderr.print(\"\\n\", .{});\n        }\n    }"}}