github.com/panekj/cli@v0.0.0-20230304125325-467dd2f3797e/cli/required.go (about)

     1  package cli
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/pkg/errors"
     7  	"github.com/spf13/cobra"
     8  )
     9  
    10  // NoArgs validates args and returns an error if there are any args
    11  func NoArgs(cmd *cobra.Command, args []string) error {
    12  	if len(args) == 0 {
    13  		return nil
    14  	}
    15  
    16  	if cmd.HasSubCommands() {
    17  		return errors.Errorf("\n" + strings.TrimRight(cmd.UsageString(), "\n"))
    18  	}
    19  
    20  	return errors.Errorf(
    21  		"%q accepts no arguments.\nSee '%s --help'.\n\nUsage:  %s\n\n%s",
    22  		cmd.CommandPath(),
    23  		cmd.CommandPath(),
    24  		cmd.UseLine(),
    25  		cmd.Short,
    26  	)
    27  }
    28  
    29  // RequiresMinArgs returns an error if there is not at least min args
    30  func RequiresMinArgs(min int) cobra.PositionalArgs {
    31  	return func(cmd *cobra.Command, args []string) error {
    32  		if len(args) >= min {
    33  			return nil
    34  		}
    35  		return errors.Errorf(
    36  			"%q requires at least %d %s.\nSee '%s --help'.\n\nUsage:  %s\n\n%s",
    37  			cmd.CommandPath(),
    38  			min,
    39  			pluralize("argument", min),
    40  			cmd.CommandPath(),
    41  			cmd.UseLine(),
    42  			cmd.Short,
    43  		)
    44  	}
    45  }
    46  
    47  // RequiresMaxArgs returns an error if there is not at most max args
    48  func RequiresMaxArgs(max int) cobra.PositionalArgs {
    49  	return func(cmd *cobra.Command, args []string) error {
    50  		if len(args) <= max {
    51  			return nil
    52  		}
    53  		return errors.Errorf(
    54  			"%q requires at most %d %s.\nSee '%s --help'.\n\nUsage:  %s\n\n%s",
    55  			cmd.CommandPath(),
    56  			max,
    57  			pluralize("argument", max),
    58  			cmd.CommandPath(),
    59  			cmd.UseLine(),
    60  			cmd.Short,
    61  		)
    62  	}
    63  }
    64  
    65  // RequiresRangeArgs returns an error if there is not at least min args and at most max args
    66  func RequiresRangeArgs(min int, max int) cobra.PositionalArgs {
    67  	return func(cmd *cobra.Command, args []string) error {
    68  		if len(args) >= min && len(args) <= max {
    69  			return nil
    70  		}
    71  		return errors.Errorf(
    72  			"%q requires at least %d and at most %d %s.\nSee '%s --help'.\n\nUsage:  %s\n\n%s",
    73  			cmd.CommandPath(),
    74  			min,
    75  			max,
    76  			pluralize("argument", max),
    77  			cmd.CommandPath(),
    78  			cmd.UseLine(),
    79  			cmd.Short,
    80  		)
    81  	}
    82  }
    83  
    84  // ExactArgs returns an error if there is not the exact number of args
    85  func ExactArgs(number int) cobra.PositionalArgs {
    86  	return func(cmd *cobra.Command, args []string) error {
    87  		if len(args) == number {
    88  			return nil
    89  		}
    90  		return errors.Errorf(
    91  			"%q requires exactly %d %s.\nSee '%s --help'.\n\nUsage:  %s\n\n%s",
    92  			cmd.CommandPath(),
    93  			number,
    94  			pluralize("argument", number),
    95  			cmd.CommandPath(),
    96  			cmd.UseLine(),
    97  			cmd.Short,
    98  		)
    99  	}
   100  }
   101  
   102  //nolint:unparam
   103  func pluralize(word string, number int) string {
   104  	if number == 1 {
   105  		return word
   106  	}
   107  	return word + "s"
   108  }