diff --git a/cmd/cbzoptimizer/commands/flags.go b/cmd/cbzoptimizer/commands/flags.go new file mode 100644 index 0000000..b6478f2 --- /dev/null +++ b/cmd/cbzoptimizer/commands/flags.go @@ -0,0 +1,26 @@ +package commands + +import ( + "fmt" + + "github.com/belphemur/CBZOptimizer/v2/pkg/converter/constant" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "github.com/thediveo/enumflag/v2" +) + +// setupFormatFlag sets up the format flag for a command +// If bindViper is true, it will also bind the flag to viper +func setupFormatFlag(cmd *cobra.Command, converterType *constant.ConversionFormat, bindViper bool) { + formatFlag := enumflag.New(converterType, "format", constant.CommandValue, enumflag.EnumCaseInsensitive) + _ = formatFlag.RegisterCompletion(cmd, "format", constant.HelpText) + + cmd.Flags().VarP( + formatFlag, + "format", "f", + fmt.Sprintf("Format to convert the images to: %s", constant.ListAll())) + + if bindViper { + _ = viper.BindPFlag("format", cmd.Flags().Lookup("format")) + } +} diff --git a/cmd/cbzoptimizer/commands/optimize_command.go b/cmd/cbzoptimizer/commands/optimize_command.go index 82a483a..a512067 100644 --- a/cmd/cbzoptimizer/commands/optimize_command.go +++ b/cmd/cbzoptimizer/commands/optimize_command.go @@ -12,7 +12,6 @@ import ( "github.com/belphemur/CBZOptimizer/v2/pkg/converter/constant" "github.com/rs/zerolog/log" "github.com/spf13/cobra" - "github.com/thediveo/enumflag/v2" ) var converterType constant.ConversionFormat @@ -25,18 +24,14 @@ func init() { RunE: ConvertCbzCommand, Args: cobra.ExactArgs(1), } - formatFlag := enumflag.New(&converterType, "format", constant.CommandValue, enumflag.EnumCaseInsensitive) - _ = formatFlag.RegisterCompletion(command, "format", constant.HelpText) + + setupFormatFlag(command, &converterType, false) command.Flags().Uint8P("quality", "q", 85, "Quality for conversion (0-100)") 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") - command.Flags().VarP( - formatFlag, - "format", "f", - fmt.Sprintf("Format to convert the images to: %s", constant.ListAll())) AddCommand(command) } diff --git a/cmd/cbzoptimizer/commands/watch_command.go b/cmd/cbzoptimizer/commands/watch_command.go index 8dfcdc4..0a95016 100644 --- a/cmd/cbzoptimizer/commands/watch_command.go +++ b/cmd/cbzoptimizer/commands/watch_command.go @@ -13,7 +13,6 @@ import ( "github.com/rs/zerolog/log" "github.com/spf13/cobra" "github.com/spf13/viper" - "github.com/thediveo/enumflag/v2" ) func init() { @@ -27,8 +26,8 @@ func init() { RunE: WatchCommand, Args: cobra.ExactArgs(1), } - formatFlag := enumflag.New(&converterType, "format", constant.CommandValue, enumflag.EnumCaseInsensitive) - _ = formatFlag.RegisterCompletion(command, "format", constant.HelpText) + + setupFormatFlag(command, &converterType, true) command.Flags().Uint8P("quality", "q", 85, "Quality for conversion (0-100)") _ = viper.BindPFlag("quality", command.Flags().Lookup("quality")) @@ -42,12 +41,6 @@ func init() { 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")) - command.Flags().VarP( - formatFlag, - "format", "f", - fmt.Sprintf("Format to convert the images to: %s", constant.ListAll())) - _ = viper.BindPFlag("format", command.Flags().Lookup("format")) - AddCommand(command) } func WatchCommand(_ *cobra.Command, args []string) error {