github.com/uhthomas/helm@v3.0.0-beta.3+incompatible/cmd/helm/require/args.go (about) 1 /* 2 Copyright The Helm Authors. 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 16 package require 17 18 import ( 19 "github.com/pkg/errors" 20 "github.com/spf13/cobra" 21 ) 22 23 // NoArgs returns an error if any args are included. 24 func NoArgs(cmd *cobra.Command, args []string) error { 25 if len(args) > 0 { 26 return errors.Errorf( 27 "%q accepts no arguments\n\nUsage: %s", 28 cmd.CommandPath(), 29 cmd.UseLine(), 30 ) 31 } 32 return nil 33 } 34 35 // ExactArgs returns an error if there are not exactly n args. 36 func ExactArgs(n int) cobra.PositionalArgs { 37 return func(cmd *cobra.Command, args []string) error { 38 if len(args) != n { 39 return errors.Errorf( 40 "%q requires %d %s\n\nUsage: %s", 41 cmd.CommandPath(), 42 n, 43 pluralize("argument", n), 44 cmd.UseLine(), 45 ) 46 } 47 return nil 48 } 49 } 50 51 // MaximumNArgs returns an error if there are more than N args. 52 func MaximumNArgs(n int) cobra.PositionalArgs { 53 return func(cmd *cobra.Command, args []string) error { 54 if len(args) > n { 55 return errors.Errorf( 56 "%q accepts at most %d %s\n\nUsage: %s", 57 cmd.CommandPath(), 58 n, 59 pluralize("argument", n), 60 cmd.UseLine(), 61 ) 62 } 63 return nil 64 } 65 } 66 67 // MinimumNArgs returns an error if there is not at least N args. 68 func MinimumNArgs(n int) cobra.PositionalArgs { 69 return func(cmd *cobra.Command, args []string) error { 70 if len(args) < n { 71 return errors.Errorf( 72 "%q requires at least %d %s\n\nUsage: %s", 73 cmd.CommandPath(), 74 n, 75 pluralize("argument", n), 76 cmd.UseLine(), 77 ) 78 } 79 return nil 80 } 81 } 82 83 func pluralize(word string, n int) string { 84 if n == 1 { 85 return word 86 } 87 return word + "s" 88 }