2023-05-02 11:33:06 -04:00
|
|
|
// Copyright IBM Corp. 2014, 2026
|
2023-08-10 18:43:27 -04:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-05-02 11:33:06 -04:00
|
|
|
|
2021-02-18 17:23:34 -05:00
|
|
|
package arguments
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"flag"
|
|
|
|
|
"fmt"
|
|
|
|
|
)
|
|
|
|
|
|
2024-04-15 19:28:30 -04:00
|
|
|
// FlagStringSlice is a flag.Value implementation which allows collecting
|
2021-02-18 17:23:34 -05:00
|
|
|
// multiple instances of a single flag into a slice. This is used for flags
|
|
|
|
|
// such as -target=aws_instance.foo and -var x=y.
|
2024-04-15 19:28:30 -04:00
|
|
|
type FlagStringSlice []string
|
2021-02-18 17:23:34 -05:00
|
|
|
|
2024-04-15 19:28:30 -04:00
|
|
|
var _ flag.Value = (*FlagStringSlice)(nil)
|
2021-02-18 17:23:34 -05:00
|
|
|
|
2024-04-15 19:28:30 -04:00
|
|
|
func (v *FlagStringSlice) String() string {
|
2021-02-18 17:23:34 -05:00
|
|
|
return ""
|
|
|
|
|
}
|
2024-04-15 19:28:30 -04:00
|
|
|
func (v *FlagStringSlice) Set(raw string) error {
|
2021-02-18 17:23:34 -05:00
|
|
|
*v = append(*v, raw)
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-15 19:28:30 -04:00
|
|
|
// FlagNameValueSlice is a flag.Value implementation that appends raw flag
|
2021-02-18 17:23:34 -05:00
|
|
|
// names and values to a slice. This is used to collect a sequence of flags
|
|
|
|
|
// with possibly different names, preserving the overall order.
|
2024-04-15 19:28:30 -04:00
|
|
|
type FlagNameValueSlice struct {
|
|
|
|
|
FlagName string
|
|
|
|
|
Items *[]FlagNameValue
|
2021-02-18 17:23:34 -05:00
|
|
|
}
|
|
|
|
|
|
2024-04-15 19:28:30 -04:00
|
|
|
var _ flag.Value = FlagNameValueSlice{}
|
2021-02-18 17:23:34 -05:00
|
|
|
|
2024-04-15 19:28:30 -04:00
|
|
|
func NewFlagNameValueSlice(flagName string) FlagNameValueSlice {
|
2021-02-18 17:23:34 -05:00
|
|
|
var items []FlagNameValue
|
2024-04-15 19:28:30 -04:00
|
|
|
return FlagNameValueSlice{
|
|
|
|
|
FlagName: flagName,
|
|
|
|
|
Items: &items,
|
2021-02-18 17:23:34 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-15 19:28:30 -04:00
|
|
|
func (f FlagNameValueSlice) Empty() bool {
|
|
|
|
|
if f.Items == nil {
|
2021-02-18 17:23:34 -05:00
|
|
|
return true
|
|
|
|
|
}
|
2024-04-15 19:28:30 -04:00
|
|
|
return len(*f.Items) == 0
|
2021-02-18 17:23:34 -05:00
|
|
|
}
|
|
|
|
|
|
2024-04-15 19:28:30 -04:00
|
|
|
func (f FlagNameValueSlice) AllItems() []FlagNameValue {
|
|
|
|
|
if f.Items == nil {
|
2021-02-18 17:23:34 -05:00
|
|
|
return nil
|
|
|
|
|
}
|
2024-04-15 19:28:30 -04:00
|
|
|
return *f.Items
|
2021-02-18 17:23:34 -05:00
|
|
|
}
|
|
|
|
|
|
2024-04-15 19:28:30 -04:00
|
|
|
func (f FlagNameValueSlice) Alias(flagName string) FlagNameValueSlice {
|
|
|
|
|
return FlagNameValueSlice{
|
|
|
|
|
FlagName: flagName,
|
|
|
|
|
Items: f.Items,
|
2021-02-18 17:23:34 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-15 19:28:30 -04:00
|
|
|
func (f FlagNameValueSlice) String() string {
|
2021-02-18 17:23:34 -05:00
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-15 19:28:30 -04:00
|
|
|
func (f FlagNameValueSlice) Set(str string) error {
|
|
|
|
|
*f.Items = append(*f.Items, FlagNameValue{
|
|
|
|
|
Name: f.FlagName,
|
2021-02-18 17:23:34 -05:00
|
|
|
Value: str,
|
|
|
|
|
})
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type FlagNameValue struct {
|
|
|
|
|
Name string
|
|
|
|
|
Value string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (f FlagNameValue) String() string {
|
|
|
|
|
return fmt.Sprintf("%s=%q", f.Name, f.Value)
|
|
|
|
|
}
|
2021-11-19 12:54:06 -05:00
|
|
|
|
|
|
|
|
// FlagIsSet returns whether a flag is explicitly set in a set of flags
|
|
|
|
|
func FlagIsSet(flags *flag.FlagSet, name string) bool {
|
|
|
|
|
isSet := false
|
|
|
|
|
flags.Visit(func(f *flag.Flag) {
|
|
|
|
|
if f.Name == name {
|
|
|
|
|
isSet = true
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
return isSet
|
|
|
|
|
}
|