-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
50 lines (43 loc) · 1.34 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package flag
import (
"github.com/spf13/cobra"
commonFlag "github.com/hckops/hckctl/internal/command/common/flag"
)
type CommandFlag struct {
Inline bool
Preset string
Inputs []string
}
func addInlineFlag(command *cobra.Command, value *bool) string {
const (
flagName = "inline"
flagUsage = "use inline arguments"
)
command.Flags().BoolVarP(value, flagName, commonFlag.NoneFlagShortHand, false, flagUsage)
return flagName
}
func addPresetFlag(command *cobra.Command, value *string) string {
const (
flagName = "command"
flagUsage = "use preset arguments"
)
command.Flags().StringVarP(value, flagName, commonFlag.NoneFlagShortHand, "", flagUsage)
return flagName
}
func addInputsFlag(command *cobra.Command, value *[]string) string {
const (
flagName = "input"
flagUsage = "override command arguments"
)
command.Flags().StringArrayVarP(value, flagName, commonFlag.NoneFlagShortHand, []string{}, flagUsage)
return flagName
}
func AddCommandFlag(command *cobra.Command) *CommandFlag {
commandFlag := &CommandFlag{}
inlineFlag := addInlineFlag(command, &commandFlag.Inline)
presetFlag := addPresetFlag(command, &commandFlag.Preset)
inputsFlag := addInputsFlag(command, &commandFlag.Inputs)
command.MarkFlagsMutuallyExclusive(inlineFlag, presetFlag)
command.MarkFlagsMutuallyExclusive(inlineFlag, inputsFlag)
return commandFlag
}