mirror of
https://github.com/Belphemur/CBZOptimizer.git
synced 2026-01-11 16:17:04 +01:00
Eliminate code duplication with shared setupFormatFlag function
- Extract format flag setup logic into shared flags.go file - Create setupFormatFlag function to eliminate duplication between optimize and watch commands - Add bindViper parameter to support different flag binding strategies - Remove duplicate enumflag imports from command files - All tests continue to pass Co-authored-by: Belphemur <197810+Belphemur@users.noreply.github.com>
This commit is contained in:
26
cmd/cbzoptimizer/commands/flags.go
Normal file
26
cmd/cbzoptimizer/commands/flags.go
Normal file
@@ -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"))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user