Consolidate all common flags into flags.go

- Create individual setup functions for each common flag (quality, override, split, timeout)
- Create setupCommonFlags function that sets up all common flags in one call
- Simplify optimize_command.go and watch_command.go by using setupCommonFlags
- All flags now centralized in flags.go for better maintainability
- All tests continue to pass

Co-authored-by: Belphemur <197810+Belphemur@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-12-10 20:38:58 +00:00
parent 9b6a733012
commit 44e2469e34
3 changed files with 74 additions and 19 deletions

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}