diff --git a/cmd/cbzoptimizer/commands/flags.go b/cmd/cbzoptimizer/commands/flags.go index 6bd6db6..81b26fb 100644 --- a/cmd/cbzoptimizer/commands/flags.go +++ b/cmd/cbzoptimizer/commands/flags.go @@ -30,3 +30,71 @@ func setupFormatFlag(cmd *cobra.Command, converterType *constant.ConversionForma _ = viper.BindPFlag("format", cmd.Flags().Lookup("format")) } } + +// setupQualityFlag sets up the quality flag for a command. +// +// Parameters: +// - cmd: The Cobra command to add the quality flag to +// - defaultValue: The default quality value (0-100) +// - bindViper: If true, binds the flag to viper for configuration file support +func setupQualityFlag(cmd *cobra.Command, defaultValue uint8, bindViper bool) { + cmd.Flags().Uint8P("quality", "q", defaultValue, "Quality for conversion (0-100)") + if bindViper { + _ = viper.BindPFlag("quality", cmd.Flags().Lookup("quality")) + } +} + +// setupOverrideFlag sets up the override flag for a command. +// +// Parameters: +// - cmd: The Cobra command to add the override flag to +// - defaultValue: The default override value +// - bindViper: If true, binds the flag to viper for configuration file support +func setupOverrideFlag(cmd *cobra.Command, defaultValue bool, bindViper bool) { + cmd.Flags().BoolP("override", "o", defaultValue, "Override the original CBZ/CBR files") + if bindViper { + _ = viper.BindPFlag("override", cmd.Flags().Lookup("override")) + } +} + +// setupSplitFlag sets up the split flag for a command. +// +// Parameters: +// - cmd: The Cobra command to add the split flag to +// - defaultValue: The default split value +// - bindViper: If true, binds the flag to viper for configuration file support +func setupSplitFlag(cmd *cobra.Command, defaultValue bool, bindViper bool) { + cmd.Flags().BoolP("split", "s", defaultValue, "Split long pages into smaller chunks") + if bindViper { + _ = viper.BindPFlag("split", cmd.Flags().Lookup("split")) + } +} + +// setupTimeoutFlag sets up the timeout flag for a command. +// +// Parameters: +// - cmd: The Cobra command to add the timeout flag to +// - bindViper: If true, binds the flag to viper for configuration file support +func setupTimeoutFlag(cmd *cobra.Command, bindViper bool) { + cmd.Flags().DurationP("timeout", "t", 0, "Maximum time allowed for converting a single chapter (e.g., 30s, 5m, 1h). 0 means no timeout") + if bindViper { + _ = viper.BindPFlag("timeout", cmd.Flags().Lookup("timeout")) + } +} + +// setupCommonFlags sets up all common flags for optimize and watch commands. +// +// Parameters: +// - cmd: The Cobra command to add the flags to +// - converterType: Pointer to the ConversionFormat variable that will store the format flag value +// - qualityDefault: The default quality value (0-100) +// - overrideDefault: The default override value +// - splitDefault: The default split value +// - bindViper: If true, binds all flags to viper for configuration file support +func setupCommonFlags(cmd *cobra.Command, converterType *constant.ConversionFormat, qualityDefault uint8, overrideDefault bool, splitDefault bool, bindViper bool) { + setupFormatFlag(cmd, converterType, bindViper) + setupQualityFlag(cmd, qualityDefault, bindViper) + setupOverrideFlag(cmd, overrideDefault, bindViper) + setupSplitFlag(cmd, splitDefault, bindViper) + setupTimeoutFlag(cmd, bindViper) +} diff --git a/cmd/cbzoptimizer/commands/optimize_command.go b/cmd/cbzoptimizer/commands/optimize_command.go index a512067..0ab6427 100644 --- a/cmd/cbzoptimizer/commands/optimize_command.go +++ b/cmd/cbzoptimizer/commands/optimize_command.go @@ -25,13 +25,11 @@ func init() { Args: cobra.ExactArgs(1), } - setupFormatFlag(command, &converterType, false) - - command.Flags().Uint8P("quality", "q", 85, "Quality for conversion (0-100)") + // Setup common flags (format, quality, override, split, timeout) + setupCommonFlags(command, &converterType, 85, false, false, false) + + // Setup optimize-specific flags command.Flags().IntP("parallelism", "n", 2, "Number of chapters to convert in parallel") - command.Flags().BoolP("override", "o", false, "Override the original CBZ/CBR files") - command.Flags().BoolP("split", "s", false, "Split long pages into smaller chunks") - command.Flags().DurationP("timeout", "t", 0, "Maximum time allowed for converting a single chapter (e.g., 30s, 5m, 1h). 0 means no timeout") AddCommand(command) } diff --git a/cmd/cbzoptimizer/commands/watch_command.go b/cmd/cbzoptimizer/commands/watch_command.go index 0a95016..ad322d5 100644 --- a/cmd/cbzoptimizer/commands/watch_command.go +++ b/cmd/cbzoptimizer/commands/watch_command.go @@ -27,19 +27,8 @@ func init() { Args: cobra.ExactArgs(1), } - setupFormatFlag(command, &converterType, true) - - command.Flags().Uint8P("quality", "q", 85, "Quality for conversion (0-100)") - _ = viper.BindPFlag("quality", command.Flags().Lookup("quality")) - - command.Flags().BoolP("override", "o", true, "Override the original CBZ/CBR files") - _ = viper.BindPFlag("override", command.Flags().Lookup("override")) - - command.Flags().BoolP("split", "s", false, "Split long pages into smaller chunks") - _ = viper.BindPFlag("split", command.Flags().Lookup("split")) - - command.Flags().DurationP("timeout", "t", 0, "Maximum time allowed for converting a single chapter (e.g., 30s, 5m, 1h). 0 means no timeout") - _ = viper.BindPFlag("timeout", command.Flags().Lookup("timeout")) + // Setup common flags (format, quality, override, split, timeout) with viper binding + setupCommonFlags(command, &converterType, 85, true, false, true) AddCommand(command) }