Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
mpanchajanya committed Mar 12, 2024
1 parent c84f422 commit 280d857
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions pkg/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,10 @@ func newRootCmd() *cobra.Command {
2. For Plugin commands; Since CLI doesn't parse args and sends all args to plugin this loop will run to verify if verbose flag is set by the user and then passes to plugin.
*/
if cmd.DisableFlagParsing {
parseFlagsForPluginCommands(args)
err := parseFlagsForPluginCommands(args)
if err != nil {
return err
}
}

log.Infof("verbose %v", verbose)
Expand Down Expand Up @@ -331,30 +334,33 @@ func newRootCmd() *cobra.Command {
return rootCmd
}

func parseFlagsForPluginCommands(args []string) {
func parseFlagsForPluginCommands(args []string) error {
for i := 0; i < len(args); i++ {
arg := args[i]
if arg == "--verbose" {
if i+1 < len(args) {
nextArg := args[i+1]
// Check if the next argument is another flag
if strings.HasPrefix(nextArg, "-") || strings.HasPrefix(nextArg, "--") {
log.Errorf("Missing value for verbose flag")
os.Exit(1)
return errors.New("Missing value for verbose flag")
}
// Try to convert the next argument to an integer
// If it's not an integer, CLI doesn't parse the verbose flag
if v, err := strconv.Atoi(nextArg); err == nil {
if v < 0 || v > 9 {
return errors.New("Invalid value for verbose flag. It should be between 0 and 9")
}
verbose = v
}
// Skip the next argument
i++
} else {
log.Errorf("Missing value for verbose flag")
os.Exit(1)
return errors.New("Missing value for verbose flag")

}
}
}
return nil
}

// setLoggerVerbosity sets the verbosity of the logger if TANZU_CLI_LOG_LEVEL is set
Expand Down

0 comments on commit 280d857

Please sign in to comment.