Skip to content

Commit 01ac928

Browse files
committed
wip: add cli arg
1 parent b4e220a commit 01ac928

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

cli/cliarg/cliarg.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Package cliarg extends cobra.Args to allow for custom error messages.
2+
//
3+
// Usage:
4+
//
5+
// cliarg.ExactNamedArgs("workspace", "This command requires a workspace name to be passed. Run this command with '--help' to see an example.")
6+
//
7+
// Will produce the following error message when calling
8+
//
9+
// Error: this command accepts 'workspace' as an argument, received no arguments. Pass in 'workspace' or call command with '--help' to see an example.")
10+
//
11+
package cliflag
12+
13+
import (
14+
"fmt"
15+
16+
"github.com/spf13/cobra"
17+
)
18+
19+
// Expects 1 arg based on namedArg, otherwise returns customErrorMessage
20+
func ExactNamedArg(namedArg string, customErrorMessage string) cobra.PositionalArgs {
21+
return func(cmd *cobra.Command, args []string) error {
22+
expectedNumOfArgs := 1
23+
if len(args) != expectedNumOfArgs {
24+
return fmt.Errorf("this command accepts <%s> as an argument, received no arguments. Pass in arg or call command with '--help' to see an example.", namedArg)
25+
}
26+
return nil
27+
}
28+
}

0 commit comments

Comments
 (0)