commit
ee875e4a71
@ -12,8 +12,7 @@ os:
|
|||||||
- osx
|
- osx
|
||||||
|
|
||||||
env:
|
env:
|
||||||
GO111MODULE=on
|
GO111MODULE=on GOPROXY=https://proxy.golang.org
|
||||||
GOPROXY=https://proxy.golang.org
|
|
||||||
|
|
||||||
cache:
|
cache:
|
||||||
directories:
|
directories:
|
||||||
@ -30,6 +29,8 @@ script:
|
|||||||
- go run build.go test
|
- go run build.go test
|
||||||
- go run build.go gfmrun docs/v1/manual.md
|
- go run build.go gfmrun docs/v1/manual.md
|
||||||
- go run build.go toc docs/v1/manual.md
|
- go run build.go toc docs/v1/manual.md
|
||||||
|
- go run build.go gfmrun docs/v2/manual.md
|
||||||
|
- go run build.go toc docs/v2/manual.md
|
||||||
|
|
||||||
after_success:
|
after_success:
|
||||||
- bash <(curl -s https://codecov.io/bash)
|
- bash <(curl -s https://codecov.io/bash)
|
||||||
|
131
altsrc/flag.go
131
altsrc/flag.go
@ -2,11 +2,11 @@ package altsrc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FlagInputSourceExtension is an extension interface of cli.Flag that
|
// FlagInputSourceExtension is an extension interface of cli.Flag that
|
||||||
@ -65,15 +65,15 @@ func InitInputSourceWithContext(flags []cli.Flag, createInputSource func(context
|
|||||||
// ApplyInputSourceValue applies a generic value to the flagSet if required
|
// ApplyInputSourceValue applies a generic value to the flagSet if required
|
||||||
func (f *GenericFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
func (f *GenericFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
||||||
if f.set != nil {
|
if f.set != nil {
|
||||||
if !context.IsSet(f.Name) && !isEnvVarSet(f.EnvVar) {
|
if !context.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) {
|
||||||
value, err := isc.Generic(f.GenericFlag.Name)
|
value, err := isc.Generic(f.GenericFlag.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if value != nil {
|
if value != nil {
|
||||||
eachName(f.Name, func(name string) {
|
for _, name := range f.Names() {
|
||||||
_ = f.set.Set(f.Name, value.String())
|
_ = f.set.Set(name, value.String())
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -84,19 +84,19 @@ func (f *GenericFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourc
|
|||||||
// ApplyInputSourceValue applies a StringSlice value to the flagSet if required
|
// ApplyInputSourceValue applies a StringSlice value to the flagSet if required
|
||||||
func (f *StringSliceFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
func (f *StringSliceFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
||||||
if f.set != nil {
|
if f.set != nil {
|
||||||
if !context.IsSet(f.Name) && !isEnvVarSet(f.EnvVar) {
|
if !context.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) {
|
||||||
value, err := isc.StringSlice(f.StringSliceFlag.Name)
|
value, err := isc.StringSlice(f.StringSliceFlag.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if value != nil {
|
if value != nil {
|
||||||
var sliceValue cli.StringSlice = value
|
var sliceValue cli.StringSlice = *(cli.NewStringSlice(value...))
|
||||||
eachName(f.Name, func(name string) {
|
for _, name := range f.Names() {
|
||||||
underlyingFlag := f.set.Lookup(f.Name)
|
underlyingFlag := f.set.Lookup(name)
|
||||||
if underlyingFlag != nil {
|
if underlyingFlag != nil {
|
||||||
underlyingFlag.Value = &sliceValue
|
underlyingFlag.Value = &sliceValue
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -106,19 +106,19 @@ func (f *StringSliceFlag) ApplyInputSourceValue(context *cli.Context, isc InputS
|
|||||||
// ApplyInputSourceValue applies a IntSlice value if required
|
// ApplyInputSourceValue applies a IntSlice value if required
|
||||||
func (f *IntSliceFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
func (f *IntSliceFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
||||||
if f.set != nil {
|
if f.set != nil {
|
||||||
if !context.IsSet(f.Name) && !isEnvVarSet(f.EnvVar) {
|
if !context.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) {
|
||||||
value, err := isc.IntSlice(f.IntSliceFlag.Name)
|
value, err := isc.IntSlice(f.IntSliceFlag.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if value != nil {
|
if value != nil {
|
||||||
var sliceValue cli.IntSlice = value
|
var sliceValue cli.IntSlice = *(cli.NewIntSlice(value...))
|
||||||
eachName(f.Name, func(name string) {
|
for _, name := range f.Names() {
|
||||||
underlyingFlag := f.set.Lookup(f.Name)
|
underlyingFlag := f.set.Lookup(name)
|
||||||
if underlyingFlag != nil {
|
if underlyingFlag != nil {
|
||||||
underlyingFlag.Value = &sliceValue
|
underlyingFlag.Value = &sliceValue
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -128,33 +128,15 @@ func (f *IntSliceFlag) ApplyInputSourceValue(context *cli.Context, isc InputSour
|
|||||||
// ApplyInputSourceValue applies a Bool value to the flagSet if required
|
// ApplyInputSourceValue applies a Bool value to the flagSet if required
|
||||||
func (f *BoolFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
func (f *BoolFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
||||||
if f.set != nil {
|
if f.set != nil {
|
||||||
if !context.IsSet(f.Name) && !isEnvVarSet(f.EnvVar) {
|
if !context.IsSet(f.Name) && !isEnvVarSet(f.EnvVars) {
|
||||||
value, err := isc.Bool(f.BoolFlag.Name)
|
value, err := isc.Bool(f.BoolFlag.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if value {
|
if value {
|
||||||
eachName(f.Name, func(name string) {
|
for _, name := range f.Names() {
|
||||||
_ = f.set.Set(f.Name, strconv.FormatBool(value))
|
_ = f.set.Set(name, strconv.FormatBool(value))
|
||||||
})
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ApplyInputSourceValue applies a BoolT value to the flagSet if required
|
|
||||||
func (f *BoolTFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
|
||||||
if f.set != nil {
|
|
||||||
if !context.IsSet(f.Name) && !isEnvVarSet(f.EnvVar) {
|
|
||||||
value, err := isc.BoolT(f.BoolTFlag.Name)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if !value {
|
|
||||||
eachName(f.Name, func(name string) {
|
|
||||||
_ = f.set.Set(f.Name, strconv.FormatBool(value))
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -164,15 +146,43 @@ func (f *BoolTFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceC
|
|||||||
// ApplyInputSourceValue applies a String value to the flagSet if required
|
// ApplyInputSourceValue applies a String value to the flagSet if required
|
||||||
func (f *StringFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
func (f *StringFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
||||||
if f.set != nil {
|
if f.set != nil {
|
||||||
if !(context.IsSet(f.Name) || isEnvVarSet(f.EnvVar)) {
|
if !(context.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) {
|
||||||
value, err := isc.String(f.StringFlag.Name)
|
value, err := isc.String(f.StringFlag.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if value != "" {
|
if value != "" {
|
||||||
eachName(f.Name, func(name string) {
|
for _, name := range f.Names() {
|
||||||
_ = f.set.Set(f.Name, value)
|
_ = f.set.Set(name, value)
|
||||||
})
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ApplyInputSourceValue applies a Path value to the flagSet if required
|
||||||
|
func (f *PathFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
||||||
|
if f.set != nil {
|
||||||
|
if !(context.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) {
|
||||||
|
value, err := isc.String(f.PathFlag.Name)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if value != "" {
|
||||||
|
for _, name := range f.Names() {
|
||||||
|
|
||||||
|
if !filepath.IsAbs(value) && isc.Source() != "" {
|
||||||
|
basePathAbs, err := filepath.Abs(isc.Source())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
value = filepath.Join(filepath.Dir(basePathAbs), value)
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = f.set.Set(name, value)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -182,15 +192,15 @@ func (f *StringFlag) ApplyInputSourceValue(context *cli.Context, isc InputSource
|
|||||||
// ApplyInputSourceValue applies a int value to the flagSet if required
|
// ApplyInputSourceValue applies a int value to the flagSet if required
|
||||||
func (f *IntFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
func (f *IntFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
||||||
if f.set != nil {
|
if f.set != nil {
|
||||||
if !(context.IsSet(f.Name) || isEnvVarSet(f.EnvVar)) {
|
if !(context.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) {
|
||||||
value, err := isc.Int(f.IntFlag.Name)
|
value, err := isc.Int(f.IntFlag.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if value > 0 {
|
if value > 0 {
|
||||||
eachName(f.Name, func(name string) {
|
for _, name := range f.Names() {
|
||||||
_ = f.set.Set(f.Name, strconv.FormatInt(int64(value), 10))
|
_ = f.set.Set(name, strconv.FormatInt(int64(value), 10))
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -200,15 +210,15 @@ func (f *IntFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceCon
|
|||||||
// ApplyInputSourceValue applies a Duration value to the flagSet if required
|
// ApplyInputSourceValue applies a Duration value to the flagSet if required
|
||||||
func (f *DurationFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
func (f *DurationFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
||||||
if f.set != nil {
|
if f.set != nil {
|
||||||
if !(context.IsSet(f.Name) || isEnvVarSet(f.EnvVar)) {
|
if !(context.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) {
|
||||||
value, err := isc.Duration(f.DurationFlag.Name)
|
value, err := isc.Duration(f.DurationFlag.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if value > 0 {
|
if value > 0 {
|
||||||
eachName(f.Name, func(name string) {
|
for _, name := range f.Names() {
|
||||||
_ = f.set.Set(f.Name, value.String())
|
_ = f.set.Set(name, value.String())
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -218,25 +228,24 @@ func (f *DurationFlag) ApplyInputSourceValue(context *cli.Context, isc InputSour
|
|||||||
// ApplyInputSourceValue applies a Float64 value to the flagSet if required
|
// ApplyInputSourceValue applies a Float64 value to the flagSet if required
|
||||||
func (f *Float64Flag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
func (f *Float64Flag) ApplyInputSourceValue(context *cli.Context, isc InputSourceContext) error {
|
||||||
if f.set != nil {
|
if f.set != nil {
|
||||||
if !(context.IsSet(f.Name) || isEnvVarSet(f.EnvVar)) {
|
if !(context.IsSet(f.Name) || isEnvVarSet(f.EnvVars)) {
|
||||||
value, err := isc.Float64(f.Float64Flag.Name)
|
value, err := isc.Float64(f.Float64Flag.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if value > 0 {
|
if value > 0 {
|
||||||
floatStr := float64ToString(value)
|
floatStr := float64ToString(value)
|
||||||
eachName(f.Name, func(name string) {
|
for _, name := range f.Names() {
|
||||||
_ = f.set.Set(f.Name, floatStr)
|
_ = f.set.Set(name, floatStr)
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func isEnvVarSet(envVars string) bool {
|
func isEnvVarSet(envVars []string) bool {
|
||||||
for _, envVar := range strings.Split(envVars, ",") {
|
for _, envVar := range envVars {
|
||||||
envVar = strings.TrimSpace(envVar)
|
|
||||||
if _, ok := syscall.Getenv(envVar); ok {
|
if _, ok := syscall.Getenv(envVar); ok {
|
||||||
// TODO: Can't use this for bools as
|
// TODO: Can't use this for bools as
|
||||||
// set means that it was true or false based on
|
// set means that it was true or false based on
|
||||||
@ -251,11 +260,3 @@ func isEnvVarSet(envVars string) bool {
|
|||||||
func float64ToString(f float64) string {
|
func float64ToString(f float64) string {
|
||||||
return fmt.Sprintf("%v", f)
|
return fmt.Sprintf("%v", f)
|
||||||
}
|
}
|
||||||
|
|
||||||
func eachName(longName string, fn func(string)) {
|
|
||||||
parts := strings.Split(longName, ",")
|
|
||||||
for _, name := range parts {
|
|
||||||
name = strings.Trim(name, " ")
|
|
||||||
fn(name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -4,343 +4,271 @@ package altsrc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// BoolFlag is the flag type that wraps cli.BoolFlag to allow
|
// BoolFlag is the flag type that wraps cli.BoolFlag to allow
|
||||||
// for other values to be specified
|
// for other values to be specified
|
||||||
type BoolFlag struct {
|
type BoolFlag struct {
|
||||||
cli.BoolFlag
|
*cli.BoolFlag
|
||||||
set *flag.FlagSet
|
set *flag.FlagSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBoolFlag creates a new BoolFlag
|
// NewBoolFlag creates a new BoolFlag
|
||||||
func NewBoolFlag(fl cli.BoolFlag) *BoolFlag {
|
func NewBoolFlag(fl *cli.BoolFlag) *BoolFlag {
|
||||||
return &BoolFlag{ BoolFlag: fl, set: nil }
|
return &BoolFlag{BoolFlag: fl, set: nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply saves the flagSet for later usage calls, then calls
|
// Apply saves the flagSet for later usage calls, then calls
|
||||||
// the wrapped BoolFlag.Apply
|
// the wrapped BoolFlag.Apply
|
||||||
func (f *BoolFlag) Apply(set *flag.FlagSet) {
|
func (f *BoolFlag) Apply(set *flag.FlagSet) error {
|
||||||
f.set = set
|
f.set = set
|
||||||
f.BoolFlag.Apply(set)
|
return f.BoolFlag.Apply(set)
|
||||||
}
|
|
||||||
|
|
||||||
// ApplyWithError saves the flagSet for later usage calls, then calls
|
|
||||||
// the wrapped BoolFlag.ApplyWithError
|
|
||||||
func (f *BoolFlag) ApplyWithError(set *flag.FlagSet) error {
|
|
||||||
f.set = set
|
|
||||||
return f.BoolFlag.ApplyWithError(set)
|
|
||||||
}
|
|
||||||
|
|
||||||
// BoolTFlag is the flag type that wraps cli.BoolTFlag to allow
|
|
||||||
// for other values to be specified
|
|
||||||
type BoolTFlag struct {
|
|
||||||
cli.BoolTFlag
|
|
||||||
set *flag.FlagSet
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewBoolTFlag creates a new BoolTFlag
|
|
||||||
func NewBoolTFlag(fl cli.BoolTFlag) *BoolTFlag {
|
|
||||||
return &BoolTFlag{ BoolTFlag: fl, set: nil }
|
|
||||||
}
|
|
||||||
|
|
||||||
// Apply saves the flagSet for later usage calls, then calls
|
|
||||||
// the wrapped BoolTFlag.Apply
|
|
||||||
func (f *BoolTFlag) Apply(set *flag.FlagSet) {
|
|
||||||
f.set = set
|
|
||||||
f.BoolTFlag.Apply(set)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ApplyWithError saves the flagSet for later usage calls, then calls
|
|
||||||
// the wrapped BoolTFlag.ApplyWithError
|
|
||||||
func (f *BoolTFlag) ApplyWithError(set *flag.FlagSet) error {
|
|
||||||
f.set = set
|
|
||||||
return f.BoolTFlag.ApplyWithError(set)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DurationFlag is the flag type that wraps cli.DurationFlag to allow
|
// DurationFlag is the flag type that wraps cli.DurationFlag to allow
|
||||||
// for other values to be specified
|
// for other values to be specified
|
||||||
type DurationFlag struct {
|
type DurationFlag struct {
|
||||||
cli.DurationFlag
|
*cli.DurationFlag
|
||||||
set *flag.FlagSet
|
set *flag.FlagSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDurationFlag creates a new DurationFlag
|
// NewDurationFlag creates a new DurationFlag
|
||||||
func NewDurationFlag(fl cli.DurationFlag) *DurationFlag {
|
func NewDurationFlag(fl *cli.DurationFlag) *DurationFlag {
|
||||||
return &DurationFlag{ DurationFlag: fl, set: nil }
|
return &DurationFlag{DurationFlag: fl, set: nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply saves the flagSet for later usage calls, then calls
|
// Apply saves the flagSet for later usage calls, then calls
|
||||||
// the wrapped DurationFlag.Apply
|
// the wrapped DurationFlag.Apply
|
||||||
func (f *DurationFlag) Apply(set *flag.FlagSet) {
|
func (f *DurationFlag) Apply(set *flag.FlagSet) error {
|
||||||
f.set = set
|
f.set = set
|
||||||
f.DurationFlag.Apply(set)
|
return f.DurationFlag.Apply(set)
|
||||||
}
|
|
||||||
|
|
||||||
// ApplyWithError saves the flagSet for later usage calls, then calls
|
|
||||||
// the wrapped DurationFlag.ApplyWithError
|
|
||||||
func (f *DurationFlag) ApplyWithError(set *flag.FlagSet) error {
|
|
||||||
f.set = set
|
|
||||||
return f.DurationFlag.ApplyWithError(set)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Float64Flag is the flag type that wraps cli.Float64Flag to allow
|
// Float64Flag is the flag type that wraps cli.Float64Flag to allow
|
||||||
// for other values to be specified
|
// for other values to be specified
|
||||||
type Float64Flag struct {
|
type Float64Flag struct {
|
||||||
cli.Float64Flag
|
*cli.Float64Flag
|
||||||
set *flag.FlagSet
|
set *flag.FlagSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFloat64Flag creates a new Float64Flag
|
// NewFloat64Flag creates a new Float64Flag
|
||||||
func NewFloat64Flag(fl cli.Float64Flag) *Float64Flag {
|
func NewFloat64Flag(fl *cli.Float64Flag) *Float64Flag {
|
||||||
return &Float64Flag{ Float64Flag: fl, set: nil }
|
return &Float64Flag{Float64Flag: fl, set: nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply saves the flagSet for later usage calls, then calls
|
// Apply saves the flagSet for later usage calls, then calls
|
||||||
// the wrapped Float64Flag.Apply
|
// the wrapped Float64Flag.Apply
|
||||||
func (f *Float64Flag) Apply(set *flag.FlagSet) {
|
func (f *Float64Flag) Apply(set *flag.FlagSet) error {
|
||||||
f.set = set
|
f.set = set
|
||||||
f.Float64Flag.Apply(set)
|
return f.Float64Flag.Apply(set)
|
||||||
}
|
|
||||||
|
|
||||||
// ApplyWithError saves the flagSet for later usage calls, then calls
|
|
||||||
// the wrapped Float64Flag.ApplyWithError
|
|
||||||
func (f *Float64Flag) ApplyWithError(set *flag.FlagSet) error {
|
|
||||||
f.set = set
|
|
||||||
return f.Float64Flag.ApplyWithError(set)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GenericFlag is the flag type that wraps cli.GenericFlag to allow
|
// GenericFlag is the flag type that wraps cli.GenericFlag to allow
|
||||||
// for other values to be specified
|
// for other values to be specified
|
||||||
type GenericFlag struct {
|
type GenericFlag struct {
|
||||||
cli.GenericFlag
|
*cli.GenericFlag
|
||||||
set *flag.FlagSet
|
set *flag.FlagSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewGenericFlag creates a new GenericFlag
|
// NewGenericFlag creates a new GenericFlag
|
||||||
func NewGenericFlag(fl cli.GenericFlag) *GenericFlag {
|
func NewGenericFlag(fl *cli.GenericFlag) *GenericFlag {
|
||||||
return &GenericFlag{ GenericFlag: fl, set: nil }
|
return &GenericFlag{GenericFlag: fl, set: nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply saves the flagSet for later usage calls, then calls
|
// Apply saves the flagSet for later usage calls, then calls
|
||||||
// the wrapped GenericFlag.Apply
|
// the wrapped GenericFlag.Apply
|
||||||
func (f *GenericFlag) Apply(set *flag.FlagSet) {
|
func (f *GenericFlag) Apply(set *flag.FlagSet) error {
|
||||||
f.set = set
|
f.set = set
|
||||||
f.GenericFlag.Apply(set)
|
return f.GenericFlag.Apply(set)
|
||||||
}
|
|
||||||
|
|
||||||
// ApplyWithError saves the flagSet for later usage calls, then calls
|
|
||||||
// the wrapped GenericFlag.ApplyWithError
|
|
||||||
func (f *GenericFlag) ApplyWithError(set *flag.FlagSet) error {
|
|
||||||
f.set = set
|
|
||||||
return f.GenericFlag.ApplyWithError(set)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Int64Flag is the flag type that wraps cli.Int64Flag to allow
|
// Int64Flag is the flag type that wraps cli.Int64Flag to allow
|
||||||
// for other values to be specified
|
// for other values to be specified
|
||||||
type Int64Flag struct {
|
type Int64Flag struct {
|
||||||
cli.Int64Flag
|
*cli.Int64Flag
|
||||||
set *flag.FlagSet
|
set *flag.FlagSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewInt64Flag creates a new Int64Flag
|
// NewInt64Flag creates a new Int64Flag
|
||||||
func NewInt64Flag(fl cli.Int64Flag) *Int64Flag {
|
func NewInt64Flag(fl *cli.Int64Flag) *Int64Flag {
|
||||||
return &Int64Flag{ Int64Flag: fl, set: nil }
|
return &Int64Flag{Int64Flag: fl, set: nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply saves the flagSet for later usage calls, then calls
|
// Apply saves the flagSet for later usage calls, then calls
|
||||||
// the wrapped Int64Flag.Apply
|
// the wrapped Int64Flag.Apply
|
||||||
func (f *Int64Flag) Apply(set *flag.FlagSet) {
|
func (f *Int64Flag) Apply(set *flag.FlagSet) error {
|
||||||
f.set = set
|
f.set = set
|
||||||
f.Int64Flag.Apply(set)
|
return f.Int64Flag.Apply(set)
|
||||||
}
|
|
||||||
|
|
||||||
// ApplyWithError saves the flagSet for later usage calls, then calls
|
|
||||||
// the wrapped Int64Flag.ApplyWithError
|
|
||||||
func (f *Int64Flag) ApplyWithError(set *flag.FlagSet) error {
|
|
||||||
f.set = set
|
|
||||||
return f.Int64Flag.ApplyWithError(set)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// IntFlag is the flag type that wraps cli.IntFlag to allow
|
// IntFlag is the flag type that wraps cli.IntFlag to allow
|
||||||
// for other values to be specified
|
// for other values to be specified
|
||||||
type IntFlag struct {
|
type IntFlag struct {
|
||||||
cli.IntFlag
|
*cli.IntFlag
|
||||||
set *flag.FlagSet
|
set *flag.FlagSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewIntFlag creates a new IntFlag
|
// NewIntFlag creates a new IntFlag
|
||||||
func NewIntFlag(fl cli.IntFlag) *IntFlag {
|
func NewIntFlag(fl *cli.IntFlag) *IntFlag {
|
||||||
return &IntFlag{ IntFlag: fl, set: nil }
|
return &IntFlag{IntFlag: fl, set: nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply saves the flagSet for later usage calls, then calls
|
// Apply saves the flagSet for later usage calls, then calls
|
||||||
// the wrapped IntFlag.Apply
|
// the wrapped IntFlag.Apply
|
||||||
func (f *IntFlag) Apply(set *flag.FlagSet) {
|
func (f *IntFlag) Apply(set *flag.FlagSet) error {
|
||||||
f.set = set
|
f.set = set
|
||||||
f.IntFlag.Apply(set)
|
return f.IntFlag.Apply(set)
|
||||||
}
|
|
||||||
|
|
||||||
// ApplyWithError saves the flagSet for later usage calls, then calls
|
|
||||||
// the wrapped IntFlag.ApplyWithError
|
|
||||||
func (f *IntFlag) ApplyWithError(set *flag.FlagSet) error {
|
|
||||||
f.set = set
|
|
||||||
return f.IntFlag.ApplyWithError(set)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// IntSliceFlag is the flag type that wraps cli.IntSliceFlag to allow
|
// IntSliceFlag is the flag type that wraps cli.IntSliceFlag to allow
|
||||||
// for other values to be specified
|
// for other values to be specified
|
||||||
type IntSliceFlag struct {
|
type IntSliceFlag struct {
|
||||||
cli.IntSliceFlag
|
*cli.IntSliceFlag
|
||||||
set *flag.FlagSet
|
set *flag.FlagSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewIntSliceFlag creates a new IntSliceFlag
|
// NewIntSliceFlag creates a new IntSliceFlag
|
||||||
func NewIntSliceFlag(fl cli.IntSliceFlag) *IntSliceFlag {
|
func NewIntSliceFlag(fl *cli.IntSliceFlag) *IntSliceFlag {
|
||||||
return &IntSliceFlag{ IntSliceFlag: fl, set: nil }
|
return &IntSliceFlag{IntSliceFlag: fl, set: nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply saves the flagSet for later usage calls, then calls
|
// Apply saves the flagSet for later usage calls, then calls
|
||||||
// the wrapped IntSliceFlag.Apply
|
// the wrapped IntSliceFlag.Apply
|
||||||
func (f *IntSliceFlag) Apply(set *flag.FlagSet) {
|
func (f *IntSliceFlag) Apply(set *flag.FlagSet) error {
|
||||||
f.set = set
|
f.set = set
|
||||||
f.IntSliceFlag.Apply(set)
|
return f.IntSliceFlag.Apply(set)
|
||||||
}
|
|
||||||
|
|
||||||
// ApplyWithError saves the flagSet for later usage calls, then calls
|
|
||||||
// the wrapped IntSliceFlag.ApplyWithError
|
|
||||||
func (f *IntSliceFlag) ApplyWithError(set *flag.FlagSet) error {
|
|
||||||
f.set = set
|
|
||||||
return f.IntSliceFlag.ApplyWithError(set)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Int64SliceFlag is the flag type that wraps cli.Int64SliceFlag to allow
|
// Int64SliceFlag is the flag type that wraps cli.Int64SliceFlag to allow
|
||||||
// for other values to be specified
|
// for other values to be specified
|
||||||
type Int64SliceFlag struct {
|
type Int64SliceFlag struct {
|
||||||
cli.Int64SliceFlag
|
*cli.Int64SliceFlag
|
||||||
set *flag.FlagSet
|
set *flag.FlagSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewInt64SliceFlag creates a new Int64SliceFlag
|
// NewInt64SliceFlag creates a new Int64SliceFlag
|
||||||
func NewInt64SliceFlag(fl cli.Int64SliceFlag) *Int64SliceFlag {
|
func NewInt64SliceFlag(fl *cli.Int64SliceFlag) *Int64SliceFlag {
|
||||||
return &Int64SliceFlag{ Int64SliceFlag: fl, set: nil }
|
return &Int64SliceFlag{Int64SliceFlag: fl, set: nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply saves the flagSet for later usage calls, then calls
|
// Apply saves the flagSet for later usage calls, then calls
|
||||||
// the wrapped Int64SliceFlag.Apply
|
// the wrapped Int64SliceFlag.Apply
|
||||||
func (f *Int64SliceFlag) Apply(set *flag.FlagSet) {
|
func (f *Int64SliceFlag) Apply(set *flag.FlagSet) error {
|
||||||
f.set = set
|
f.set = set
|
||||||
f.Int64SliceFlag.Apply(set)
|
return f.Int64SliceFlag.Apply(set)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApplyWithError saves the flagSet for later usage calls, then calls
|
// Float64SliceFlag is the flag type that wraps cli.Float64SliceFlag to allow
|
||||||
// the wrapped Int64SliceFlag.ApplyWithError
|
// for other values to be specified
|
||||||
func (f *Int64SliceFlag) ApplyWithError(set *flag.FlagSet) error {
|
type Float64SliceFlag struct {
|
||||||
|
*cli.Float64SliceFlag
|
||||||
|
set *flag.FlagSet
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewFloat64SliceFlag creates a new Float64SliceFlag
|
||||||
|
func NewFloat64SliceFlag(fl *cli.Float64SliceFlag) *Float64SliceFlag {
|
||||||
|
return &Float64SliceFlag{Float64SliceFlag: fl, set: nil}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply saves the flagSet for later usage calls, then calls the
|
||||||
|
// wrapped Float64SliceFlag.Apply
|
||||||
|
func (f *Float64SliceFlag) Apply(set *flag.FlagSet) error {
|
||||||
f.set = set
|
f.set = set
|
||||||
return f.Int64SliceFlag.ApplyWithError(set)
|
return f.Float64SliceFlag.Apply(set)
|
||||||
}
|
}
|
||||||
|
|
||||||
// StringFlag is the flag type that wraps cli.StringFlag to allow
|
// StringFlag is the flag type that wraps cli.StringFlag to allow
|
||||||
// for other values to be specified
|
// for other values to be specified
|
||||||
type StringFlag struct {
|
type StringFlag struct {
|
||||||
cli.StringFlag
|
*cli.StringFlag
|
||||||
set *flag.FlagSet
|
set *flag.FlagSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewStringFlag creates a new StringFlag
|
// NewStringFlag creates a new StringFlag
|
||||||
func NewStringFlag(fl cli.StringFlag) *StringFlag {
|
func NewStringFlag(fl *cli.StringFlag) *StringFlag {
|
||||||
return &StringFlag{ StringFlag: fl, set: nil }
|
return &StringFlag{StringFlag: fl, set: nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply saves the flagSet for later usage calls, then calls
|
// Apply saves the flagSet for later usage calls, then calls
|
||||||
// the wrapped StringFlag.Apply
|
// the wrapped StringFlag.Apply
|
||||||
func (f *StringFlag) Apply(set *flag.FlagSet) {
|
func (f *StringFlag) Apply(set *flag.FlagSet) error {
|
||||||
f.set = set
|
f.set = set
|
||||||
f.StringFlag.Apply(set)
|
return f.StringFlag.Apply(set)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApplyWithError saves the flagSet for later usage calls, then calls
|
// PathFlag is the flag type that wraps cli.PathFlag to allow
|
||||||
// the wrapped StringFlag.ApplyWithError
|
// for other values to be specified
|
||||||
func (f *StringFlag) ApplyWithError(set *flag.FlagSet) error {
|
type PathFlag struct {
|
||||||
|
*cli.PathFlag
|
||||||
|
set *flag.FlagSet
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewPathFlag creates a new PathFlag
|
||||||
|
func NewPathFlag(fl *cli.PathFlag) *PathFlag {
|
||||||
|
return &PathFlag{PathFlag: fl, set: nil}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply saves the flagSet for later usage calls, then calls the
|
||||||
|
// wrapped PathFlag.Apply
|
||||||
|
func (f *PathFlag) Apply(set *flag.FlagSet) error {
|
||||||
f.set = set
|
f.set = set
|
||||||
return f.StringFlag.ApplyWithError(set)
|
return f.PathFlag.Apply(set)
|
||||||
}
|
}
|
||||||
|
|
||||||
// StringSliceFlag is the flag type that wraps cli.StringSliceFlag to allow
|
// StringSliceFlag is the flag type that wraps cli.StringSliceFlag to allow
|
||||||
// for other values to be specified
|
// for other values to be specified
|
||||||
type StringSliceFlag struct {
|
type StringSliceFlag struct {
|
||||||
cli.StringSliceFlag
|
*cli.StringSliceFlag
|
||||||
set *flag.FlagSet
|
set *flag.FlagSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewStringSliceFlag creates a new StringSliceFlag
|
// NewStringSliceFlag creates a new StringSliceFlag
|
||||||
func NewStringSliceFlag(fl cli.StringSliceFlag) *StringSliceFlag {
|
func NewStringSliceFlag(fl *cli.StringSliceFlag) *StringSliceFlag {
|
||||||
return &StringSliceFlag{ StringSliceFlag: fl, set: nil }
|
return &StringSliceFlag{StringSliceFlag: fl, set: nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply saves the flagSet for later usage calls, then calls
|
// Apply saves the flagSet for later usage calls, then calls
|
||||||
// the wrapped StringSliceFlag.Apply
|
// the wrapped StringSliceFlag.Apply
|
||||||
func (f *StringSliceFlag) Apply(set *flag.FlagSet) {
|
func (f *StringSliceFlag) Apply(set *flag.FlagSet) error {
|
||||||
f.set = set
|
f.set = set
|
||||||
f.StringSliceFlag.Apply(set)
|
return f.StringSliceFlag.Apply(set)
|
||||||
}
|
|
||||||
|
|
||||||
// ApplyWithError saves the flagSet for later usage calls, then calls
|
|
||||||
// the wrapped StringSliceFlag.ApplyWithError
|
|
||||||
func (f *StringSliceFlag) ApplyWithError(set *flag.FlagSet) error {
|
|
||||||
f.set = set
|
|
||||||
return f.StringSliceFlag.ApplyWithError(set)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Uint64Flag is the flag type that wraps cli.Uint64Flag to allow
|
// Uint64Flag is the flag type that wraps cli.Uint64Flag to allow
|
||||||
// for other values to be specified
|
// for other values to be specified
|
||||||
type Uint64Flag struct {
|
type Uint64Flag struct {
|
||||||
cli.Uint64Flag
|
*cli.Uint64Flag
|
||||||
set *flag.FlagSet
|
set *flag.FlagSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewUint64Flag creates a new Uint64Flag
|
// NewUint64Flag creates a new Uint64Flag
|
||||||
func NewUint64Flag(fl cli.Uint64Flag) *Uint64Flag {
|
func NewUint64Flag(fl *cli.Uint64Flag) *Uint64Flag {
|
||||||
return &Uint64Flag{ Uint64Flag: fl, set: nil }
|
return &Uint64Flag{Uint64Flag: fl, set: nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply saves the flagSet for later usage calls, then calls
|
// Apply saves the flagSet for later usage calls, then calls
|
||||||
// the wrapped Uint64Flag.Apply
|
// the wrapped Uint64Flag.Apply
|
||||||
func (f *Uint64Flag) Apply(set *flag.FlagSet) {
|
func (f *Uint64Flag) Apply(set *flag.FlagSet) error {
|
||||||
f.set = set
|
f.set = set
|
||||||
f.Uint64Flag.Apply(set)
|
return f.Uint64Flag.Apply(set)
|
||||||
}
|
|
||||||
|
|
||||||
// ApplyWithError saves the flagSet for later usage calls, then calls
|
|
||||||
// the wrapped Uint64Flag.ApplyWithError
|
|
||||||
func (f *Uint64Flag) ApplyWithError(set *flag.FlagSet) error {
|
|
||||||
f.set = set
|
|
||||||
return f.Uint64Flag.ApplyWithError(set)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// UintFlag is the flag type that wraps cli.UintFlag to allow
|
// UintFlag is the flag type that wraps cli.UintFlag to allow
|
||||||
// for other values to be specified
|
// for other values to be specified
|
||||||
type UintFlag struct {
|
type UintFlag struct {
|
||||||
cli.UintFlag
|
*cli.UintFlag
|
||||||
set *flag.FlagSet
|
set *flag.FlagSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewUintFlag creates a new UintFlag
|
// NewUintFlag creates a new UintFlag
|
||||||
func NewUintFlag(fl cli.UintFlag) *UintFlag {
|
func NewUintFlag(fl *cli.UintFlag) *UintFlag {
|
||||||
return &UintFlag{ UintFlag: fl, set: nil }
|
return &UintFlag{UintFlag: fl, set: nil}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply saves the flagSet for later usage calls, then calls
|
// Apply saves the flagSet for later usage calls, then calls
|
||||||
// the wrapped UintFlag.Apply
|
// the wrapped UintFlag.Apply
|
||||||
func (f *UintFlag) Apply(set *flag.FlagSet) {
|
func (f *UintFlag) Apply(set *flag.FlagSet) error {
|
||||||
f.set = set
|
f.set = set
|
||||||
f.UintFlag.Apply(set)
|
return f.UintFlag.Apply(set)
|
||||||
}
|
|
||||||
|
|
||||||
// ApplyWithError saves the flagSet for later usage calls, then calls
|
|
||||||
// the wrapped UintFlag.ApplyWithError
|
|
||||||
func (f *UintFlag) ApplyWithError(set *flag.FlagSet) error {
|
|
||||||
f.set = set
|
|
||||||
return f.UintFlag.ApplyWithError(set)
|
|
||||||
}
|
}
|
||||||
|
@ -3,12 +3,12 @@ package altsrc
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/urfave/cli"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type testApplyInputSource struct {
|
type testApplyInputSource struct {
|
||||||
@ -20,13 +20,14 @@ type testApplyInputSource struct {
|
|||||||
ContextValue flag.Value
|
ContextValue flag.Value
|
||||||
EnvVarValue string
|
EnvVarValue string
|
||||||
EnvVarName string
|
EnvVarName string
|
||||||
|
SourcePath string
|
||||||
MapValue interface{}
|
MapValue interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGenericApplyInputSourceValue(t *testing.T) {
|
func TestGenericApplyInputSourceValue(t *testing.T) {
|
||||||
v := &Parser{"abc", "def"}
|
v := &Parser{"abc", "def"}
|
||||||
c := runTest(t, testApplyInputSource{
|
c := runTest(t, testApplyInputSource{
|
||||||
Flag: NewGenericFlag(cli.GenericFlag{Name: "test", Value: &Parser{}}),
|
Flag: NewGenericFlag(&cli.GenericFlag{Name: "test", Value: &Parser{}}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: v,
|
MapValue: v,
|
||||||
})
|
})
|
||||||
@ -36,7 +37,7 @@ func TestGenericApplyInputSourceValue(t *testing.T) {
|
|||||||
func TestGenericApplyInputSourceMethodContextSet(t *testing.T) {
|
func TestGenericApplyInputSourceMethodContextSet(t *testing.T) {
|
||||||
p := &Parser{"abc", "def"}
|
p := &Parser{"abc", "def"}
|
||||||
c := runTest(t, testApplyInputSource{
|
c := runTest(t, testApplyInputSource{
|
||||||
Flag: NewGenericFlag(cli.GenericFlag{Name: "test", Value: &Parser{}}),
|
Flag: NewGenericFlag(&cli.GenericFlag{Name: "test", Value: &Parser{}}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: &Parser{"efg", "hig"},
|
MapValue: &Parser{"efg", "hig"},
|
||||||
ContextValueString: p.String(),
|
ContextValueString: p.String(),
|
||||||
@ -46,7 +47,11 @@ func TestGenericApplyInputSourceMethodContextSet(t *testing.T) {
|
|||||||
|
|
||||||
func TestGenericApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
func TestGenericApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
c := runTest(t, testApplyInputSource{
|
||||||
Flag: NewGenericFlag(cli.GenericFlag{Name: "test", Value: &Parser{}, EnvVar: "TEST"}),
|
Flag: NewGenericFlag(&cli.GenericFlag{
|
||||||
|
Name: "test",
|
||||||
|
Value: &Parser{},
|
||||||
|
EnvVars: []string{"TEST"},
|
||||||
|
}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: &Parser{"efg", "hij"},
|
MapValue: &Parser{"efg", "hij"},
|
||||||
EnvVarName: "TEST",
|
EnvVarName: "TEST",
|
||||||
@ -57,7 +62,7 @@ func TestGenericApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
|||||||
|
|
||||||
func TestStringSliceApplyInputSourceValue(t *testing.T) {
|
func TestStringSliceApplyInputSourceValue(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
c := runTest(t, testApplyInputSource{
|
||||||
Flag: NewStringSliceFlag(cli.StringSliceFlag{Name: "test"}),
|
Flag: NewStringSliceFlag(&cli.StringSliceFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: []interface{}{"hello", "world"},
|
MapValue: []interface{}{"hello", "world"},
|
||||||
})
|
})
|
||||||
@ -66,7 +71,7 @@ func TestStringSliceApplyInputSourceValue(t *testing.T) {
|
|||||||
|
|
||||||
func TestStringSliceApplyInputSourceMethodContextSet(t *testing.T) {
|
func TestStringSliceApplyInputSourceMethodContextSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
c := runTest(t, testApplyInputSource{
|
||||||
Flag: NewStringSliceFlag(cli.StringSliceFlag{Name: "test"}),
|
Flag: NewStringSliceFlag(&cli.StringSliceFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: []interface{}{"hello", "world"},
|
MapValue: []interface{}{"hello", "world"},
|
||||||
ContextValueString: "ohno",
|
ContextValueString: "ohno",
|
||||||
@ -76,7 +81,7 @@ func TestStringSliceApplyInputSourceMethodContextSet(t *testing.T) {
|
|||||||
|
|
||||||
func TestStringSliceApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
func TestStringSliceApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
c := runTest(t, testApplyInputSource{
|
||||||
Flag: NewStringSliceFlag(cli.StringSliceFlag{Name: "test", EnvVar: "TEST"}),
|
Flag: NewStringSliceFlag(&cli.StringSliceFlag{Name: "test", EnvVars: []string{"TEST"}}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: []interface{}{"hello", "world"},
|
MapValue: []interface{}{"hello", "world"},
|
||||||
EnvVarName: "TEST",
|
EnvVarName: "TEST",
|
||||||
@ -87,7 +92,7 @@ func TestStringSliceApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
|||||||
|
|
||||||
func TestIntSliceApplyInputSourceValue(t *testing.T) {
|
func TestIntSliceApplyInputSourceValue(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
c := runTest(t, testApplyInputSource{
|
||||||
Flag: NewIntSliceFlag(cli.IntSliceFlag{Name: "test"}),
|
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: []interface{}{1, 2},
|
MapValue: []interface{}{1, 2},
|
||||||
})
|
})
|
||||||
@ -96,7 +101,7 @@ func TestIntSliceApplyInputSourceValue(t *testing.T) {
|
|||||||
|
|
||||||
func TestIntSliceApplyInputSourceMethodContextSet(t *testing.T) {
|
func TestIntSliceApplyInputSourceMethodContextSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
c := runTest(t, testApplyInputSource{
|
||||||
Flag: NewIntSliceFlag(cli.IntSliceFlag{Name: "test"}),
|
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: []interface{}{1, 2},
|
MapValue: []interface{}{1, 2},
|
||||||
ContextValueString: "3",
|
ContextValueString: "3",
|
||||||
@ -106,7 +111,7 @@ func TestIntSliceApplyInputSourceMethodContextSet(t *testing.T) {
|
|||||||
|
|
||||||
func TestIntSliceApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
func TestIntSliceApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
c := runTest(t, testApplyInputSource{
|
||||||
Flag: NewIntSliceFlag(cli.IntSliceFlag{Name: "test", EnvVar: "TEST"}),
|
Flag: NewIntSliceFlag(&cli.IntSliceFlag{Name: "test", EnvVars: []string{"TEST"}}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: []interface{}{1, 2},
|
MapValue: []interface{}{1, 2},
|
||||||
EnvVarName: "TEST",
|
EnvVarName: "TEST",
|
||||||
@ -117,7 +122,7 @@ func TestIntSliceApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
|||||||
|
|
||||||
func TestBoolApplyInputSourceMethodSet(t *testing.T) {
|
func TestBoolApplyInputSourceMethodSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
c := runTest(t, testApplyInputSource{
|
||||||
Flag: NewBoolFlag(cli.BoolFlag{Name: "test"}),
|
Flag: NewBoolFlag(&cli.BoolFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: true,
|
MapValue: true,
|
||||||
})
|
})
|
||||||
@ -126,7 +131,7 @@ func TestBoolApplyInputSourceMethodSet(t *testing.T) {
|
|||||||
|
|
||||||
func TestBoolApplyInputSourceMethodContextSet(t *testing.T) {
|
func TestBoolApplyInputSourceMethodContextSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
c := runTest(t, testApplyInputSource{
|
||||||
Flag: NewBoolFlag(cli.BoolFlag{Name: "test"}),
|
Flag: NewBoolFlag(&cli.BoolFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: false,
|
MapValue: false,
|
||||||
ContextValueString: "true",
|
ContextValueString: "true",
|
||||||
@ -136,7 +141,7 @@ func TestBoolApplyInputSourceMethodContextSet(t *testing.T) {
|
|||||||
|
|
||||||
func TestBoolApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
func TestBoolApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
c := runTest(t, testApplyInputSource{
|
||||||
Flag: NewBoolFlag(cli.BoolFlag{Name: "test", EnvVar: "TEST"}),
|
Flag: NewBoolFlag(&cli.BoolFlag{Name: "test", EnvVars: []string{"TEST"}}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: false,
|
MapValue: false,
|
||||||
EnvVarName: "TEST",
|
EnvVarName: "TEST",
|
||||||
@ -145,39 +150,9 @@ func TestBoolApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
|||||||
expect(t, true, c.Bool("test"))
|
expect(t, true, c.Bool("test"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBoolTApplyInputSourceMethodSet(t *testing.T) {
|
|
||||||
c := runTest(t, testApplyInputSource{
|
|
||||||
Flag: NewBoolTFlag(cli.BoolTFlag{Name: "test"}),
|
|
||||||
FlagName: "test",
|
|
||||||
MapValue: false,
|
|
||||||
})
|
|
||||||
expect(t, false, c.BoolT("test"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBoolTApplyInputSourceMethodContextSet(t *testing.T) {
|
|
||||||
c := runTest(t, testApplyInputSource{
|
|
||||||
Flag: NewBoolTFlag(cli.BoolTFlag{Name: "test"}),
|
|
||||||
FlagName: "test",
|
|
||||||
MapValue: true,
|
|
||||||
ContextValueString: "false",
|
|
||||||
})
|
|
||||||
expect(t, false, c.BoolT("test"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestBoolTApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
|
||||||
c := runTest(t, testApplyInputSource{
|
|
||||||
Flag: NewBoolTFlag(cli.BoolTFlag{Name: "test", EnvVar: "TEST"}),
|
|
||||||
FlagName: "test",
|
|
||||||
MapValue: true,
|
|
||||||
EnvVarName: "TEST",
|
|
||||||
EnvVarValue: "false",
|
|
||||||
})
|
|
||||||
expect(t, false, c.BoolT("test"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestStringApplyInputSourceMethodSet(t *testing.T) {
|
func TestStringApplyInputSourceMethodSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
c := runTest(t, testApplyInputSource{
|
||||||
Flag: NewStringFlag(cli.StringFlag{Name: "test"}),
|
Flag: NewStringFlag(&cli.StringFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: "hello",
|
MapValue: "hello",
|
||||||
})
|
})
|
||||||
@ -186,7 +161,7 @@ func TestStringApplyInputSourceMethodSet(t *testing.T) {
|
|||||||
|
|
||||||
func TestStringApplyInputSourceMethodContextSet(t *testing.T) {
|
func TestStringApplyInputSourceMethodContextSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
c := runTest(t, testApplyInputSource{
|
||||||
Flag: NewStringFlag(cli.StringFlag{Name: "test"}),
|
Flag: NewStringFlag(&cli.StringFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: "hello",
|
MapValue: "hello",
|
||||||
ContextValueString: "goodbye",
|
ContextValueString: "goodbye",
|
||||||
@ -196,7 +171,7 @@ func TestStringApplyInputSourceMethodContextSet(t *testing.T) {
|
|||||||
|
|
||||||
func TestStringApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
func TestStringApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
c := runTest(t, testApplyInputSource{
|
||||||
Flag: NewStringFlag(cli.StringFlag{Name: "test", EnvVar: "TEST"}),
|
Flag: NewStringFlag(&cli.StringFlag{Name: "test", EnvVars: []string{"TEST"}}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: "hello",
|
MapValue: "hello",
|
||||||
EnvVarName: "TEST",
|
EnvVarName: "TEST",
|
||||||
@ -204,10 +179,47 @@ func TestStringApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
|||||||
})
|
})
|
||||||
expect(t, "goodbye", c.String("test"))
|
expect(t, "goodbye", c.String("test"))
|
||||||
}
|
}
|
||||||
|
func TestPathApplyInputSourceMethodSet(t *testing.T) {
|
||||||
|
c := runTest(t, testApplyInputSource{
|
||||||
|
Flag: NewPathFlag(&cli.PathFlag{Name: "test"}),
|
||||||
|
FlagName: "test",
|
||||||
|
MapValue: "hello",
|
||||||
|
SourcePath: "/path/to/source/file",
|
||||||
|
})
|
||||||
|
|
||||||
|
expected := "/path/to/source/hello"
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
expected = `C:\path\to\source\hello`
|
||||||
|
}
|
||||||
|
expect(t, expected, c.String("test"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPathApplyInputSourceMethodContextSet(t *testing.T) {
|
||||||
|
c := runTest(t, testApplyInputSource{
|
||||||
|
Flag: NewPathFlag(&cli.PathFlag{Name: "test"}),
|
||||||
|
FlagName: "test",
|
||||||
|
MapValue: "hello",
|
||||||
|
ContextValueString: "goodbye",
|
||||||
|
SourcePath: "/path/to/source/file",
|
||||||
|
})
|
||||||
|
expect(t, "goodbye", c.String("test"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPathApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
||||||
|
c := runTest(t, testApplyInputSource{
|
||||||
|
Flag: NewPathFlag(&cli.PathFlag{Name: "test", EnvVars: []string{"TEST"}}),
|
||||||
|
FlagName: "test",
|
||||||
|
MapValue: "hello",
|
||||||
|
EnvVarName: "TEST",
|
||||||
|
EnvVarValue: "goodbye",
|
||||||
|
SourcePath: "/path/to/source/file",
|
||||||
|
})
|
||||||
|
expect(t, "goodbye", c.String("test"))
|
||||||
|
}
|
||||||
|
|
||||||
func TestIntApplyInputSourceMethodSet(t *testing.T) {
|
func TestIntApplyInputSourceMethodSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
c := runTest(t, testApplyInputSource{
|
||||||
Flag: NewIntFlag(cli.IntFlag{Name: "test"}),
|
Flag: NewIntFlag(&cli.IntFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: 15,
|
MapValue: 15,
|
||||||
})
|
})
|
||||||
@ -216,7 +228,7 @@ func TestIntApplyInputSourceMethodSet(t *testing.T) {
|
|||||||
|
|
||||||
func TestIntApplyInputSourceMethodContextSet(t *testing.T) {
|
func TestIntApplyInputSourceMethodContextSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
c := runTest(t, testApplyInputSource{
|
||||||
Flag: NewIntFlag(cli.IntFlag{Name: "test"}),
|
Flag: NewIntFlag(&cli.IntFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: 15,
|
MapValue: 15,
|
||||||
ContextValueString: "7",
|
ContextValueString: "7",
|
||||||
@ -226,7 +238,7 @@ func TestIntApplyInputSourceMethodContextSet(t *testing.T) {
|
|||||||
|
|
||||||
func TestIntApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
func TestIntApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
c := runTest(t, testApplyInputSource{
|
||||||
Flag: NewIntFlag(cli.IntFlag{Name: "test", EnvVar: "TEST"}),
|
Flag: NewIntFlag(&cli.IntFlag{Name: "test", EnvVars: []string{"TEST"}}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: 15,
|
MapValue: 15,
|
||||||
EnvVarName: "TEST",
|
EnvVarName: "TEST",
|
||||||
@ -237,7 +249,7 @@ func TestIntApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
|||||||
|
|
||||||
func TestDurationApplyInputSourceMethodSet(t *testing.T) {
|
func TestDurationApplyInputSourceMethodSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
c := runTest(t, testApplyInputSource{
|
||||||
Flag: NewDurationFlag(cli.DurationFlag{Name: "test"}),
|
Flag: NewDurationFlag(&cli.DurationFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: 30 * time.Second,
|
MapValue: 30 * time.Second,
|
||||||
})
|
})
|
||||||
@ -246,7 +258,7 @@ func TestDurationApplyInputSourceMethodSet(t *testing.T) {
|
|||||||
|
|
||||||
func TestDurationApplyInputSourceMethodContextSet(t *testing.T) {
|
func TestDurationApplyInputSourceMethodContextSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
c := runTest(t, testApplyInputSource{
|
||||||
Flag: NewDurationFlag(cli.DurationFlag{Name: "test"}),
|
Flag: NewDurationFlag(&cli.DurationFlag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: 30 * time.Second,
|
MapValue: 30 * time.Second,
|
||||||
ContextValueString: (15 * time.Second).String(),
|
ContextValueString: (15 * time.Second).String(),
|
||||||
@ -256,7 +268,7 @@ func TestDurationApplyInputSourceMethodContextSet(t *testing.T) {
|
|||||||
|
|
||||||
func TestDurationApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
func TestDurationApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
c := runTest(t, testApplyInputSource{
|
||||||
Flag: NewDurationFlag(cli.DurationFlag{Name: "test", EnvVar: "TEST"}),
|
Flag: NewDurationFlag(&cli.DurationFlag{Name: "test", EnvVars: []string{"TEST"}}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: 30 * time.Second,
|
MapValue: 30 * time.Second,
|
||||||
EnvVarName: "TEST",
|
EnvVarName: "TEST",
|
||||||
@ -267,7 +279,7 @@ func TestDurationApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
|||||||
|
|
||||||
func TestFloat64ApplyInputSourceMethodSet(t *testing.T) {
|
func TestFloat64ApplyInputSourceMethodSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
c := runTest(t, testApplyInputSource{
|
||||||
Flag: NewFloat64Flag(cli.Float64Flag{Name: "test"}),
|
Flag: NewFloat64Flag(&cli.Float64Flag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: 1.3,
|
MapValue: 1.3,
|
||||||
})
|
})
|
||||||
@ -276,7 +288,7 @@ func TestFloat64ApplyInputSourceMethodSet(t *testing.T) {
|
|||||||
|
|
||||||
func TestFloat64ApplyInputSourceMethodContextSet(t *testing.T) {
|
func TestFloat64ApplyInputSourceMethodContextSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
c := runTest(t, testApplyInputSource{
|
||||||
Flag: NewFloat64Flag(cli.Float64Flag{Name: "test"}),
|
Flag: NewFloat64Flag(&cli.Float64Flag{Name: "test"}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: 1.3,
|
MapValue: 1.3,
|
||||||
ContextValueString: fmt.Sprintf("%v", 1.4),
|
ContextValueString: fmt.Sprintf("%v", 1.4),
|
||||||
@ -286,7 +298,7 @@ func TestFloat64ApplyInputSourceMethodContextSet(t *testing.T) {
|
|||||||
|
|
||||||
func TestFloat64ApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
func TestFloat64ApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
||||||
c := runTest(t, testApplyInputSource{
|
c := runTest(t, testApplyInputSource{
|
||||||
Flag: NewFloat64Flag(cli.Float64Flag{Name: "test", EnvVar: "TEST"}),
|
Flag: NewFloat64Flag(&cli.Float64Flag{Name: "test", EnvVars: []string{"TEST"}}),
|
||||||
FlagName: "test",
|
FlagName: "test",
|
||||||
MapValue: 1.3,
|
MapValue: 1.3,
|
||||||
EnvVarName: "TEST",
|
EnvVarName: "TEST",
|
||||||
@ -296,7 +308,10 @@ func TestFloat64ApplyInputSourceMethodEnvVarSet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func runTest(t *testing.T, test testApplyInputSource) *cli.Context {
|
func runTest(t *testing.T, test testApplyInputSource) *cli.Context {
|
||||||
inputSource := &MapInputSource{valueMap: map[interface{}]interface{}{test.FlagName: test.MapValue}}
|
inputSource := &MapInputSource{
|
||||||
|
file: test.SourcePath,
|
||||||
|
valueMap: map[interface{}]interface{}{test.FlagName: test.MapValue},
|
||||||
|
}
|
||||||
set := flag.NewFlagSet(test.FlagSetName, flag.ContinueOnError)
|
set := flag.NewFlagSet(test.FlagSetName, flag.ContinueOnError)
|
||||||
c := cli.NewContext(nil, set, nil)
|
c := cli.NewContext(nil, set, nil)
|
||||||
if test.EnvVarName != "" && test.EnvVarValue != "" {
|
if test.EnvVarName != "" && test.EnvVarValue != "" {
|
||||||
@ -304,7 +319,7 @@ func runTest(t *testing.T, test testApplyInputSource) *cli.Context {
|
|||||||
defer os.Setenv(test.EnvVarName, "")
|
defer os.Setenv(test.EnvVarName, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
test.Flag.Apply(set)
|
_ = test.Flag.Apply(set)
|
||||||
if test.ContextValue != nil {
|
if test.ContextValue != nil {
|
||||||
f := set.Lookup(test.FlagName)
|
f := set.Lookup(test.FlagName)
|
||||||
f.Value = test.ContextValue
|
f.Value = test.ContextValue
|
||||||
|
@ -1,13 +1,23 @@
|
|||||||
package altsrc
|
package altsrc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
wd, _ = os.Getwd()
|
||||||
|
)
|
||||||
|
|
||||||
func expect(t *testing.T, a interface{}, b interface{}) {
|
func expect(t *testing.T, a interface{}, b interface{}) {
|
||||||
if !reflect.DeepEqual(b, a) {
|
_, fn, line, _ := runtime.Caller(1)
|
||||||
t.Errorf("Expected %#v (type %v) - Got %#v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
|
fn = strings.Replace(fn, wd+"/", "", -1)
|
||||||
|
|
||||||
|
if !reflect.DeepEqual(a, b) {
|
||||||
|
t.Errorf("(%s:%d) Expected %v (type %v) - Got %v (type %v)", fn, line, b, reflect.TypeOf(b), a, reflect.TypeOf(a))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,14 +1,18 @@
|
|||||||
package altsrc
|
package altsrc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/urfave/cli"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// InputSourceContext is an interface used to allow
|
// InputSourceContext is an interface used to allow
|
||||||
// other input sources to be implemented as needed.
|
// other input sources to be implemented as needed.
|
||||||
|
//
|
||||||
|
// Source returns an identifier for the input source. In case of file source
|
||||||
|
// it should return path to the file.
|
||||||
type InputSourceContext interface {
|
type InputSourceContext interface {
|
||||||
|
Source() string
|
||||||
|
|
||||||
Int(name string) (int, error)
|
Int(name string) (int, error)
|
||||||
Duration(name string) (time.Duration, error)
|
Duration(name string) (time.Duration, error)
|
||||||
Float64(name string) (float64, error)
|
Float64(name string) (float64, error)
|
||||||
@ -17,5 +21,4 @@ type InputSourceContext interface {
|
|||||||
IntSlice(name string) ([]int, error)
|
IntSlice(name string) ([]int, error)
|
||||||
Generic(name string) (cli.Generic, error)
|
Generic(name string) (cli.Generic, error)
|
||||||
Bool(name string) (bool, error)
|
Bool(name string) (bool, error)
|
||||||
BoolT(name string) (bool, error)
|
|
||||||
}
|
}
|
||||||
|
@ -2,11 +2,10 @@ package altsrc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/urfave/cli"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -19,7 +18,7 @@ func TestCommandJSONFileTest(t *testing.T) {
|
|||||||
cleanup := writeTempFile(t, fileName, simpleJSON)
|
cleanup := writeTempFile(t, fileName, simpleJSON)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
test := []string{"test-cmd", "--load", fileName}
|
test := []string{"test-cmd", "--load", fileName}
|
||||||
_ = set.Parse(test)
|
_ = set.Parse(test)
|
||||||
@ -37,7 +36,7 @@ func TestCommandJSONFileTest(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "test"}),
|
NewIntFlag(&cli.IntFlag{Name: "test"}),
|
||||||
&cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
|
||||||
@ -50,7 +49,7 @@ func TestCommandJSONFileTestGlobalEnvVarWins(t *testing.T) {
|
|||||||
cleanup := writeTempFile(t, fileName, simpleJSON)
|
cleanup := writeTempFile(t, fileName, simpleJSON)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
_ = os.Setenv("THE_TEST", "10")
|
_ = os.Setenv("THE_TEST", "10")
|
||||||
defer os.Setenv("THE_TEST", "")
|
defer os.Setenv("THE_TEST", "")
|
||||||
@ -71,7 +70,7 @@ func TestCommandJSONFileTestGlobalEnvVarWins(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "test", EnvVar: "THE_TEST"}),
|
NewIntFlag(&cli.IntFlag{Name: "test", EnvVars: []string{"THE_TEST"}}),
|
||||||
&cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
|
||||||
@ -85,7 +84,7 @@ func TestCommandJSONFileTestGlobalEnvVarWinsNested(t *testing.T) {
|
|||||||
cleanup := writeTempFile(t, fileName, nestedJSON)
|
cleanup := writeTempFile(t, fileName, nestedJSON)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
_ = os.Setenv("THE_TEST", "10")
|
_ = os.Setenv("THE_TEST", "10")
|
||||||
defer os.Setenv("THE_TEST", "")
|
defer os.Setenv("THE_TEST", "")
|
||||||
@ -106,7 +105,7 @@ func TestCommandJSONFileTestGlobalEnvVarWinsNested(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "top.test", EnvVar: "THE_TEST"}),
|
NewIntFlag(&cli.IntFlag{Name: "top.test", EnvVars: []string{"THE_TEST"}}),
|
||||||
&cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
|
||||||
@ -120,7 +119,7 @@ func TestCommandJSONFileTestSpecifiedFlagWins(t *testing.T) {
|
|||||||
cleanup := writeTempFile(t, fileName, simpleJSON)
|
cleanup := writeTempFile(t, fileName, simpleJSON)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
test := []string{"test-cmd", "--load", fileName, "--test", "7"}
|
test := []string{"test-cmd", "--load", fileName, "--test", "7"}
|
||||||
_ = set.Parse(test)
|
_ = set.Parse(test)
|
||||||
@ -138,7 +137,7 @@ func TestCommandJSONFileTestSpecifiedFlagWins(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "test"}),
|
NewIntFlag(&cli.IntFlag{Name: "test"}),
|
||||||
&cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
|
||||||
@ -152,7 +151,7 @@ func TestCommandJSONFileTestSpecifiedFlagWinsNested(t *testing.T) {
|
|||||||
cleanup := writeTempFile(t, fileName, nestedJSON)
|
cleanup := writeTempFile(t, fileName, nestedJSON)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
test := []string{"test-cmd", "--load", fileName, "--top.test", "7"}
|
test := []string{"test-cmd", "--load", fileName, "--top.test", "7"}
|
||||||
_ = set.Parse(test)
|
_ = set.Parse(test)
|
||||||
@ -170,7 +169,7 @@ func TestCommandJSONFileTestSpecifiedFlagWinsNested(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "top.test"}),
|
NewIntFlag(&cli.IntFlag{Name: "top.test"}),
|
||||||
&cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
|
||||||
@ -184,7 +183,7 @@ func TestCommandJSONFileTestDefaultValueFileWins(t *testing.T) {
|
|||||||
cleanup := writeTempFile(t, fileName, simpleJSON)
|
cleanup := writeTempFile(t, fileName, simpleJSON)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
test := []string{"test-cmd", "--load", fileName}
|
test := []string{"test-cmd", "--load", fileName}
|
||||||
_ = set.Parse(test)
|
_ = set.Parse(test)
|
||||||
@ -202,7 +201,7 @@ func TestCommandJSONFileTestDefaultValueFileWins(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "test", Value: 7}),
|
NewIntFlag(&cli.IntFlag{Name: "test", Value: 7}),
|
||||||
&cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
|
||||||
@ -216,7 +215,7 @@ func TestCommandJSONFileTestDefaultValueFileWinsNested(t *testing.T) {
|
|||||||
cleanup := writeTempFile(t, fileName, nestedJSON)
|
cleanup := writeTempFile(t, fileName, nestedJSON)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
test := []string{"test-cmd", "--load", fileName}
|
test := []string{"test-cmd", "--load", fileName}
|
||||||
_ = set.Parse(test)
|
_ = set.Parse(test)
|
||||||
@ -234,7 +233,7 @@ func TestCommandJSONFileTestDefaultValueFileWinsNested(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "top.test", Value: 7}),
|
NewIntFlag(&cli.IntFlag{Name: "top.test", Value: 7}),
|
||||||
&cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
|
||||||
@ -248,7 +247,7 @@ func TestCommandJSONFileFlagHasDefaultGlobalEnvJSONSetGlobalEnvWins(t *testing.T
|
|||||||
cleanup := writeTempFile(t, fileName, simpleJSON)
|
cleanup := writeTempFile(t, fileName, simpleJSON)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
_ = os.Setenv("THE_TEST", "11")
|
_ = os.Setenv("THE_TEST", "11")
|
||||||
defer os.Setenv("THE_TEST", "")
|
defer os.Setenv("THE_TEST", "")
|
||||||
@ -269,7 +268,7 @@ func TestCommandJSONFileFlagHasDefaultGlobalEnvJSONSetGlobalEnvWins(t *testing.T
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "test", Value: 7, EnvVar: "THE_TEST"}),
|
NewIntFlag(&cli.IntFlag{Name: "test", Value: 7, EnvVars: []string{"THE_TEST"}}),
|
||||||
&cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
|
||||||
@ -282,7 +281,7 @@ func TestCommandJSONFileFlagHasDefaultGlobalEnvJSONSetGlobalEnvWinsNested(t *tes
|
|||||||
cleanup := writeTempFile(t, fileName, nestedJSON)
|
cleanup := writeTempFile(t, fileName, nestedJSON)
|
||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
_ = os.Setenv("THE_TEST", "11")
|
_ = os.Setenv("THE_TEST", "11")
|
||||||
defer os.Setenv("THE_TEST", "")
|
defer os.Setenv("THE_TEST", "")
|
||||||
@ -303,7 +302,7 @@ func TestCommandJSONFileFlagHasDefaultGlobalEnvJSONSetGlobalEnvWinsNested(t *tes
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "top.test", Value: 7, EnvVar: "THE_TEST"}),
|
NewIntFlag(&cli.IntFlag{Name: "top.test", Value: 7, EnvVars: []string{"THE_TEST"}}),
|
||||||
&cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewJSONSourceFromFlagFunc("load"))
|
||||||
|
@ -3,12 +3,11 @@ package altsrc
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/urfave/cli"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewJSONSourceFromFlagFunc returns a func that takes a cli.Context
|
// NewJSONSourceFromFlagFunc returns a func that takes a cli.Context
|
||||||
@ -29,6 +28,7 @@ func NewJSONSourceFromFile(f string) (InputSourceContext, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return NewJSONSource(data)
|
return NewJSONSource(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,6 +52,10 @@ func NewJSONSource(data []byte) (InputSourceContext, error) {
|
|||||||
return &jsonSource{deserialized: deserialized}, nil
|
return &jsonSource{deserialized: deserialized}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (x *jsonSource) Source() string {
|
||||||
|
return x.file
|
||||||
|
}
|
||||||
|
|
||||||
func (x *jsonSource) Int(name string) (int, error) {
|
func (x *jsonSource) Int(name string) (int, error) {
|
||||||
i, err := x.getValue(name)
|
i, err := x.getValue(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -62,10 +66,10 @@ func (x *jsonSource) Int(name string) (int, error) {
|
|||||||
return 0, fmt.Errorf("unexpected type %T for %q", i, name)
|
return 0, fmt.Errorf("unexpected type %T for %q", i, name)
|
||||||
case int:
|
case int:
|
||||||
return v, nil
|
return v, nil
|
||||||
case float64:
|
|
||||||
return int(float64(v)), nil
|
|
||||||
case float32:
|
case float32:
|
||||||
return int(float32(v)), nil
|
return int(v), nil
|
||||||
|
case float64:
|
||||||
|
return int(v), nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,12 +179,6 @@ func (x *jsonSource) Bool(name string) (bool, error) {
|
|||||||
return v, nil
|
return v, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// since this source appears to require all configuration to be specified, the
|
|
||||||
// concept of a boolean defaulting to true seems inconsistent with no defaults
|
|
||||||
func (x *jsonSource) BoolT(name string) (bool, error) {
|
|
||||||
return false, fmt.Errorf("unsupported type BoolT for JSONSource")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (x *jsonSource) getValue(key string) (interface{}, error) {
|
func (x *jsonSource) getValue(key string) (interface{}, error) {
|
||||||
return jsonGetValue(key, x.deserialized)
|
return jsonGetValue(key, x.deserialized)
|
||||||
}
|
}
|
||||||
@ -204,5 +202,6 @@ func jsonGetValue(key string, m map[string]interface{}) (interface{}, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type jsonSource struct {
|
type jsonSource struct {
|
||||||
|
file string
|
||||||
deserialized map[string]interface{}
|
deserialized map[string]interface{}
|
||||||
}
|
}
|
||||||
|
@ -2,16 +2,16 @@ package altsrc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/urfave/cli"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// MapInputSource implements InputSourceContext to return
|
// MapInputSource implements InputSourceContext to return
|
||||||
// data from the map that is loaded.
|
// data from the map that is loaded.
|
||||||
type MapInputSource struct {
|
type MapInputSource struct {
|
||||||
|
file string
|
||||||
valueMap map[interface{}]interface{}
|
valueMap map[interface{}]interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -39,6 +39,11 @@ func nestedVal(name string, tree map[interface{}]interface{}) (interface{}, bool
|
|||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Source returns the path of the source file
|
||||||
|
func (fsm *MapInputSource) Source() string {
|
||||||
|
return fsm.file
|
||||||
|
}
|
||||||
|
|
||||||
// Int returns an int from the map if it exists otherwise returns 0
|
// Int returns an int from the map if it exists otherwise returns 0
|
||||||
func (fsm *MapInputSource) Int(name string) (int, error) {
|
func (fsm *MapInputSource) Int(name string) (int, error) {
|
||||||
otherGenericValue, exists := fsm.valueMap[name]
|
otherGenericValue, exists := fsm.valueMap[name]
|
||||||
@ -65,24 +70,28 @@ func (fsm *MapInputSource) Int(name string) (int, error) {
|
|||||||
func (fsm *MapInputSource) Duration(name string) (time.Duration, error) {
|
func (fsm *MapInputSource) Duration(name string) (time.Duration, error) {
|
||||||
otherGenericValue, exists := fsm.valueMap[name]
|
otherGenericValue, exists := fsm.valueMap[name]
|
||||||
if exists {
|
if exists {
|
||||||
otherValue, isType := otherGenericValue.(time.Duration)
|
return castDuration(name, otherGenericValue)
|
||||||
if !isType {
|
|
||||||
return 0, incorrectTypeForFlagError(name, "duration", otherGenericValue)
|
|
||||||
}
|
|
||||||
return otherValue, nil
|
|
||||||
}
|
}
|
||||||
nestedGenericValue, exists := nestedVal(name, fsm.valueMap)
|
nestedGenericValue, exists := nestedVal(name, fsm.valueMap)
|
||||||
if exists {
|
if exists {
|
||||||
otherValue, isType := nestedGenericValue.(time.Duration)
|
return castDuration(name, nestedGenericValue)
|
||||||
if !isType {
|
|
||||||
return 0, incorrectTypeForFlagError(name, "duration", nestedGenericValue)
|
|
||||||
}
|
|
||||||
return otherValue, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func castDuration(name string, value interface{}) (time.Duration, error) {
|
||||||
|
if otherValue, isType := value.(time.Duration); isType {
|
||||||
|
return otherValue, nil
|
||||||
|
}
|
||||||
|
otherStringValue, isType := value.(string)
|
||||||
|
parsedValue, err := time.ParseDuration(otherStringValue)
|
||||||
|
if !isType || err != nil {
|
||||||
|
return 0, incorrectTypeForFlagError(name, "duration", value)
|
||||||
|
}
|
||||||
|
return parsedValue, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Float64 returns an float64 from the map if it exists otherwise returns 0
|
// Float64 returns an float64 from the map if it exists otherwise returns 0
|
||||||
func (fsm *MapInputSource) Float64(name string) (float64, error) {
|
func (fsm *MapInputSource) Float64(name string) (float64, error) {
|
||||||
otherGenericValue, exists := fsm.valueMap[name]
|
otherGenericValue, exists := fsm.valueMap[name]
|
||||||
@ -229,28 +238,6 @@ func (fsm *MapInputSource) Bool(name string) (bool, error) {
|
|||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// BoolT returns an bool from the map otherwise returns true
|
|
||||||
func (fsm *MapInputSource) BoolT(name string) (bool, error) {
|
|
||||||
otherGenericValue, exists := fsm.valueMap[name]
|
|
||||||
if exists {
|
|
||||||
otherValue, isType := otherGenericValue.(bool)
|
|
||||||
if !isType {
|
|
||||||
return true, incorrectTypeForFlagError(name, "bool", otherGenericValue)
|
|
||||||
}
|
|
||||||
return otherValue, nil
|
|
||||||
}
|
|
||||||
nestedGenericValue, exists := nestedVal(name, fsm.valueMap)
|
|
||||||
if exists {
|
|
||||||
otherValue, isType := nestedGenericValue.(bool)
|
|
||||||
if !isType {
|
|
||||||
return true, incorrectTypeForFlagError(name, "bool", nestedGenericValue)
|
|
||||||
}
|
|
||||||
return otherValue, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func incorrectTypeForFlagError(name, expectedTypeName string, value interface{}) error {
|
func incorrectTypeForFlagError(name, expectedTypeName string, value interface{}) error {
|
||||||
valueType := reflect.TypeOf(value)
|
valueType := reflect.TypeOf(value)
|
||||||
valueTypeName := ""
|
valueTypeName := ""
|
||||||
|
25
altsrc/map_input_source_test.go
Normal file
25
altsrc/map_input_source_test.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package altsrc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMapDuration(t *testing.T) {
|
||||||
|
inputSource := &MapInputSource{
|
||||||
|
file: "test",
|
||||||
|
valueMap: map[interface{}]interface{}{
|
||||||
|
"duration_of_duration_type": time.Minute,
|
||||||
|
"duration_of_string_type": "1m",
|
||||||
|
"duration_of_int_type": 1000,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
d, err := inputSource.Duration("duration_of_duration_type")
|
||||||
|
expect(t, time.Minute, d)
|
||||||
|
expect(t, nil, err)
|
||||||
|
d, err = inputSource.Duration("duration_of_string_type")
|
||||||
|
expect(t, time.Minute, d)
|
||||||
|
expect(t, nil, err)
|
||||||
|
d, err = inputSource.Duration("duration_of_int_type")
|
||||||
|
refute(t, nil, err)
|
||||||
|
}
|
@ -1,26 +1,20 @@
|
|||||||
// Disabling building of toml support in cases where golang is 1.0 or 1.1
|
|
||||||
// as the encoding library is not implemented or supported.
|
|
||||||
|
|
||||||
// +build go1.2
|
|
||||||
|
|
||||||
package altsrc
|
package altsrc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/urfave/cli"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCommandTomFileTest(t *testing.T) {
|
func TestCommandTomFileTest(t *testing.T) {
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
|
_ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
|
||||||
defer os.Remove("current.toml")
|
defer os.Remove("current.toml")
|
||||||
test := []string{"test-cmd", "--load", "current.toml"}
|
test := []string{"test-cmd", "--load", "current.toml"}
|
||||||
set.Parse(test)
|
_ = set.Parse(test)
|
||||||
|
|
||||||
c := cli.NewContext(app, set, nil)
|
c := cli.NewContext(app, set, nil)
|
||||||
|
|
||||||
@ -35,8 +29,8 @@ func TestCommandTomFileTest(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "test"}),
|
NewIntFlag(&cli.IntFlag{Name: "test"}),
|
||||||
cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
|
||||||
err := command.Run(c)
|
err := command.Run(c)
|
||||||
@ -45,15 +39,15 @@ func TestCommandTomFileTest(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCommandTomlFileTestGlobalEnvVarWins(t *testing.T) {
|
func TestCommandTomlFileTestGlobalEnvVarWins(t *testing.T) {
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
|
_ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
|
||||||
defer os.Remove("current.toml")
|
defer os.Remove("current.toml")
|
||||||
|
|
||||||
os.Setenv("THE_TEST", "10")
|
_ = os.Setenv("THE_TEST", "10")
|
||||||
defer os.Setenv("THE_TEST", "")
|
defer os.Setenv("THE_TEST", "")
|
||||||
test := []string{"test-cmd", "--load", "current.toml"}
|
test := []string{"test-cmd", "--load", "current.toml"}
|
||||||
set.Parse(test)
|
_ = set.Parse(test)
|
||||||
|
|
||||||
c := cli.NewContext(app, set, nil)
|
c := cli.NewContext(app, set, nil)
|
||||||
|
|
||||||
@ -68,8 +62,8 @@ func TestCommandTomlFileTestGlobalEnvVarWins(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "test", EnvVar: "THE_TEST"}),
|
NewIntFlag(&cli.IntFlag{Name: "test", EnvVars: []string{"THE_TEST"}}),
|
||||||
cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
|
||||||
|
|
||||||
@ -79,15 +73,15 @@ func TestCommandTomlFileTestGlobalEnvVarWins(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCommandTomlFileTestGlobalEnvVarWinsNested(t *testing.T) {
|
func TestCommandTomlFileTestGlobalEnvVarWinsNested(t *testing.T) {
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
|
_ = ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
|
||||||
defer os.Remove("current.toml")
|
defer os.Remove("current.toml")
|
||||||
|
|
||||||
os.Setenv("THE_TEST", "10")
|
_ = os.Setenv("THE_TEST", "10")
|
||||||
defer os.Setenv("THE_TEST", "")
|
defer os.Setenv("THE_TEST", "")
|
||||||
test := []string{"test-cmd", "--load", "current.toml"}
|
test := []string{"test-cmd", "--load", "current.toml"}
|
||||||
set.Parse(test)
|
_ = set.Parse(test)
|
||||||
|
|
||||||
c := cli.NewContext(app, set, nil)
|
c := cli.NewContext(app, set, nil)
|
||||||
|
|
||||||
@ -102,8 +96,8 @@ func TestCommandTomlFileTestGlobalEnvVarWinsNested(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "top.test", EnvVar: "THE_TEST"}),
|
NewIntFlag(&cli.IntFlag{Name: "top.test", EnvVars: []string{"THE_TEST"}}),
|
||||||
cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
|
||||||
|
|
||||||
@ -113,13 +107,13 @@ func TestCommandTomlFileTestGlobalEnvVarWinsNested(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCommandTomlFileTestSpecifiedFlagWins(t *testing.T) {
|
func TestCommandTomlFileTestSpecifiedFlagWins(t *testing.T) {
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
|
_ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
|
||||||
defer os.Remove("current.toml")
|
defer os.Remove("current.toml")
|
||||||
|
|
||||||
test := []string{"test-cmd", "--load", "current.toml", "--test", "7"}
|
test := []string{"test-cmd", "--load", "current.toml", "--test", "7"}
|
||||||
set.Parse(test)
|
_ = set.Parse(test)
|
||||||
|
|
||||||
c := cli.NewContext(app, set, nil)
|
c := cli.NewContext(app, set, nil)
|
||||||
|
|
||||||
@ -134,8 +128,8 @@ func TestCommandTomlFileTestSpecifiedFlagWins(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "test"}),
|
NewIntFlag(&cli.IntFlag{Name: "test"}),
|
||||||
cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
|
||||||
|
|
||||||
@ -145,14 +139,14 @@ func TestCommandTomlFileTestSpecifiedFlagWins(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCommandTomlFileTestSpecifiedFlagWinsNested(t *testing.T) {
|
func TestCommandTomlFileTestSpecifiedFlagWinsNested(t *testing.T) {
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
ioutil.WriteFile("current.toml", []byte(`[top]
|
_ = ioutil.WriteFile("current.toml", []byte(`[top]
|
||||||
test = 15`), 0666)
|
test = 15`), 0666)
|
||||||
defer os.Remove("current.toml")
|
defer os.Remove("current.toml")
|
||||||
|
|
||||||
test := []string{"test-cmd", "--load", "current.toml", "--top.test", "7"}
|
test := []string{"test-cmd", "--load", "current.toml", "--top.test", "7"}
|
||||||
set.Parse(test)
|
_ = set.Parse(test)
|
||||||
|
|
||||||
c := cli.NewContext(app, set, nil)
|
c := cli.NewContext(app, set, nil)
|
||||||
|
|
||||||
@ -167,8 +161,8 @@ func TestCommandTomlFileTestSpecifiedFlagWinsNested(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "top.test"}),
|
NewIntFlag(&cli.IntFlag{Name: "top.test"}),
|
||||||
cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
|
||||||
|
|
||||||
@ -178,13 +172,13 @@ func TestCommandTomlFileTestSpecifiedFlagWinsNested(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCommandTomlFileTestDefaultValueFileWins(t *testing.T) {
|
func TestCommandTomlFileTestDefaultValueFileWins(t *testing.T) {
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
|
_ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
|
||||||
defer os.Remove("current.toml")
|
defer os.Remove("current.toml")
|
||||||
|
|
||||||
test := []string{"test-cmd", "--load", "current.toml"}
|
test := []string{"test-cmd", "--load", "current.toml"}
|
||||||
set.Parse(test)
|
_ = set.Parse(test)
|
||||||
|
|
||||||
c := cli.NewContext(app, set, nil)
|
c := cli.NewContext(app, set, nil)
|
||||||
|
|
||||||
@ -199,8 +193,8 @@ func TestCommandTomlFileTestDefaultValueFileWins(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "test", Value: 7}),
|
NewIntFlag(&cli.IntFlag{Name: "test", Value: 7}),
|
||||||
cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
|
||||||
|
|
||||||
@ -210,13 +204,13 @@ func TestCommandTomlFileTestDefaultValueFileWins(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCommandTomlFileTestDefaultValueFileWinsNested(t *testing.T) {
|
func TestCommandTomlFileTestDefaultValueFileWinsNested(t *testing.T) {
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
|
_ = ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
|
||||||
defer os.Remove("current.toml")
|
defer os.Remove("current.toml")
|
||||||
|
|
||||||
test := []string{"test-cmd", "--load", "current.toml"}
|
test := []string{"test-cmd", "--load", "current.toml"}
|
||||||
set.Parse(test)
|
_ = set.Parse(test)
|
||||||
|
|
||||||
c := cli.NewContext(app, set, nil)
|
c := cli.NewContext(app, set, nil)
|
||||||
|
|
||||||
@ -231,8 +225,8 @@ func TestCommandTomlFileTestDefaultValueFileWinsNested(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "top.test", Value: 7}),
|
NewIntFlag(&cli.IntFlag{Name: "top.test", Value: 7}),
|
||||||
cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
|
||||||
|
|
||||||
@ -242,16 +236,16 @@ func TestCommandTomlFileTestDefaultValueFileWinsNested(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCommandTomlFileFlagHasDefaultGlobalEnvTomlSetGlobalEnvWins(t *testing.T) {
|
func TestCommandTomlFileFlagHasDefaultGlobalEnvTomlSetGlobalEnvWins(t *testing.T) {
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
|
_ = ioutil.WriteFile("current.toml", []byte("test = 15"), 0666)
|
||||||
defer os.Remove("current.toml")
|
defer os.Remove("current.toml")
|
||||||
|
|
||||||
os.Setenv("THE_TEST", "11")
|
_ = os.Setenv("THE_TEST", "11")
|
||||||
defer os.Setenv("THE_TEST", "")
|
defer os.Setenv("THE_TEST", "")
|
||||||
|
|
||||||
test := []string{"test-cmd", "--load", "current.toml"}
|
test := []string{"test-cmd", "--load", "current.toml"}
|
||||||
set.Parse(test)
|
_ = set.Parse(test)
|
||||||
|
|
||||||
c := cli.NewContext(app, set, nil)
|
c := cli.NewContext(app, set, nil)
|
||||||
|
|
||||||
@ -266,8 +260,8 @@ func TestCommandTomlFileFlagHasDefaultGlobalEnvTomlSetGlobalEnvWins(t *testing.T
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "test", Value: 7, EnvVar: "THE_TEST"}),
|
NewIntFlag(&cli.IntFlag{Name: "test", Value: 7, EnvVars: []string{"THE_TEST"}}),
|
||||||
cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
|
||||||
err := command.Run(c)
|
err := command.Run(c)
|
||||||
@ -276,16 +270,16 @@ func TestCommandTomlFileFlagHasDefaultGlobalEnvTomlSetGlobalEnvWins(t *testing.T
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCommandTomlFileFlagHasDefaultGlobalEnvTomlSetGlobalEnvWinsNested(t *testing.T) {
|
func TestCommandTomlFileFlagHasDefaultGlobalEnvTomlSetGlobalEnvWinsNested(t *testing.T) {
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
|
_ = ioutil.WriteFile("current.toml", []byte("[top]\ntest = 15"), 0666)
|
||||||
defer os.Remove("current.toml")
|
defer os.Remove("current.toml")
|
||||||
|
|
||||||
os.Setenv("THE_TEST", "11")
|
_ = os.Setenv("THE_TEST", "11")
|
||||||
defer os.Setenv("THE_TEST", "")
|
defer os.Setenv("THE_TEST", "")
|
||||||
|
|
||||||
test := []string{"test-cmd", "--load", "current.toml"}
|
test := []string{"test-cmd", "--load", "current.toml"}
|
||||||
set.Parse(test)
|
_ = set.Parse(test)
|
||||||
|
|
||||||
c := cli.NewContext(app, set, nil)
|
c := cli.NewContext(app, set, nil)
|
||||||
|
|
||||||
@ -300,8 +294,8 @@ func TestCommandTomlFileFlagHasDefaultGlobalEnvTomlSetGlobalEnvWinsNested(t *tes
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "top.test", Value: 7, EnvVar: "THE_TEST"}),
|
NewIntFlag(&cli.IntFlag{Name: "top.test", Value: 7, EnvVars: []string{"THE_TEST"}}),
|
||||||
cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewTomlSourceFromFlagFunc("load"))
|
||||||
err := command.Run(c)
|
err := command.Run(c)
|
||||||
|
@ -1,8 +1,3 @@
|
|||||||
// Disabling building of toml support in cases where golang is 1.0 or 1.1
|
|
||||||
// as the encoding library is not implemented or supported.
|
|
||||||
|
|
||||||
// +build go1.2
|
|
||||||
|
|
||||||
package altsrc
|
package altsrc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -10,7 +5,7 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type tomlMap struct {
|
type tomlMap struct {
|
||||||
@ -28,7 +23,7 @@ func unmarshalMap(i interface{}) (ret map[interface{}]interface{}, err error) {
|
|||||||
case reflect.String:
|
case reflect.String:
|
||||||
ret[key] = val.(string)
|
ret[key] = val.(string)
|
||||||
case reflect.Int:
|
case reflect.Int:
|
||||||
ret[key] = int(val.(int))
|
ret[key] = val.(int)
|
||||||
case reflect.Int8:
|
case reflect.Int8:
|
||||||
ret[key] = int(val.(int8))
|
ret[key] = int(val.(int8))
|
||||||
case reflect.Int16:
|
case reflect.Int16:
|
||||||
@ -50,7 +45,7 @@ func unmarshalMap(i interface{}) (ret map[interface{}]interface{}, err error) {
|
|||||||
case reflect.Float32:
|
case reflect.Float32:
|
||||||
ret[key] = float64(val.(float32))
|
ret[key] = float64(val.(float32))
|
||||||
case reflect.Float64:
|
case reflect.Float64:
|
||||||
ret[key] = float64(val.(float64))
|
ret[key] = val.(float64)
|
||||||
case reflect.Map:
|
case reflect.Map:
|
||||||
if tmp, err := unmarshalMap(val); err == nil {
|
if tmp, err := unmarshalMap(val); err == nil {
|
||||||
ret[key] = tmp
|
ret[key] = tmp
|
||||||
@ -86,7 +81,7 @@ func NewTomlSourceFromFile(file string) (InputSourceContext, error) {
|
|||||||
if err := readCommandToml(tsc.FilePath, &results); err != nil {
|
if err := readCommandToml(tsc.FilePath, &results); err != nil {
|
||||||
return nil, fmt.Errorf("Unable to load TOML file '%s': inner error: \n'%v'", tsc.FilePath, err.Error())
|
return nil, fmt.Errorf("Unable to load TOML file '%s': inner error: \n'%v'", tsc.FilePath, err.Error())
|
||||||
}
|
}
|
||||||
return &MapInputSource{valueMap: results.Map}, nil
|
return &MapInputSource{file: file, valueMap: results.Map}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTomlSourceFromFlagFunc creates a new TOML InputSourceContext from a provided flag name and source context.
|
// NewTomlSourceFromFlagFunc creates a new TOML InputSourceContext from a provided flag name and source context.
|
||||||
|
@ -1,26 +1,20 @@
|
|||||||
// Disabling building of yaml support in cases where golang is 1.0 or 1.1
|
|
||||||
// as the encoding library is not implemented or supported.
|
|
||||||
|
|
||||||
// +build go1.2
|
|
||||||
|
|
||||||
package altsrc
|
package altsrc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/urfave/cli"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCommandYamlFileTest(t *testing.T) {
|
func TestCommandYamlFileTest(t *testing.T) {
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
|
_ = ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
|
||||||
defer os.Remove("current.yaml")
|
defer os.Remove("current.yaml")
|
||||||
test := []string{"test-cmd", "--load", "current.yaml"}
|
test := []string{"test-cmd", "--load", "current.yaml"}
|
||||||
set.Parse(test)
|
_ = set.Parse(test)
|
||||||
|
|
||||||
c := cli.NewContext(app, set, nil)
|
c := cli.NewContext(app, set, nil)
|
||||||
|
|
||||||
@ -35,8 +29,8 @@ func TestCommandYamlFileTest(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "test"}),
|
NewIntFlag(&cli.IntFlag{Name: "test"}),
|
||||||
cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
||||||
err := command.Run(c)
|
err := command.Run(c)
|
||||||
@ -45,15 +39,15 @@ func TestCommandYamlFileTest(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCommandYamlFileTestGlobalEnvVarWins(t *testing.T) {
|
func TestCommandYamlFileTestGlobalEnvVarWins(t *testing.T) {
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
|
_ = ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
|
||||||
defer os.Remove("current.yaml")
|
defer os.Remove("current.yaml")
|
||||||
|
|
||||||
os.Setenv("THE_TEST", "10")
|
_ = os.Setenv("THE_TEST", "10")
|
||||||
defer os.Setenv("THE_TEST", "")
|
defer os.Setenv("THE_TEST", "")
|
||||||
test := []string{"test-cmd", "--load", "current.yaml"}
|
test := []string{"test-cmd", "--load", "current.yaml"}
|
||||||
set.Parse(test)
|
_ = set.Parse(test)
|
||||||
|
|
||||||
c := cli.NewContext(app, set, nil)
|
c := cli.NewContext(app, set, nil)
|
||||||
|
|
||||||
@ -68,8 +62,8 @@ func TestCommandYamlFileTestGlobalEnvVarWins(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "test", EnvVar: "THE_TEST"}),
|
NewIntFlag(&cli.IntFlag{Name: "test", EnvVars: []string{"THE_TEST"}}),
|
||||||
cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
||||||
|
|
||||||
@ -79,16 +73,16 @@ func TestCommandYamlFileTestGlobalEnvVarWins(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCommandYamlFileTestGlobalEnvVarWinsNested(t *testing.T) {
|
func TestCommandYamlFileTestGlobalEnvVarWinsNested(t *testing.T) {
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
ioutil.WriteFile("current.yaml", []byte(`top:
|
_ = ioutil.WriteFile("current.yaml", []byte(`top:
|
||||||
test: 15`), 0666)
|
test: 15`), 0666)
|
||||||
defer os.Remove("current.yaml")
|
defer os.Remove("current.yaml")
|
||||||
|
|
||||||
os.Setenv("THE_TEST", "10")
|
_ = os.Setenv("THE_TEST", "10")
|
||||||
defer os.Setenv("THE_TEST", "")
|
defer os.Setenv("THE_TEST", "")
|
||||||
test := []string{"test-cmd", "--load", "current.yaml"}
|
test := []string{"test-cmd", "--load", "current.yaml"}
|
||||||
set.Parse(test)
|
_ = set.Parse(test)
|
||||||
|
|
||||||
c := cli.NewContext(app, set, nil)
|
c := cli.NewContext(app, set, nil)
|
||||||
|
|
||||||
@ -103,8 +97,8 @@ func TestCommandYamlFileTestGlobalEnvVarWinsNested(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "top.test", EnvVar: "THE_TEST"}),
|
NewIntFlag(&cli.IntFlag{Name: "top.test", EnvVars: []string{"THE_TEST"}}),
|
||||||
cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
||||||
|
|
||||||
@ -114,13 +108,13 @@ func TestCommandYamlFileTestGlobalEnvVarWinsNested(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCommandYamlFileTestSpecifiedFlagWins(t *testing.T) {
|
func TestCommandYamlFileTestSpecifiedFlagWins(t *testing.T) {
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
|
_ = ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
|
||||||
defer os.Remove("current.yaml")
|
defer os.Remove("current.yaml")
|
||||||
|
|
||||||
test := []string{"test-cmd", "--load", "current.yaml", "--test", "7"}
|
test := []string{"test-cmd", "--load", "current.yaml", "--test", "7"}
|
||||||
set.Parse(test)
|
_ = set.Parse(test)
|
||||||
|
|
||||||
c := cli.NewContext(app, set, nil)
|
c := cli.NewContext(app, set, nil)
|
||||||
|
|
||||||
@ -135,8 +129,8 @@ func TestCommandYamlFileTestSpecifiedFlagWins(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "test"}),
|
NewIntFlag(&cli.IntFlag{Name: "test"}),
|
||||||
cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
||||||
|
|
||||||
@ -146,14 +140,14 @@ func TestCommandYamlFileTestSpecifiedFlagWins(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCommandYamlFileTestSpecifiedFlagWinsNested(t *testing.T) {
|
func TestCommandYamlFileTestSpecifiedFlagWinsNested(t *testing.T) {
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
ioutil.WriteFile("current.yaml", []byte(`top:
|
_ = ioutil.WriteFile("current.yaml", []byte(`top:
|
||||||
test: 15`), 0666)
|
test: 15`), 0666)
|
||||||
defer os.Remove("current.yaml")
|
defer os.Remove("current.yaml")
|
||||||
|
|
||||||
test := []string{"test-cmd", "--load", "current.yaml", "--top.test", "7"}
|
test := []string{"test-cmd", "--load", "current.yaml", "--top.test", "7"}
|
||||||
set.Parse(test)
|
_ = set.Parse(test)
|
||||||
|
|
||||||
c := cli.NewContext(app, set, nil)
|
c := cli.NewContext(app, set, nil)
|
||||||
|
|
||||||
@ -168,8 +162,8 @@ func TestCommandYamlFileTestSpecifiedFlagWinsNested(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "top.test"}),
|
NewIntFlag(&cli.IntFlag{Name: "top.test"}),
|
||||||
cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
||||||
|
|
||||||
@ -179,13 +173,13 @@ func TestCommandYamlFileTestSpecifiedFlagWinsNested(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCommandYamlFileTestDefaultValueFileWins(t *testing.T) {
|
func TestCommandYamlFileTestDefaultValueFileWins(t *testing.T) {
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
|
_ = ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
|
||||||
defer os.Remove("current.yaml")
|
defer os.Remove("current.yaml")
|
||||||
|
|
||||||
test := []string{"test-cmd", "--load", "current.yaml"}
|
test := []string{"test-cmd", "--load", "current.yaml"}
|
||||||
set.Parse(test)
|
_ = set.Parse(test)
|
||||||
|
|
||||||
c := cli.NewContext(app, set, nil)
|
c := cli.NewContext(app, set, nil)
|
||||||
|
|
||||||
@ -200,8 +194,8 @@ func TestCommandYamlFileTestDefaultValueFileWins(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "test", Value: 7}),
|
NewIntFlag(&cli.IntFlag{Name: "test", Value: 7}),
|
||||||
cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
||||||
|
|
||||||
@ -211,14 +205,14 @@ func TestCommandYamlFileTestDefaultValueFileWins(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCommandYamlFileTestDefaultValueFileWinsNested(t *testing.T) {
|
func TestCommandYamlFileTestDefaultValueFileWinsNested(t *testing.T) {
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
ioutil.WriteFile("current.yaml", []byte(`top:
|
_ = ioutil.WriteFile("current.yaml", []byte(`top:
|
||||||
test: 15`), 0666)
|
test: 15`), 0666)
|
||||||
defer os.Remove("current.yaml")
|
defer os.Remove("current.yaml")
|
||||||
|
|
||||||
test := []string{"test-cmd", "--load", "current.yaml"}
|
test := []string{"test-cmd", "--load", "current.yaml"}
|
||||||
set.Parse(test)
|
_ = set.Parse(test)
|
||||||
|
|
||||||
c := cli.NewContext(app, set, nil)
|
c := cli.NewContext(app, set, nil)
|
||||||
|
|
||||||
@ -233,8 +227,8 @@ func TestCommandYamlFileTestDefaultValueFileWinsNested(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "top.test", Value: 7}),
|
NewIntFlag(&cli.IntFlag{Name: "top.test", Value: 7}),
|
||||||
cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
||||||
|
|
||||||
@ -244,16 +238,16 @@ func TestCommandYamlFileTestDefaultValueFileWinsNested(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWins(t *testing.T) {
|
func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWins(t *testing.T) {
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
|
_ = ioutil.WriteFile("current.yaml", []byte("test: 15"), 0666)
|
||||||
defer os.Remove("current.yaml")
|
defer os.Remove("current.yaml")
|
||||||
|
|
||||||
os.Setenv("THE_TEST", "11")
|
_ = os.Setenv("THE_TEST", "11")
|
||||||
defer os.Setenv("THE_TEST", "")
|
defer os.Setenv("THE_TEST", "")
|
||||||
|
|
||||||
test := []string{"test-cmd", "--load", "current.yaml"}
|
test := []string{"test-cmd", "--load", "current.yaml"}
|
||||||
set.Parse(test)
|
_ = set.Parse(test)
|
||||||
|
|
||||||
c := cli.NewContext(app, set, nil)
|
c := cli.NewContext(app, set, nil)
|
||||||
|
|
||||||
@ -268,8 +262,8 @@ func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWins(t *testing.T
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "test", Value: 7, EnvVar: "THE_TEST"}),
|
NewIntFlag(&cli.IntFlag{Name: "test", Value: 7, EnvVars: []string{"THE_TEST"}}),
|
||||||
cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
||||||
err := command.Run(c)
|
err := command.Run(c)
|
||||||
@ -278,17 +272,17 @@ func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWins(t *testing.T
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWinsNested(t *testing.T) {
|
func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWinsNested(t *testing.T) {
|
||||||
app := cli.NewApp()
|
app := &cli.App{}
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
ioutil.WriteFile("current.yaml", []byte(`top:
|
_ = ioutil.WriteFile("current.yaml", []byte(`top:
|
||||||
test: 15`), 0666)
|
test: 15`), 0666)
|
||||||
defer os.Remove("current.yaml")
|
defer os.Remove("current.yaml")
|
||||||
|
|
||||||
os.Setenv("THE_TEST", "11")
|
_ = os.Setenv("THE_TEST", "11")
|
||||||
defer os.Setenv("THE_TEST", "")
|
defer os.Setenv("THE_TEST", "")
|
||||||
|
|
||||||
test := []string{"test-cmd", "--load", "current.yaml"}
|
test := []string{"test-cmd", "--load", "current.yaml"}
|
||||||
set.Parse(test)
|
_ = set.Parse(test)
|
||||||
|
|
||||||
c := cli.NewContext(app, set, nil)
|
c := cli.NewContext(app, set, nil)
|
||||||
|
|
||||||
@ -303,8 +297,8 @@ func TestCommandYamlFileFlagHasDefaultGlobalEnvYamlSetGlobalEnvWinsNested(t *tes
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
NewIntFlag(cli.IntFlag{Name: "top.test", Value: 7, EnvVar: "THE_TEST"}),
|
NewIntFlag(&cli.IntFlag{Name: "top.test", Value: 7, EnvVars: []string{"THE_TEST"}}),
|
||||||
cli.StringFlag{Name: "load"}},
|
&cli.StringFlag{Name: "load"}},
|
||||||
}
|
}
|
||||||
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
command.Before = InitInputSourceWithContext(command.Flags, NewYamlSourceFromFlagFunc("load"))
|
||||||
err := command.Run(c)
|
err := command.Run(c)
|
||||||
|
@ -1,8 +1,3 @@
|
|||||||
// Disabling building of yaml support in cases where golang is 1.0 or 1.1
|
|
||||||
// as the encoding library is not implemented or supported.
|
|
||||||
|
|
||||||
// +build go1.2
|
|
||||||
|
|
||||||
package altsrc
|
package altsrc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -14,7 +9,7 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli/v2"
|
||||||
|
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
@ -32,7 +27,7 @@ func NewYamlSourceFromFile(file string) (InputSourceContext, error) {
|
|||||||
return nil, fmt.Errorf("Unable to load Yaml file '%s': inner error: \n'%v'", ysc.FilePath, err.Error())
|
return nil, fmt.Errorf("Unable to load Yaml file '%s': inner error: \n'%v'", ysc.FilePath, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return &MapInputSource{valueMap: results}, nil
|
return &MapInputSource{file: file, valueMap: results}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewYamlSourceFromFlagFunc creates a new Yaml InputSourceContext from a provided flag name and source context.
|
// NewYamlSourceFromFlagFunc creates a new Yaml InputSourceContext from a provided flag name and source context.
|
||||||
|
137
app.go
137
app.go
@ -6,6 +6,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -13,12 +14,8 @@ import (
|
|||||||
var (
|
var (
|
||||||
changeLogURL = "https://github.com/urfave/cli/blob/master/CHANGELOG.md"
|
changeLogURL = "https://github.com/urfave/cli/blob/master/CHANGELOG.md"
|
||||||
appActionDeprecationURL = fmt.Sprintf("%s#deprecated-cli-app-action-signature", changeLogURL)
|
appActionDeprecationURL = fmt.Sprintf("%s#deprecated-cli-app-action-signature", changeLogURL)
|
||||||
// unused variable. commented for now. will remove in future if agreed upon by everyone
|
contactSysadmin = "This is an error in the application. Please contact the distributor of this application if this is not you."
|
||||||
//runAndExitOnErrorDeprecationURL = fmt.Sprintf("%s#deprecated-cli-app-runandexitonerror", changeLogURL)
|
errInvalidActionType = NewExitError("ERROR invalid Action type. "+
|
||||||
|
|
||||||
contactSysadmin = "This is an error in the application. Please contact the distributor of this application if this is not you."
|
|
||||||
|
|
||||||
errInvalidActionType = NewExitError("ERROR invalid Action type. "+
|
|
||||||
fmt.Sprintf("Must be `func(*Context`)` or `func(*Context) error). %s", contactSysadmin)+
|
fmt.Sprintf("Must be `func(*Context`)` or `func(*Context) error). %s", contactSysadmin)+
|
||||||
fmt.Sprintf("See %s", appActionDeprecationURL), 2)
|
fmt.Sprintf("See %s", appActionDeprecationURL), 2)
|
||||||
)
|
)
|
||||||
@ -41,7 +38,7 @@ type App struct {
|
|||||||
// Description of the program
|
// Description of the program
|
||||||
Description string
|
Description string
|
||||||
// List of commands to execute
|
// List of commands to execute
|
||||||
Commands []Command
|
Commands []*Command
|
||||||
// List of flags to parse
|
// List of flags to parse
|
||||||
Flags []Flag
|
Flags []Flag
|
||||||
// Boolean to enable bash completion commands
|
// Boolean to enable bash completion commands
|
||||||
@ -50,9 +47,9 @@ type App struct {
|
|||||||
HideHelp bool
|
HideHelp bool
|
||||||
// Boolean to hide built-in version flag and the VERSION section of help
|
// Boolean to hide built-in version flag and the VERSION section of help
|
||||||
HideVersion bool
|
HideVersion bool
|
||||||
// Populate on app startup, only gettable through method Categories()
|
// categories contains the categorized commands and is populated on app startup
|
||||||
categories CommandCategories
|
categories CommandCategories
|
||||||
// An action to execute when the bash-completion flag is set
|
// An action to execute when the shell completion flag is set
|
||||||
BashComplete BashCompleteFunc
|
BashComplete BashCompleteFunc
|
||||||
// An action to execute before any subcommands are run, but after the context is ready
|
// An action to execute before any subcommands are run, but after the context is ready
|
||||||
// If a non-nil error is returned, no subcommands are run
|
// If a non-nil error is returned, no subcommands are run
|
||||||
@ -60,12 +57,8 @@ type App struct {
|
|||||||
// An action to execute after any subcommands are run, but after the subcommand has finished
|
// An action to execute after any subcommands are run, but after the subcommand has finished
|
||||||
// It is run even if Action() panics
|
// It is run even if Action() panics
|
||||||
After AfterFunc
|
After AfterFunc
|
||||||
|
|
||||||
// The action to execute when no subcommands are specified
|
// The action to execute when no subcommands are specified
|
||||||
// Expects a `cli.ActionFunc` but will accept the *deprecated* signature of `func(*cli.Context) {}`
|
Action ActionFunc
|
||||||
// *Note*: support for the deprecated `Action` signature will be removed in a future version
|
|
||||||
Action interface{}
|
|
||||||
|
|
||||||
// Execute this function if the proper command cannot be found
|
// Execute this function if the proper command cannot be found
|
||||||
CommandNotFound CommandNotFoundFunc
|
CommandNotFound CommandNotFoundFunc
|
||||||
// Execute this function if an usage error occurs
|
// Execute this function if an usage error occurs
|
||||||
@ -73,13 +66,9 @@ type App struct {
|
|||||||
// Compilation date
|
// Compilation date
|
||||||
Compiled time.Time
|
Compiled time.Time
|
||||||
// List of all authors who contributed
|
// List of all authors who contributed
|
||||||
Authors []Author
|
Authors []*Author
|
||||||
// Copyright of the binary if any
|
// Copyright of the binary if any
|
||||||
Copyright string
|
Copyright string
|
||||||
// Name of Author (Note: Use App.Authors, this is deprecated)
|
|
||||||
Author string
|
|
||||||
// Email of Author (Note: Use App.Authors, this is deprecated)
|
|
||||||
Email string
|
|
||||||
// Writer writer to write output to
|
// Writer writer to write output to
|
||||||
Writer io.Writer
|
Writer io.Writer
|
||||||
// ErrWriter writes error output
|
// ErrWriter writes error output
|
||||||
@ -96,7 +85,7 @@ type App struct {
|
|||||||
// render custom help text by setting this variable.
|
// render custom help text by setting this variable.
|
||||||
CustomAppHelpTemplate string
|
CustomAppHelpTemplate string
|
||||||
// Boolean to enable short-option handling so user can combine several
|
// Boolean to enable short-option handling so user can combine several
|
||||||
// single-character bool arguements into one
|
// single-character bool arguments into one
|
||||||
// i.e. foobar -o -v -> foobar -ov
|
// i.e. foobar -o -v -> foobar -ov
|
||||||
UseShortOptionHandling bool
|
UseShortOptionHandling bool
|
||||||
|
|
||||||
@ -139,22 +128,52 @@ func (a *App) Setup() {
|
|||||||
|
|
||||||
a.didSetup = true
|
a.didSetup = true
|
||||||
|
|
||||||
if a.Author != "" || a.Email != "" {
|
if a.Name == "" {
|
||||||
a.Authors = append(a.Authors, Author{Name: a.Author, Email: a.Email})
|
a.Name = filepath.Base(os.Args[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
var newCmds []Command
|
if a.HelpName == "" {
|
||||||
|
a.HelpName = filepath.Base(os.Args[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
if a.Usage == "" {
|
||||||
|
a.Usage = "A new cli application"
|
||||||
|
}
|
||||||
|
|
||||||
|
if a.Version == "" {
|
||||||
|
a.Version = "0.0.0"
|
||||||
|
}
|
||||||
|
|
||||||
|
if a.BashComplete == nil {
|
||||||
|
a.BashComplete = DefaultAppComplete
|
||||||
|
}
|
||||||
|
|
||||||
|
if a.Action == nil {
|
||||||
|
a.Action = helpCommand.Action
|
||||||
|
}
|
||||||
|
|
||||||
|
if a.Compiled == (time.Time{}) {
|
||||||
|
a.Compiled = compileTime()
|
||||||
|
}
|
||||||
|
|
||||||
|
if a.Writer == nil {
|
||||||
|
a.Writer = os.Stdout
|
||||||
|
}
|
||||||
|
|
||||||
|
var newCommands []*Command
|
||||||
|
|
||||||
for _, c := range a.Commands {
|
for _, c := range a.Commands {
|
||||||
if c.HelpName == "" {
|
if c.HelpName == "" {
|
||||||
c.HelpName = fmt.Sprintf("%s %s", a.HelpName, c.Name)
|
c.HelpName = fmt.Sprintf("%s %s", a.HelpName, c.Name)
|
||||||
}
|
}
|
||||||
newCmds = append(newCmds, c)
|
newCommands = append(newCommands, c)
|
||||||
}
|
}
|
||||||
a.Commands = newCmds
|
a.Commands = newCommands
|
||||||
|
|
||||||
if a.Command(helpCommand.Name) == nil && !a.HideHelp {
|
if a.Command(helpCommand.Name) == nil && !a.HideHelp {
|
||||||
a.Commands = append(a.Commands, helpCommand)
|
a.appendCommand(helpCommand)
|
||||||
if (HelpFlag != BoolFlag{}) {
|
|
||||||
|
if HelpFlag != nil {
|
||||||
a.appendFlag(HelpFlag)
|
a.appendFlag(HelpFlag)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -163,11 +182,11 @@ func (a *App) Setup() {
|
|||||||
a.appendFlag(VersionFlag)
|
a.appendFlag(VersionFlag)
|
||||||
}
|
}
|
||||||
|
|
||||||
a.categories = CommandCategories{}
|
a.categories = newCommandCategories()
|
||||||
for _, command := range a.Commands {
|
for _, command := range a.Commands {
|
||||||
a.categories = a.categories.AddCommand(command.Category, command)
|
a.categories.AddCommand(command.Category, command)
|
||||||
}
|
}
|
||||||
sort.Sort(a.categories)
|
sort.Sort(a.categories.(*commandCategories))
|
||||||
|
|
||||||
if a.Metadata == nil {
|
if a.Metadata == nil {
|
||||||
a.Metadata = make(map[string]interface{})
|
a.Metadata = make(map[string]interface{})
|
||||||
@ -249,7 +268,7 @@ func (a *App) Run(arguments []string) (err error) {
|
|||||||
defer func() {
|
defer func() {
|
||||||
if afterErr := a.After(context); afterErr != nil {
|
if afterErr := a.After(context); afterErr != nil {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = NewMultiError(err, afterErr)
|
err = newMultiError(err, afterErr)
|
||||||
} else {
|
} else {
|
||||||
err = afterErr
|
err = afterErr
|
||||||
}
|
}
|
||||||
@ -282,7 +301,7 @@ func (a *App) Run(arguments []string) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Run default Action
|
// Run default Action
|
||||||
err = HandleAction(a.Action, context)
|
err = a.Action(context)
|
||||||
|
|
||||||
a.handleExitCoder(context, err)
|
a.handleExitCoder(context, err)
|
||||||
return err
|
return err
|
||||||
@ -303,17 +322,20 @@ func (a *App) RunAndExitOnError() {
|
|||||||
// RunAsSubcommand invokes the subcommand given the context, parses ctx.Args() to
|
// RunAsSubcommand invokes the subcommand given the context, parses ctx.Args() to
|
||||||
// generate command-specific flags
|
// generate command-specific flags
|
||||||
func (a *App) RunAsSubcommand(ctx *Context) (err error) {
|
func (a *App) RunAsSubcommand(ctx *Context) (err error) {
|
||||||
|
a.Setup()
|
||||||
|
|
||||||
// append help to commands
|
// append help to commands
|
||||||
if len(a.Commands) > 0 {
|
if len(a.Commands) > 0 {
|
||||||
if a.Command(helpCommand.Name) == nil && !a.HideHelp {
|
if a.Command(helpCommand.Name) == nil && !a.HideHelp {
|
||||||
a.Commands = append(a.Commands, helpCommand)
|
a.appendCommand(helpCommand)
|
||||||
if (HelpFlag != BoolFlag{}) {
|
|
||||||
|
if HelpFlag != nil {
|
||||||
a.appendFlag(HelpFlag)
|
a.appendFlag(HelpFlag)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
newCmds := []Command{}
|
var newCmds []*Command
|
||||||
for _, c := range a.Commands {
|
for _, c := range a.Commands {
|
||||||
if c.HelpName == "" {
|
if c.HelpName == "" {
|
||||||
c.HelpName = fmt.Sprintf("%s %s", a.HelpName, c.Name)
|
c.HelpName = fmt.Sprintf("%s %s", a.HelpName, c.Name)
|
||||||
@ -379,7 +401,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
|
|||||||
if afterErr != nil {
|
if afterErr != nil {
|
||||||
a.handleExitCoder(context, err)
|
a.handleExitCoder(context, err)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = NewMultiError(err, afterErr)
|
err = newMultiError(err, afterErr)
|
||||||
} else {
|
} else {
|
||||||
err = afterErr
|
err = afterErr
|
||||||
}
|
}
|
||||||
@ -406,7 +428,7 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Run default Action
|
// Run default Action
|
||||||
err = HandleAction(a.Action, context)
|
err = a.Action(context)
|
||||||
|
|
||||||
a.handleExitCoder(context, err)
|
a.handleExitCoder(context, err)
|
||||||
return err
|
return err
|
||||||
@ -416,28 +438,21 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
|
|||||||
func (a *App) Command(name string) *Command {
|
func (a *App) Command(name string) *Command {
|
||||||
for _, c := range a.Commands {
|
for _, c := range a.Commands {
|
||||||
if c.HasName(name) {
|
if c.HasName(name) {
|
||||||
return &c
|
return c
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Categories returns a slice containing all the categories with the commands they contain
|
|
||||||
func (a *App) Categories() CommandCategories {
|
|
||||||
return a.categories
|
|
||||||
}
|
|
||||||
|
|
||||||
// VisibleCategories returns a slice of categories and commands that are
|
// VisibleCategories returns a slice of categories and commands that are
|
||||||
// Hidden=false
|
// Hidden=false
|
||||||
func (a *App) VisibleCategories() []*CommandCategory {
|
func (a *App) VisibleCategories() []CommandCategory {
|
||||||
ret := []*CommandCategory{}
|
ret := []CommandCategory{}
|
||||||
for _, category := range a.categories {
|
for _, category := range a.categories.Categories() {
|
||||||
if visible := func() *CommandCategory {
|
if visible := func() CommandCategory {
|
||||||
for _, command := range category.Commands {
|
if len(category.VisibleCommands()) > 0 {
|
||||||
if !command.Hidden {
|
return category
|
||||||
return category
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}(); visible != nil {
|
}(); visible != nil {
|
||||||
@ -448,8 +463,8 @@ func (a *App) VisibleCategories() []*CommandCategory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// VisibleCommands returns a slice of the Commands with Hidden=false
|
// VisibleCommands returns a slice of the Commands with Hidden=false
|
||||||
func (a *App) VisibleCommands() []Command {
|
func (a *App) VisibleCommands() []*Command {
|
||||||
var ret []Command
|
var ret []*Command
|
||||||
for _, command := range a.Commands {
|
for _, command := range a.Commands {
|
||||||
if !command.Hidden {
|
if !command.Hidden {
|
||||||
ret = append(ret, command)
|
ret = append(ret, command)
|
||||||
@ -465,7 +480,7 @@ func (a *App) VisibleFlags() []Flag {
|
|||||||
|
|
||||||
func (a *App) hasFlag(flag Flag) bool {
|
func (a *App) hasFlag(flag Flag) bool {
|
||||||
for _, f := range a.Flags {
|
for _, f := range a.Flags {
|
||||||
if flag == f {
|
if reflect.DeepEqual(flag, f) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -482,9 +497,15 @@ func (a *App) errWriter() io.Writer {
|
|||||||
return a.ErrWriter
|
return a.ErrWriter
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) appendFlag(flag Flag) {
|
func (a *App) appendFlag(fl Flag) {
|
||||||
if !a.hasFlag(flag) {
|
if !hasFlag(a.Flags, fl) {
|
||||||
a.Flags = append(a.Flags, flag)
|
a.Flags = append(a.Flags, fl)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *App) appendCommand(c *Command) {
|
||||||
|
if !hasCommand(a.Commands, c) {
|
||||||
|
a.Commands = append(a.Commands, c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -503,7 +524,7 @@ type Author struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// String makes Author comply to the Stringer interface, to allow an easy print in the templating process
|
// String makes Author comply to the Stringer interface, to allow an easy print in the templating process
|
||||||
func (a Author) String() string {
|
func (a *Author) String() string {
|
||||||
e := ""
|
e := ""
|
||||||
if a.Email != "" {
|
if a.Email != "" {
|
||||||
e = " <" + a.Email + ">"
|
e = " <" + a.Email + ">"
|
||||||
|
@ -1,59 +0,0 @@
|
|||||||
package cli
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TestRegression tests a regression that was merged between versions 1.20.0 and 1.21.0
|
|
||||||
// The included app.Run line worked in 1.20.0, and then was broken in 1.21.0.
|
|
||||||
// Relevant PR: https://github.com/urfave/cli/pull/872
|
|
||||||
func TestVersionOneTwoOneRegression(t *testing.T) {
|
|
||||||
testData := []struct {
|
|
||||||
testCase string
|
|
||||||
appRunInput []string
|
|
||||||
skipArgReorder bool
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
testCase: "with_dash_dash",
|
|
||||||
appRunInput: []string{"cli", "command", "--flagone", "flagvalue", "--", "docker", "image", "ls", "--no-trunc"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
testCase: "with_dash_dash_and_skip_reorder",
|
|
||||||
appRunInput: []string{"cli", "command", "--flagone", "flagvalue", "--", "docker", "image", "ls", "--no-trunc"},
|
|
||||||
skipArgReorder: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
testCase: "without_dash_dash",
|
|
||||||
appRunInput: []string{"cli", "command", "--flagone", "flagvalue", "docker", "image", "ls", "--no-trunc"},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
testCase: "without_dash_dash_and_skip_reorder",
|
|
||||||
appRunInput: []string{"cli", "command", "--flagone", "flagvalue", "docker", "image", "ls", "--no-trunc"},
|
|
||||||
skipArgReorder: true,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for _, test := range testData {
|
|
||||||
t.Run(test.testCase, func(t *testing.T) {
|
|
||||||
// setup
|
|
||||||
app := NewApp()
|
|
||||||
app.Commands = []Command{{
|
|
||||||
Name: "command",
|
|
||||||
SkipArgReorder: test.skipArgReorder,
|
|
||||||
Flags: []Flag{
|
|
||||||
StringFlag{
|
|
||||||
Name: "flagone",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Action: func(c *Context) error { return nil },
|
|
||||||
}}
|
|
||||||
|
|
||||||
// logic under test
|
|
||||||
err := app.Run(test.appRunInput)
|
|
||||||
|
|
||||||
// assertions
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("did not expected an error, but there was one: %s", err)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
1500
app_test.go
1500
app_test.go
File diff suppressed because it is too large
Load Diff
@ -20,7 +20,8 @@ install:
|
|||||||
- go version
|
- go version
|
||||||
- go env
|
- go env
|
||||||
- go get github.com/urfave/gfmrun/cmd/gfmrun
|
- go get github.com/urfave/gfmrun/cmd/gfmrun
|
||||||
- go mod vendor
|
- go get golang.org/x/tools/cmd/goimports
|
||||||
|
- go mod tidy
|
||||||
|
|
||||||
build_script:
|
build_script:
|
||||||
- go run build.go vet
|
- go run build.go vet
|
||||||
|
54
args.go
Normal file
54
args.go
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
package cli
|
||||||
|
|
||||||
|
type Args interface {
|
||||||
|
// Get returns the nth argument, or else a blank string
|
||||||
|
Get(n int) string
|
||||||
|
// First returns the first argument, or else a blank string
|
||||||
|
First() string
|
||||||
|
// Tail returns the rest of the arguments (not the first one)
|
||||||
|
// or else an empty string slice
|
||||||
|
Tail() []string
|
||||||
|
// Len returns the length of the wrapped slice
|
||||||
|
Len() int
|
||||||
|
// Present checks if there are any arguments present
|
||||||
|
Present() bool
|
||||||
|
// Slice returns a copy of the internal slice
|
||||||
|
Slice() []string
|
||||||
|
}
|
||||||
|
|
||||||
|
type args []string
|
||||||
|
|
||||||
|
func (a *args) Get(n int) string {
|
||||||
|
if len(*a) > n {
|
||||||
|
return (*a)[n]
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *args) First() string {
|
||||||
|
return a.Get(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *args) Tail() []string {
|
||||||
|
if a.Len() >= 2 {
|
||||||
|
tail := []string((*a)[1:])
|
||||||
|
ret := make([]string, len(tail))
|
||||||
|
copy(ret, tail)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *args) Len() int {
|
||||||
|
return len(*a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *args) Present() bool {
|
||||||
|
return a.Len() != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *args) Slice() []string {
|
||||||
|
ret := make([]string, len(*a))
|
||||||
|
copy(ret, *a)
|
||||||
|
return ret
|
||||||
|
}
|
14
build.go
14
build.go
@ -12,7 +12,7 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
var packages = []string{"cli", "altsrc"}
|
var packages = []string{"cli", "altsrc"}
|
||||||
@ -24,19 +24,19 @@ func main() {
|
|||||||
app.Usage = "Generates a new urfave/cli build!"
|
app.Usage = "Generates a new urfave/cli build!"
|
||||||
|
|
||||||
app.Commands = cli.Commands{
|
app.Commands = cli.Commands{
|
||||||
cli.Command{
|
{
|
||||||
Name: "vet",
|
Name: "vet",
|
||||||
Action: VetActionFunc,
|
Action: VetActionFunc,
|
||||||
},
|
},
|
||||||
cli.Command{
|
{
|
||||||
Name: "test",
|
Name: "test",
|
||||||
Action: TestActionFunc,
|
Action: TestActionFunc,
|
||||||
},
|
},
|
||||||
cli.Command{
|
{
|
||||||
Name: "gfmrun",
|
Name: "gfmrun",
|
||||||
Action: GfmrunActionFunc,
|
Action: GfmrunActionFunc,
|
||||||
},
|
},
|
||||||
cli.Command{
|
{
|
||||||
Name: "toc",
|
Name: "toc",
|
||||||
Action: TocActionFunc,
|
Action: TocActionFunc,
|
||||||
},
|
},
|
||||||
@ -67,9 +67,9 @@ func TestActionFunc(c *cli.Context) error {
|
|||||||
var packageName string
|
var packageName string
|
||||||
|
|
||||||
if pkg == "cli" {
|
if pkg == "cli" {
|
||||||
packageName = "github.com/urfave/cli"
|
packageName = "github.com/urfave/cli/v2"
|
||||||
} else {
|
} else {
|
||||||
packageName = fmt.Sprintf("github.com/urfave/cli/%s", pkg)
|
packageName = fmt.Sprintf("github.com/urfave/cli/v2/%s", pkg)
|
||||||
}
|
}
|
||||||
|
|
||||||
coverProfile := fmt.Sprintf("--coverprofile=%s.coverprofile", pkg)
|
coverProfile := fmt.Sprintf("--coverprofile=%s.coverprofile", pkg)
|
||||||
|
82
category.go
82
category.go
@ -1,41 +1,75 @@
|
|||||||
package cli
|
package cli
|
||||||
|
|
||||||
// CommandCategories is a slice of *CommandCategory.
|
type CommandCategories interface {
|
||||||
type CommandCategories []*CommandCategory
|
// AddCommand adds a command to a category, creating a new category if necessary.
|
||||||
|
AddCommand(category string, command *Command)
|
||||||
// CommandCategory is a category containing commands.
|
// categories returns a copy of the category slice
|
||||||
type CommandCategory struct {
|
Categories() []CommandCategory
|
||||||
Name string
|
|
||||||
Commands Commands
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c CommandCategories) Less(i, j int) bool {
|
type commandCategories []*commandCategory
|
||||||
return lexicographicLess(c[i].Name, c[j].Name)
|
|
||||||
|
func newCommandCategories() CommandCategories {
|
||||||
|
ret := commandCategories([]*commandCategory{})
|
||||||
|
return &ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c CommandCategories) Len() int {
|
func (c *commandCategories) Less(i, j int) bool {
|
||||||
return len(c)
|
return lexicographicLess((*c)[i].Name(), (*c)[j].Name())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c CommandCategories) Swap(i, j int) {
|
func (c *commandCategories) Len() int {
|
||||||
c[i], c[j] = c[j], c[i]
|
return len(*c)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddCommand adds a command to a category.
|
func (c *commandCategories) Swap(i, j int) {
|
||||||
func (c CommandCategories) AddCommand(category string, command Command) CommandCategories {
|
(*c)[i], (*c)[j] = (*c)[j], (*c)[i]
|
||||||
for _, commandCategory := range c {
|
}
|
||||||
if commandCategory.Name == category {
|
|
||||||
commandCategory.Commands = append(commandCategory.Commands, command)
|
func (c *commandCategories) AddCommand(category string, command *Command) {
|
||||||
return c
|
for _, commandCategory := range []*commandCategory(*c) {
|
||||||
|
if commandCategory.name == category {
|
||||||
|
commandCategory.commands = append(commandCategory.commands, command)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return append(c, &CommandCategory{Name: category, Commands: []Command{command}})
|
newVal := append(*c,
|
||||||
|
&commandCategory{name: category, commands: []*Command{command}})
|
||||||
|
*c = newVal
|
||||||
}
|
}
|
||||||
|
|
||||||
// VisibleCommands returns a slice of the Commands with Hidden=false
|
func (c *commandCategories) Categories() []CommandCategory {
|
||||||
func (c *CommandCategory) VisibleCommands() []Command {
|
ret := make([]CommandCategory, len(*c))
|
||||||
ret := []Command{}
|
for i, cat := range *c {
|
||||||
for _, command := range c.Commands {
|
ret[i] = cat
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
// CommandCategory is a category containing commands.
|
||||||
|
type CommandCategory interface {
|
||||||
|
// Name returns the category name string
|
||||||
|
Name() string
|
||||||
|
// VisibleCommands returns a slice of the Commands with Hidden=false
|
||||||
|
VisibleCommands() []*Command
|
||||||
|
}
|
||||||
|
|
||||||
|
type commandCategory struct {
|
||||||
|
name string
|
||||||
|
commands []*Command
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *commandCategory) Name() string {
|
||||||
|
return c.name
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *commandCategory) VisibleCommands() []*Command {
|
||||||
|
if c.commands == nil {
|
||||||
|
c.commands = []*Command{}
|
||||||
|
}
|
||||||
|
|
||||||
|
var ret []*Command
|
||||||
|
for _, command := range c.commands {
|
||||||
if !command.Hidden {
|
if !command.Hidden {
|
||||||
ret = append(ret, command)
|
ret = append(ret, command)
|
||||||
}
|
}
|
||||||
|
17
cli.go
17
cli.go
@ -2,18 +2,19 @@
|
|||||||
// Go applications. cli is designed to be easy to understand and write, the most simple
|
// Go applications. cli is designed to be easy to understand and write, the most simple
|
||||||
// cli application can be written as follows:
|
// cli application can be written as follows:
|
||||||
// func main() {
|
// func main() {
|
||||||
// cli.NewApp().Run(os.Args)
|
// (&cli.App{}).Run(os.Args)
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// Of course this application does not do much, so let's make this an actual application:
|
// Of course this application does not do much, so let's make this an actual application:
|
||||||
// func main() {
|
// func main() {
|
||||||
// app := cli.NewApp()
|
// app := &cli.App{
|
||||||
// app.Name = "greet"
|
// Name: "greet",
|
||||||
// app.Usage = "say a greeting"
|
// Usage: "say a greeting",
|
||||||
// app.Action = func(c *cli.Context) error {
|
// Action: func(c *cli.Context) error {
|
||||||
// println("Greetings")
|
// fmt.Println("Greetings")
|
||||||
// return nil
|
// return nil
|
||||||
// }
|
// },
|
||||||
|
// }
|
||||||
//
|
//
|
||||||
// app.Run(os.Args)
|
// app.Run(os.Args)
|
||||||
// }
|
// }
|
||||||
|
198
command.go
198
command.go
@ -11,8 +11,6 @@ import (
|
|||||||
type Command struct {
|
type Command struct {
|
||||||
// The name of the command
|
// The name of the command
|
||||||
Name string
|
Name string
|
||||||
// short name of the command. Typically one character (deprecated, use `Aliases`)
|
|
||||||
ShortName string
|
|
||||||
// A list of aliases for the command
|
// A list of aliases for the command
|
||||||
Aliases []string
|
Aliases []string
|
||||||
// A short description of the usage of this command
|
// A short description of the usage of this command
|
||||||
@ -34,23 +32,15 @@ type Command struct {
|
|||||||
// It is run even if Action() panics
|
// It is run even if Action() panics
|
||||||
After AfterFunc
|
After AfterFunc
|
||||||
// The function to call when this command is invoked
|
// The function to call when this command is invoked
|
||||||
Action interface{}
|
Action ActionFunc
|
||||||
// TODO: replace `Action: interface{}` with `Action: ActionFunc` once some kind
|
|
||||||
// of deprecation period has passed, maybe?
|
|
||||||
|
|
||||||
// Execute this function if a usage error occurs.
|
// Execute this function if a usage error occurs.
|
||||||
OnUsageError OnUsageErrorFunc
|
OnUsageError OnUsageErrorFunc
|
||||||
// List of child commands
|
// List of child commands
|
||||||
Subcommands Commands
|
Subcommands []*Command
|
||||||
// List of flags to parse
|
// List of flags to parse
|
||||||
Flags []Flag
|
Flags []Flag
|
||||||
// Treat all flags as normal arguments if true
|
// Treat all flags as normal arguments if true
|
||||||
SkipFlagParsing bool
|
SkipFlagParsing bool
|
||||||
// Skip argument reordering which attempts to move flags before arguments,
|
|
||||||
// but only works if all flags appear after all arguments. This behavior was
|
|
||||||
// removed n version 2 since it only works under specific conditions so we
|
|
||||||
// backport here by exposing it as an option for compatibility.
|
|
||||||
SkipArgReorder bool
|
|
||||||
// Boolean to hide built-in help command
|
// Boolean to hide built-in help command
|
||||||
HideHelp bool
|
HideHelp bool
|
||||||
// Boolean to hide this command from help or completion
|
// Boolean to hide this command from help or completion
|
||||||
@ -70,7 +60,9 @@ type Command struct {
|
|||||||
CustomHelpTemplate string
|
CustomHelpTemplate string
|
||||||
}
|
}
|
||||||
|
|
||||||
type CommandsByName []Command
|
type Commands []*Command
|
||||||
|
|
||||||
|
type CommandsByName []*Command
|
||||||
|
|
||||||
func (c CommandsByName) Len() int {
|
func (c CommandsByName) Len() int {
|
||||||
return len(c)
|
return len(c)
|
||||||
@ -86,35 +78,29 @@ func (c CommandsByName) Swap(i, j int) {
|
|||||||
|
|
||||||
// FullName returns the full name of the command.
|
// FullName returns the full name of the command.
|
||||||
// For subcommands this ensures that parent commands are part of the command path
|
// For subcommands this ensures that parent commands are part of the command path
|
||||||
func (c Command) FullName() string {
|
func (c *Command) FullName() string {
|
||||||
if c.commandNamePath == nil {
|
if c.commandNamePath == nil {
|
||||||
return c.Name
|
return c.Name
|
||||||
}
|
}
|
||||||
return strings.Join(c.commandNamePath, " ")
|
return strings.Join(c.commandNamePath, " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Commands is a slice of Command
|
|
||||||
type Commands []Command
|
|
||||||
|
|
||||||
// Run invokes the command given the context, parses ctx.Args() to generate command-specific flags
|
// Run invokes the command given the context, parses ctx.Args() to generate command-specific flags
|
||||||
func (c Command) Run(ctx *Context) (err error) {
|
func (c *Command) Run(ctx *Context) (err error) {
|
||||||
if len(c.Subcommands) > 0 {
|
if len(c.Subcommands) > 0 {
|
||||||
return c.startApp(ctx)
|
return c.startApp(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !c.HideHelp && (HelpFlag != BoolFlag{}) {
|
if !c.HideHelp && HelpFlag != nil {
|
||||||
// append help to flags
|
// append help to flags
|
||||||
c.Flags = append(
|
c.appendFlag(HelpFlag)
|
||||||
c.Flags,
|
|
||||||
HelpFlag,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ctx.App.UseShortOptionHandling {
|
if ctx.App.UseShortOptionHandling {
|
||||||
c.UseShortOptionHandling = true
|
c.UseShortOptionHandling = true
|
||||||
}
|
}
|
||||||
|
|
||||||
set, err := c.parseFlags(ctx.Args().Tail())
|
set, err := c.parseFlags(ctx.Args())
|
||||||
|
|
||||||
context := NewContext(ctx.App, set, ctx)
|
context := NewContext(ctx.App, set, ctx)
|
||||||
context.Command = c
|
context.Command = c
|
||||||
@ -124,7 +110,7 @@ func (c Command) Run(ctx *Context) (err error) {
|
|||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if c.OnUsageError != nil {
|
if c.OnUsageError != nil {
|
||||||
err := c.OnUsageError(context, err, false)
|
err = c.OnUsageError(context, err, false)
|
||||||
context.App.handleExitCoder(context, err)
|
context.App.handleExitCoder(context, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -150,7 +136,7 @@ func (c Command) Run(ctx *Context) (err error) {
|
|||||||
if afterErr != nil {
|
if afterErr != nil {
|
||||||
context.App.handleExitCoder(context, err)
|
context.App.handleExitCoder(context, err)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = NewMultiError(err, afterErr)
|
err = newMultiError(err, afterErr)
|
||||||
} else {
|
} else {
|
||||||
err = afterErr
|
err = afterErr
|
||||||
}
|
}
|
||||||
@ -171,7 +157,8 @@ func (c Command) Run(ctx *Context) (err error) {
|
|||||||
c.Action = helpSubcommand.Action
|
c.Action = helpSubcommand.Action
|
||||||
}
|
}
|
||||||
|
|
||||||
err = HandleAction(c.Action, context)
|
context.Command = c
|
||||||
|
err = c.Action(context)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
context.App.handleExitCoder(context, err)
|
context.App.handleExitCoder(context, err)
|
||||||
@ -179,26 +166,25 @@ func (c Command) Run(ctx *Context) (err error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Command) newFlagSet() (*flag.FlagSet, error) {
|
||||||
|
return flagSet(c.Name, c.Flags)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Command) useShortOptionHandling() bool {
|
||||||
|
return c.UseShortOptionHandling
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Command) parseFlags(args Args) (*flag.FlagSet, error) {
|
func (c *Command) parseFlags(args Args) (*flag.FlagSet, error) {
|
||||||
if c.SkipFlagParsing {
|
|
||||||
set, err := c.newFlagSet()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return set, set.Parse(append([]string{"--"}, args...))
|
|
||||||
}
|
|
||||||
|
|
||||||
if !c.SkipArgReorder {
|
|
||||||
args = reorderArgs(c.Flags, args)
|
|
||||||
}
|
|
||||||
|
|
||||||
set, err := c.newFlagSet()
|
set, err := c.newFlagSet()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = parseIter(set, c, args)
|
if c.SkipFlagParsing {
|
||||||
|
return set, set.Parse(append([]string{"--"}, args.Tail()...))
|
||||||
|
}
|
||||||
|
|
||||||
|
err = parseIter(set, c, args.Tail())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -211,96 +197,13 @@ func (c *Command) parseFlags(args Args) (*flag.FlagSet, error) {
|
|||||||
return set, nil
|
return set, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Command) newFlagSet() (*flag.FlagSet, error) {
|
|
||||||
return flagSet(c.Name, c.Flags)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Command) useShortOptionHandling() bool {
|
|
||||||
return c.UseShortOptionHandling
|
|
||||||
}
|
|
||||||
|
|
||||||
// reorderArgs moves all flags (via reorderedArgs) before the rest of
|
|
||||||
// the arguments (remainingArgs) as this is what flag expects.
|
|
||||||
func reorderArgs(commandFlags []Flag, args []string) []string {
|
|
||||||
var remainingArgs, reorderedArgs []string
|
|
||||||
|
|
||||||
nextIndexMayContainValue := false
|
|
||||||
for i, arg := range args {
|
|
||||||
|
|
||||||
// dont reorder any args after a --
|
|
||||||
// read about -- here:
|
|
||||||
// https://unix.stackexchange.com/questions/11376/what-does-double-dash-mean-also-known-as-bare-double-dash
|
|
||||||
if arg == "--" {
|
|
||||||
remainingArgs = append(remainingArgs, args[i:]...)
|
|
||||||
break
|
|
||||||
|
|
||||||
// checks if this arg is a value that should be re-ordered next to its associated flag
|
|
||||||
} else if nextIndexMayContainValue && !strings.HasPrefix(arg, "-") {
|
|
||||||
nextIndexMayContainValue = false
|
|
||||||
reorderedArgs = append(reorderedArgs, arg)
|
|
||||||
|
|
||||||
// checks if this is an arg that should be re-ordered
|
|
||||||
} else if argIsFlag(commandFlags, arg) {
|
|
||||||
// we have determined that this is a flag that we should re-order
|
|
||||||
reorderedArgs = append(reorderedArgs, arg)
|
|
||||||
// if this arg does not contain a "=", then the next index may contain the value for this flag
|
|
||||||
nextIndexMayContainValue = !strings.Contains(arg, "=")
|
|
||||||
|
|
||||||
// simply append any remaining args
|
|
||||||
} else {
|
|
||||||
remainingArgs = append(remainingArgs, arg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return append(reorderedArgs, remainingArgs...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// argIsFlag checks if an arg is one of our command flags
|
|
||||||
func argIsFlag(commandFlags []Flag, arg string) bool {
|
|
||||||
// checks if this is just a `-`, and so definitely not a flag
|
|
||||||
if arg == "-" {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
// flags always start with a -
|
|
||||||
if !strings.HasPrefix(arg, "-") {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
// this line turns `--flag` into `flag`
|
|
||||||
if strings.HasPrefix(arg, "--") {
|
|
||||||
arg = strings.Replace(arg, "-", "", 2)
|
|
||||||
}
|
|
||||||
// this line turns `-flag` into `flag`
|
|
||||||
if strings.HasPrefix(arg, "-") {
|
|
||||||
arg = strings.Replace(arg, "-", "", 1)
|
|
||||||
}
|
|
||||||
// this line turns `flag=value` into `flag`
|
|
||||||
arg = strings.Split(arg, "=")[0]
|
|
||||||
// look through all the flags, to see if the `arg` is one of our flags
|
|
||||||
for _, flag := range commandFlags {
|
|
||||||
for _, key := range strings.Split(flag.GetName(), ",") {
|
|
||||||
key := strings.TrimSpace(key)
|
|
||||||
if key == arg {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// return false if this arg was not one of our flags
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Names returns the names including short names and aliases.
|
// Names returns the names including short names and aliases.
|
||||||
func (c Command) Names() []string {
|
func (c *Command) Names() []string {
|
||||||
names := []string{c.Name}
|
return append([]string{c.Name}, c.Aliases...)
|
||||||
|
|
||||||
if c.ShortName != "" {
|
|
||||||
names = append(names, c.ShortName)
|
|
||||||
}
|
|
||||||
|
|
||||||
return append(names, c.Aliases...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasName returns true if Command.Name or Command.ShortName matches given name
|
// HasName returns true if Command.Name matches given name
|
||||||
func (c Command) HasName(name string) bool {
|
func (c *Command) HasName(name string) bool {
|
||||||
for _, n := range c.Names() {
|
for _, n := range c.Names() {
|
||||||
if n == name {
|
if n == name {
|
||||||
return true
|
return true
|
||||||
@ -309,12 +212,12 @@ func (c Command) HasName(name string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Command) startApp(ctx *Context) error {
|
func (c *Command) startApp(ctx *Context) error {
|
||||||
app := NewApp()
|
app := &App{
|
||||||
app.Metadata = ctx.App.Metadata
|
Metadata: ctx.App.Metadata,
|
||||||
app.ExitErrHandler = ctx.App.ExitErrHandler
|
Name: fmt.Sprintf("%s %s", ctx.App.Name, c.Name),
|
||||||
// set the name and usage
|
}
|
||||||
app.Name = fmt.Sprintf("%s %s", ctx.App.Name, c.Name)
|
|
||||||
if c.HelpName == "" {
|
if c.HelpName == "" {
|
||||||
app.HelpName = c.HelpName
|
app.HelpName = c.HelpName
|
||||||
} else {
|
} else {
|
||||||
@ -337,18 +240,17 @@ func (c Command) startApp(ctx *Context) error {
|
|||||||
app.Version = ctx.App.Version
|
app.Version = ctx.App.Version
|
||||||
app.HideVersion = ctx.App.HideVersion
|
app.HideVersion = ctx.App.HideVersion
|
||||||
app.Compiled = ctx.App.Compiled
|
app.Compiled = ctx.App.Compiled
|
||||||
app.Author = ctx.App.Author
|
|
||||||
app.Email = ctx.App.Email
|
|
||||||
app.Writer = ctx.App.Writer
|
app.Writer = ctx.App.Writer
|
||||||
app.ErrWriter = ctx.App.ErrWriter
|
app.ErrWriter = ctx.App.ErrWriter
|
||||||
|
app.ExitErrHandler = ctx.App.ExitErrHandler
|
||||||
app.UseShortOptionHandling = ctx.App.UseShortOptionHandling
|
app.UseShortOptionHandling = ctx.App.UseShortOptionHandling
|
||||||
|
|
||||||
app.categories = CommandCategories{}
|
app.categories = newCommandCategories()
|
||||||
for _, command := range c.Subcommands {
|
for _, command := range c.Subcommands {
|
||||||
app.categories = app.categories.AddCommand(command.Category, command)
|
app.categories.AddCommand(command.Category, command)
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Sort(app.categories)
|
sort.Sort(app.categories.(*commandCategories))
|
||||||
|
|
||||||
// bash completion
|
// bash completion
|
||||||
app.EnableBashCompletion = ctx.App.EnableBashCompletion
|
app.EnableBashCompletion = ctx.App.EnableBashCompletion
|
||||||
@ -374,6 +276,22 @@ func (c Command) startApp(ctx *Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// VisibleFlags returns a slice of the Flags with Hidden=false
|
// VisibleFlags returns a slice of the Flags with Hidden=false
|
||||||
func (c Command) VisibleFlags() []Flag {
|
func (c *Command) VisibleFlags() []Flag {
|
||||||
return visibleFlags(c.Flags)
|
return visibleFlags(c.Flags)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Command) appendFlag(fl Flag) {
|
||||||
|
if !hasFlag(c.Flags, fl) {
|
||||||
|
c.Flags = append(c.Flags, fl)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasCommand(commands []*Command, command *Command) bool {
|
||||||
|
for _, existing := range commands {
|
||||||
|
if command == existing {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
322
command_test.go
322
command_test.go
@ -13,79 +13,69 @@ func TestCommandFlagParsing(t *testing.T) {
|
|||||||
cases := []struct {
|
cases := []struct {
|
||||||
testArgs []string
|
testArgs []string
|
||||||
skipFlagParsing bool
|
skipFlagParsing bool
|
||||||
skipArgReorder bool
|
useShortOptionHandling bool
|
||||||
expectedErr error
|
expectedErr error
|
||||||
UseShortOptionHandling bool
|
|
||||||
}{
|
}{
|
||||||
// Test normal "not ignoring flags" flow
|
// Test normal "not ignoring flags" flow
|
||||||
{[]string{"test-cmd", "blah", "blah", "-break"}, false, false, nil, false},
|
{testArgs: []string{"test-cmd", "-break", "blah", "blah"}, skipFlagParsing: false, useShortOptionHandling: false, expectedErr: errors.New("flag provided but not defined: -break")},
|
||||||
|
{testArgs: []string{"test-cmd", "blah", "blah"}, skipFlagParsing: true, useShortOptionHandling: false, expectedErr: nil}, // Test SkipFlagParsing without any args that look like flags
|
||||||
// Test no arg reorder
|
{testArgs: []string{"test-cmd", "blah", "-break"}, skipFlagParsing: true, useShortOptionHandling: false, expectedErr: nil}, // Test SkipFlagParsing with random flag arg
|
||||||
{[]string{"test-cmd", "blah", "blah", "-break"}, false, true, nil, false},
|
{testArgs: []string{"test-cmd", "blah", "-help"}, skipFlagParsing: true, useShortOptionHandling: false, expectedErr: nil}, // Test SkipFlagParsing with "special" help flag arg
|
||||||
{[]string{"test-cmd", "blah", "blah", "-break", "ls", "-l"}, false, true, nil, true},
|
{testArgs: []string{"test-cmd", "blah", "-h"}, skipFlagParsing: false, useShortOptionHandling: true, expectedErr: nil}, // Test UseShortOptionHandling
|
||||||
|
|
||||||
{[]string{"test-cmd", "blah", "blah"}, true, false, nil, false}, // Test SkipFlagParsing without any args that look like flags
|
|
||||||
{[]string{"test-cmd", "blah", "-break"}, true, false, nil, false}, // Test SkipFlagParsing with random flag arg
|
|
||||||
{[]string{"test-cmd", "blah", "-help"}, true, false, nil, false}, // Test SkipFlagParsing with "special" help flag arg
|
|
||||||
{[]string{"test-cmd", "blah"}, false, false, nil, true}, // Test UseShortOptionHandling
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
app := NewApp()
|
app := &App{Writer: ioutil.Discard}
|
||||||
app.Writer = ioutil.Discard
|
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
_ = set.Parse(c.testArgs)
|
_ = set.Parse(c.testArgs)
|
||||||
|
|
||||||
context := NewContext(app, set, nil)
|
context := NewContext(app, set, nil)
|
||||||
|
|
||||||
command := Command{
|
command := Command{
|
||||||
Name: "test-cmd",
|
Name: "test-cmd",
|
||||||
Aliases: []string{"tc"},
|
Aliases: []string{"tc"},
|
||||||
Usage: "this is for testing",
|
Usage: "this is for testing",
|
||||||
Description: "testing",
|
Description: "testing",
|
||||||
Action: func(_ *Context) error { return nil },
|
Action: func(_ *Context) error { return nil },
|
||||||
SkipFlagParsing: c.skipFlagParsing,
|
SkipFlagParsing: c.skipFlagParsing,
|
||||||
SkipArgReorder: c.skipArgReorder,
|
|
||||||
UseShortOptionHandling: c.UseShortOptionHandling,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err := command.Run(context)
|
err := command.Run(context)
|
||||||
|
|
||||||
expect(t, err, c.expectedErr)
|
expect(t, err, c.expectedErr)
|
||||||
expect(t, []string(context.Args()), c.testArgs)
|
expect(t, context.Args().Slice(), c.testArgs)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseAndRunShortOpts(t *testing.T) {
|
func TestParseAndRunShortOpts(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
testArgs []string
|
testArgs args
|
||||||
expectedErr error
|
expectedErr error
|
||||||
expectedArgs []string
|
expectedArgs Args
|
||||||
}{
|
}{
|
||||||
{[]string{"foo", "test", "-a"}, nil, []string{}},
|
{testArgs: args{"foo", "test", "-a"}, expectedErr: nil, expectedArgs: &args{}},
|
||||||
{[]string{"foo", "test", "-c", "arg1", "arg2"}, nil, []string{"arg1", "arg2"}},
|
{testArgs: args{"foo", "test", "-c", "arg1", "arg2"}, expectedErr: nil, expectedArgs: &args{"arg1", "arg2"}},
|
||||||
{[]string{"foo", "test", "-f"}, nil, []string{}},
|
{testArgs: args{"foo", "test", "-f"}, expectedErr: nil, expectedArgs: &args{}},
|
||||||
{[]string{"foo", "test", "-ac", "--fgh"}, nil, []string{}},
|
{testArgs: args{"foo", "test", "-ac", "--fgh"}, expectedErr: nil, expectedArgs: &args{}},
|
||||||
{[]string{"foo", "test", "-af"}, nil, []string{}},
|
{testArgs: args{"foo", "test", "-af"}, expectedErr: nil, expectedArgs: &args{}},
|
||||||
{[]string{"foo", "test", "-cf"}, nil, []string{}},
|
{testArgs: args{"foo", "test", "-cf"}, expectedErr: nil, expectedArgs: &args{}},
|
||||||
{[]string{"foo", "test", "-acf"}, nil, []string{}},
|
{testArgs: args{"foo", "test", "-acf"}, expectedErr: nil, expectedArgs: &args{}},
|
||||||
{[]string{"foo", "test", "--acf"}, errors.New("flag provided but not defined: -acf"), nil},
|
{testArgs: args{"foo", "test", "--acf"}, expectedErr: errors.New("flag provided but not defined: -acf"), expectedArgs: nil},
|
||||||
{[]string{"foo", "test", "-invalid"}, errors.New("flag provided but not defined: -invalid"), nil},
|
{testArgs: args{"foo", "test", "-invalid"}, expectedErr: errors.New("flag provided but not defined: -invalid"), expectedArgs: nil},
|
||||||
{[]string{"foo", "test", "-acf", "-invalid"}, errors.New("flag provided but not defined: -invalid"), nil},
|
{testArgs: args{"foo", "test", "-acf", "-invalid"}, expectedErr: errors.New("flag provided but not defined: -invalid"), expectedArgs: nil},
|
||||||
{[]string{"foo", "test", "--invalid"}, errors.New("flag provided but not defined: -invalid"), nil},
|
{testArgs: args{"foo", "test", "--invalid"}, expectedErr: errors.New("flag provided but not defined: -invalid"), expectedArgs: nil},
|
||||||
{[]string{"foo", "test", "-acf", "--invalid"}, errors.New("flag provided but not defined: -invalid"), nil},
|
{testArgs: args{"foo", "test", "-acf", "--invalid"}, expectedErr: errors.New("flag provided but not defined: -invalid"), expectedArgs: nil},
|
||||||
{[]string{"foo", "test", "-acf", "arg1", "-invalid"}, nil, []string{"arg1", "-invalid"}},
|
{testArgs: args{"foo", "test", "-acf", "arg1", "-invalid"}, expectedErr: nil, expectedArgs: &args{"arg1", "-invalid"}},
|
||||||
{[]string{"foo", "test", "-acf", "arg1", "--invalid"}, nil, []string{"arg1", "--invalid"}},
|
{testArgs: args{"foo", "test", "-acf", "arg1", "--invalid"}, expectedErr: nil, expectedArgs: &args{"arg1", "--invalid"}},
|
||||||
{[]string{"foo", "test", "-acfi", "not-arg", "arg1", "-invalid"}, nil, []string{"arg1", "-invalid"}},
|
{testArgs: args{"foo", "test", "-acfi", "not-arg", "arg1", "-invalid"}, expectedErr: nil, expectedArgs: &args{"arg1", "-invalid"}},
|
||||||
{[]string{"foo", "test", "-i", "ivalue"}, nil, []string{}},
|
{testArgs: args{"foo", "test", "-i", "ivalue"}, expectedErr: nil, expectedArgs: &args{}},
|
||||||
{[]string{"foo", "test", "-i", "ivalue", "arg1"}, nil, []string{"arg1"}},
|
{testArgs: args{"foo", "test", "-i", "ivalue", "arg1"}, expectedErr: nil, expectedArgs: &args{"arg1"}},
|
||||||
{[]string{"foo", "test", "-i"}, errors.New("flag needs an argument: -i"), nil},
|
{testArgs: args{"foo", "test", "-i"}, expectedErr: errors.New("flag needs an argument: -i"), expectedArgs: nil},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
var args []string
|
var args Args
|
||||||
cmd := Command{
|
cmd := &Command{
|
||||||
Name: "test",
|
Name: "test",
|
||||||
Usage: "this is for testing",
|
Usage: "this is for testing",
|
||||||
Description: "testing",
|
Description: "testing",
|
||||||
@ -93,18 +83,17 @@ func TestParseAndRunShortOpts(t *testing.T) {
|
|||||||
args = c.Args()
|
args = c.Args()
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
SkipArgReorder: true,
|
|
||||||
UseShortOptionHandling: true,
|
UseShortOptionHandling: true,
|
||||||
Flags: []Flag{
|
Flags: []Flag{
|
||||||
BoolFlag{Name: "abc, a"},
|
&BoolFlag{Name: "abc", Aliases: []string{"a"}},
|
||||||
BoolFlag{Name: "cde, c"},
|
&BoolFlag{Name: "cde", Aliases: []string{"c"}},
|
||||||
BoolFlag{Name: "fgh, f"},
|
&BoolFlag{Name: "fgh", Aliases: []string{"f"}},
|
||||||
StringFlag{Name: "ijk, i"},
|
&StringFlag{Name: "ijk", Aliases: []string{"i"}},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
app := NewApp()
|
app := NewApp()
|
||||||
app.Commands = []Command{cmd}
|
app.Commands = []*Command{cmd}
|
||||||
|
|
||||||
err := app.Run(c.testArgs)
|
err := app.Run(c.testArgs)
|
||||||
|
|
||||||
@ -114,15 +103,16 @@ func TestParseAndRunShortOpts(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCommand_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
|
func TestCommand_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
|
||||||
app := NewApp()
|
app := &App{
|
||||||
app.Commands = []Command{
|
Commands: []*Command{
|
||||||
{
|
{
|
||||||
Name: "bar",
|
Name: "bar",
|
||||||
Before: func(c *Context) error {
|
Before: func(c *Context) error {
|
||||||
return fmt.Errorf("before error")
|
return fmt.Errorf("before error")
|
||||||
},
|
},
|
||||||
After: func(c *Context) error {
|
After: func(c *Context) error {
|
||||||
return fmt.Errorf("after error")
|
return fmt.Errorf("after error")
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -144,29 +134,30 @@ func TestCommand_Run_BeforeSavesMetadata(t *testing.T) {
|
|||||||
var receivedMsgFromAction string
|
var receivedMsgFromAction string
|
||||||
var receivedMsgFromAfter string
|
var receivedMsgFromAfter string
|
||||||
|
|
||||||
app := NewApp()
|
app := &App{
|
||||||
app.Commands = []Command{
|
Commands: []*Command{
|
||||||
{
|
{
|
||||||
Name: "bar",
|
Name: "bar",
|
||||||
Before: func(c *Context) error {
|
Before: func(c *Context) error {
|
||||||
c.App.Metadata["msg"] = "hello world"
|
c.App.Metadata["msg"] = "hello world"
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
Action: func(c *Context) error {
|
Action: func(c *Context) error {
|
||||||
msg, ok := c.App.Metadata["msg"]
|
msg, ok := c.App.Metadata["msg"]
|
||||||
if !ok {
|
if !ok {
|
||||||
return errors.New("msg not found")
|
return errors.New("msg not found")
|
||||||
}
|
}
|
||||||
receivedMsgFromAction = msg.(string)
|
receivedMsgFromAction = msg.(string)
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
After: func(c *Context) error {
|
After: func(c *Context) error {
|
||||||
msg, ok := c.App.Metadata["msg"]
|
msg, ok := c.App.Metadata["msg"]
|
||||||
if !ok {
|
if !ok {
|
||||||
return errors.New("msg not found")
|
return errors.New("msg not found")
|
||||||
}
|
}
|
||||||
receivedMsgFromAfter = msg.(string)
|
receivedMsgFromAfter = msg.(string)
|
||||||
return nil
|
return nil
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -189,15 +180,16 @@ func TestCommand_Run_BeforeSavesMetadata(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCommand_OnUsageError_hasCommandContext(t *testing.T) {
|
func TestCommand_OnUsageError_hasCommandContext(t *testing.T) {
|
||||||
app := NewApp()
|
app := &App{
|
||||||
app.Commands = []Command{
|
Commands: []*Command{
|
||||||
{
|
{
|
||||||
Name: "bar",
|
Name: "bar",
|
||||||
Flags: []Flag{
|
Flags: []Flag{
|
||||||
IntFlag{Name: "flag"},
|
&IntFlag{Name: "flag"},
|
||||||
},
|
},
|
||||||
OnUsageError: func(c *Context, err error, _ bool) error {
|
OnUsageError: func(c *Context, err error, _ bool) error {
|
||||||
return fmt.Errorf("intercepted in %s: %s", c.Command.Name, err.Error())
|
return fmt.Errorf("intercepted in %s: %s", c.Command.Name, err.Error())
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -213,18 +205,19 @@ func TestCommand_OnUsageError_hasCommandContext(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCommand_OnUsageError_WithWrongFlagValue(t *testing.T) {
|
func TestCommand_OnUsageError_WithWrongFlagValue(t *testing.T) {
|
||||||
app := NewApp()
|
app := &App{
|
||||||
app.Commands = []Command{
|
Commands: []*Command{
|
||||||
{
|
{
|
||||||
Name: "bar",
|
Name: "bar",
|
||||||
Flags: []Flag{
|
Flags: []Flag{
|
||||||
IntFlag{Name: "flag"},
|
&IntFlag{Name: "flag"},
|
||||||
},
|
},
|
||||||
OnUsageError: func(c *Context, err error, _ bool) error {
|
OnUsageError: func(c *Context, err error, _ bool) error {
|
||||||
if !strings.HasPrefix(err.Error(), "invalid value \"wrong\"") {
|
if !strings.HasPrefix(err.Error(), "invalid value \"wrong\"") {
|
||||||
t.Errorf("Expect an invalid value error, but got \"%v\"", err)
|
t.Errorf("Expect an invalid value error, but got \"%v\"", err)
|
||||||
}
|
}
|
||||||
return errors.New("intercepted: " + err.Error())
|
return errors.New("intercepted: " + err.Error())
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -240,23 +233,24 @@ func TestCommand_OnUsageError_WithWrongFlagValue(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCommand_OnUsageError_WithSubcommand(t *testing.T) {
|
func TestCommand_OnUsageError_WithSubcommand(t *testing.T) {
|
||||||
app := NewApp()
|
app := &App{
|
||||||
app.Commands = []Command{
|
Commands: []*Command{
|
||||||
{
|
{
|
||||||
Name: "bar",
|
Name: "bar",
|
||||||
Subcommands: []Command{
|
Subcommands: []*Command{
|
||||||
{
|
{
|
||||||
Name: "baz",
|
Name: "baz",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Flags: []Flag{
|
||||||
|
&IntFlag{Name: "flag"},
|
||||||
|
},
|
||||||
|
OnUsageError: func(c *Context, err error, _ bool) error {
|
||||||
|
if !strings.HasPrefix(err.Error(), "invalid value \"wrong\"") {
|
||||||
|
t.Errorf("Expect an invalid value error, but got \"%v\"", err)
|
||||||
|
}
|
||||||
|
return errors.New("intercepted: " + err.Error())
|
||||||
},
|
},
|
||||||
},
|
|
||||||
Flags: []Flag{
|
|
||||||
IntFlag{Name: "flag"},
|
|
||||||
},
|
|
||||||
OnUsageError: func(c *Context, err error, _ bool) error {
|
|
||||||
if !strings.HasPrefix(err.Error(), "invalid value \"wrong\"") {
|
|
||||||
t.Errorf("Expect an invalid value error, but got \"%v\"", err)
|
|
||||||
}
|
|
||||||
return errors.New("intercepted: " + err.Error())
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -272,22 +266,23 @@ func TestCommand_OnUsageError_WithSubcommand(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCommand_Run_SubcommandsCanUseErrWriter(t *testing.T) {
|
func TestCommand_Run_SubcommandsCanUseErrWriter(t *testing.T) {
|
||||||
app := NewApp()
|
app := &App{
|
||||||
app.ErrWriter = ioutil.Discard
|
ErrWriter: ioutil.Discard,
|
||||||
app.Commands = []Command{
|
Commands: []*Command{
|
||||||
{
|
{
|
||||||
Name: "bar",
|
Name: "bar",
|
||||||
Usage: "this is for testing",
|
Usage: "this is for testing",
|
||||||
Subcommands: []Command{
|
Subcommands: []*Command{
|
||||||
{
|
{
|
||||||
Name: "baz",
|
Name: "baz",
|
||||||
Usage: "this is for testing",
|
Usage: "this is for testing",
|
||||||
Action: func(c *Context) error {
|
Action: func(c *Context) error {
|
||||||
if c.App.ErrWriter != ioutil.Discard {
|
if c.App.ErrWriter != ioutil.Discard {
|
||||||
return fmt.Errorf("ErrWriter not passed")
|
return fmt.Errorf("ErrWriter not passed")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -300,67 +295,30 @@ func TestCommand_Run_SubcommandsCanUseErrWriter(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCommandFlagReordering(t *testing.T) {
|
|
||||||
cases := []struct {
|
|
||||||
testArgs []string
|
|
||||||
expectedValue string
|
|
||||||
expectedArgs []string
|
|
||||||
expectedErr error
|
|
||||||
}{
|
|
||||||
{[]string{"some-exec", "some-command", "some-arg", "--flag", "foo"}, "foo", []string{"some-arg"}, nil},
|
|
||||||
{[]string{"some-exec", "some-command", "some-arg", "--flag=foo"}, "foo", []string{"some-arg"}, nil},
|
|
||||||
{[]string{"some-exec", "some-command", "--flag=foo", "some-arg"}, "foo", []string{"some-arg"}, nil},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, c := range cases {
|
|
||||||
value := ""
|
|
||||||
var args []string
|
|
||||||
app := &App{
|
|
||||||
Commands: []Command{
|
|
||||||
{
|
|
||||||
Name: "some-command",
|
|
||||||
Flags: []Flag{
|
|
||||||
StringFlag{Name: "flag"},
|
|
||||||
},
|
|
||||||
Action: func(c *Context) {
|
|
||||||
fmt.Printf("%+v\n", c.String("flag"))
|
|
||||||
value = c.String("flag")
|
|
||||||
args = c.Args()
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
err := app.Run(c.testArgs)
|
|
||||||
expect(t, err, c.expectedErr)
|
|
||||||
expect(t, value, c.expectedValue)
|
|
||||||
expect(t, args, c.expectedArgs)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestCommandSkipFlagParsing(t *testing.T) {
|
func TestCommandSkipFlagParsing(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
testArgs []string
|
testArgs args
|
||||||
expectedArgs []string
|
expectedArgs *args
|
||||||
expectedErr error
|
expectedErr error
|
||||||
}{
|
}{
|
||||||
{[]string{"some-exec", "some-command", "some-arg", "--flag", "foo"}, []string{"some-arg", "--flag", "foo"}, nil},
|
{testArgs: args{"some-exec", "some-command", "some-arg", "--flag", "foo"}, expectedArgs: &args{"some-arg", "--flag", "foo"}, expectedErr: nil},
|
||||||
{[]string{"some-exec", "some-command", "some-arg", "--flag=foo"}, []string{"some-arg", "--flag=foo"}, nil},
|
{testArgs: args{"some-exec", "some-command", "some-arg", "--flag=foo"}, expectedArgs: &args{"some-arg", "--flag=foo"}, expectedErr: nil},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
var args []string
|
var args Args
|
||||||
app := &App{
|
app := &App{
|
||||||
Commands: []Command{
|
Commands: []*Command{
|
||||||
{
|
{
|
||||||
SkipFlagParsing: true,
|
SkipFlagParsing: true,
|
||||||
Name: "some-command",
|
Name: "some-command",
|
||||||
Flags: []Flag{
|
Flags: []Flag{
|
||||||
StringFlag{Name: "flag"},
|
&StringFlag{Name: "flag"},
|
||||||
},
|
},
|
||||||
Action: func(c *Context) {
|
Action: func(c *Context) error {
|
||||||
fmt.Printf("%+v\n", c.String("flag"))
|
fmt.Printf("%+v\n", c.String("flag"))
|
||||||
args = c.Args()
|
args = c.Args()
|
||||||
|
return nil
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
280
context.go
280
context.go
@ -1,36 +1,51 @@
|
|||||||
package cli
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Context is a type that is passed through to
|
// Context is a type that is passed through to
|
||||||
// each Handler action in a cli application. Context
|
// each Handler action in a cli application. Context
|
||||||
// can be used to retrieve context-specific Args and
|
// can be used to retrieve context-specific args and
|
||||||
// parsed command-line options.
|
// parsed command-line options.
|
||||||
type Context struct {
|
type Context struct {
|
||||||
|
context.Context
|
||||||
App *App
|
App *App
|
||||||
Command Command
|
Command *Command
|
||||||
shellComplete bool
|
shellComplete bool
|
||||||
flagSet *flag.FlagSet
|
|
||||||
setFlags map[string]bool
|
setFlags map[string]bool
|
||||||
|
flagSet *flag.FlagSet
|
||||||
parentContext *Context
|
parentContext *Context
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewContext creates a new context. For use in when invoking an App or Command action.
|
// NewContext creates a new context. For use in when invoking an App or Command action.
|
||||||
func NewContext(app *App, set *flag.FlagSet, parentCtx *Context) *Context {
|
func NewContext(app *App, set *flag.FlagSet, parentCtx *Context) *Context {
|
||||||
c := &Context{App: app, flagSet: set, parentContext: parentCtx}
|
c := &Context{App: app, flagSet: set, parentContext: parentCtx}
|
||||||
|
|
||||||
if parentCtx != nil {
|
if parentCtx != nil {
|
||||||
|
c.Context = parentCtx.Context
|
||||||
c.shellComplete = parentCtx.shellComplete
|
c.shellComplete = parentCtx.shellComplete
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c.Command = &Command{}
|
||||||
|
|
||||||
|
if c.Context == nil {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
go func() {
|
||||||
|
defer cancel()
|
||||||
|
sigs := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
||||||
|
<-sigs
|
||||||
|
}()
|
||||||
|
c.Context = ctx
|
||||||
|
}
|
||||||
|
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,214 +56,122 @@ func (c *Context) NumFlags() int {
|
|||||||
|
|
||||||
// Set sets a context flag to a value.
|
// Set sets a context flag to a value.
|
||||||
func (c *Context) Set(name, value string) error {
|
func (c *Context) Set(name, value string) error {
|
||||||
c.setFlags = nil
|
|
||||||
return c.flagSet.Set(name, value)
|
return c.flagSet.Set(name, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GlobalSet sets a context flag to a value on the global flagset
|
|
||||||
func (c *Context) GlobalSet(name, value string) error {
|
|
||||||
globalContext(c).setFlags = nil
|
|
||||||
return globalContext(c).flagSet.Set(name, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsSet determines if the flag was actually set
|
// IsSet determines if the flag was actually set
|
||||||
func (c *Context) IsSet(name string) bool {
|
func (c *Context) IsSet(name string) bool {
|
||||||
if c.setFlags == nil {
|
if fs := lookupFlagSet(name, c); fs != nil {
|
||||||
c.setFlags = make(map[string]bool)
|
if fs := lookupFlagSet(name, c); fs != nil {
|
||||||
|
isSet := false
|
||||||
c.flagSet.Visit(func(f *flag.Flag) {
|
fs.Visit(func(f *flag.Flag) {
|
||||||
c.setFlags[f.Name] = true
|
if f.Name == name {
|
||||||
})
|
isSet = true
|
||||||
|
|
||||||
c.flagSet.VisitAll(func(f *flag.Flag) {
|
|
||||||
if _, ok := c.setFlags[f.Name]; ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
c.setFlags[f.Name] = false
|
|
||||||
})
|
|
||||||
|
|
||||||
// XXX hack to support IsSet for flags with EnvVar
|
|
||||||
//
|
|
||||||
// There isn't an easy way to do this with the current implementation since
|
|
||||||
// whether a flag was set via an environment variable is very difficult to
|
|
||||||
// determine here. Instead, we intend to introduce a backwards incompatible
|
|
||||||
// change in version 2 to add `IsSet` to the Flag interface to push the
|
|
||||||
// responsibility closer to where the information required to determine
|
|
||||||
// whether a flag is set by non-standard means such as environment
|
|
||||||
// variables is available.
|
|
||||||
//
|
|
||||||
// See https://github.com/urfave/cli/issues/294 for additional discussion
|
|
||||||
flags := c.Command.Flags
|
|
||||||
if c.Command.Name == "" { // cannot == Command{} since it contains slice types
|
|
||||||
if c.App != nil {
|
|
||||||
flags = c.App.Flags
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for _, f := range flags {
|
|
||||||
eachName(f.GetName(), func(name string) {
|
|
||||||
if isSet, ok := c.setFlags[name]; isSet || !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
val := reflect.ValueOf(f)
|
|
||||||
if val.Kind() == reflect.Ptr {
|
|
||||||
val = val.Elem()
|
|
||||||
}
|
|
||||||
|
|
||||||
filePathValue := val.FieldByName("FilePath")
|
|
||||||
if filePathValue.IsValid() {
|
|
||||||
eachName(filePathValue.String(), func(filePath string) {
|
|
||||||
if _, err := os.Stat(filePath); err == nil {
|
|
||||||
c.setFlags[name] = true
|
|
||||||
return
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
envVarValue := val.FieldByName("EnvVar")
|
|
||||||
if envVarValue.IsValid() {
|
|
||||||
eachName(envVarValue.String(), func(envVar string) {
|
|
||||||
envVar = strings.TrimSpace(envVar)
|
|
||||||
if _, ok := syscall.Getenv(envVar); ok {
|
|
||||||
c.setFlags[name] = true
|
|
||||||
return
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
if isSet {
|
||||||
|
return true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return c.setFlags[name]
|
f := lookupFlag(name, c)
|
||||||
}
|
if f == nil {
|
||||||
|
return false
|
||||||
// GlobalIsSet determines if the global flag was actually set
|
|
||||||
func (c *Context) GlobalIsSet(name string) bool {
|
|
||||||
ctx := c
|
|
||||||
if ctx.parentContext != nil {
|
|
||||||
ctx = ctx.parentContext
|
|
||||||
}
|
|
||||||
|
|
||||||
for ; ctx != nil; ctx = ctx.parentContext {
|
|
||||||
if ctx.IsSet(name) {
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return f.IsSet()
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// FlagNames returns a slice of flag names used in this context.
|
// LocalFlagNames returns a slice of flag names used in this context.
|
||||||
func (c *Context) FlagNames() (names []string) {
|
func (c *Context) LocalFlagNames() []string {
|
||||||
for _, f := range c.Command.Flags {
|
var names []string
|
||||||
name := strings.Split(f.GetName(), ",")[0]
|
c.flagSet.Visit(makeFlagNameVisitor(&names))
|
||||||
if name == "help" {
|
return names
|
||||||
continue
|
}
|
||||||
}
|
|
||||||
names = append(names, name)
|
// FlagNames returns a slice of flag names used by the this context and all of
|
||||||
|
// its parent contexts.
|
||||||
|
func (c *Context) FlagNames() []string {
|
||||||
|
var names []string
|
||||||
|
for _, ctx := range c.Lineage() {
|
||||||
|
ctx.flagSet.Visit(makeFlagNameVisitor(&names))
|
||||||
}
|
}
|
||||||
return
|
return names
|
||||||
}
|
}
|
||||||
|
|
||||||
// GlobalFlagNames returns a slice of global flag names used by the app.
|
// Lineage returns *this* context and all of its ancestor contexts in order from
|
||||||
func (c *Context) GlobalFlagNames() (names []string) {
|
// child to parent
|
||||||
for _, f := range c.App.Flags {
|
func (c *Context) Lineage() []*Context {
|
||||||
name := strings.Split(f.GetName(), ",")[0]
|
var lineage []*Context
|
||||||
if name == "help" || name == "version" {
|
|
||||||
continue
|
for cur := c; cur != nil; cur = cur.parentContext {
|
||||||
}
|
lineage = append(lineage, cur)
|
||||||
names = append(names, name)
|
|
||||||
}
|
}
|
||||||
return
|
|
||||||
|
return lineage
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parent returns the parent context, if any
|
// value returns the value of the flag corresponding to `name`
|
||||||
func (c *Context) Parent() *Context {
|
|
||||||
return c.parentContext
|
|
||||||
}
|
|
||||||
|
|
||||||
// value returns the value of the flag coressponding to `name`
|
|
||||||
func (c *Context) value(name string) interface{} {
|
func (c *Context) value(name string) interface{} {
|
||||||
return c.flagSet.Lookup(name).Value.(flag.Getter).Get()
|
return c.flagSet.Lookup(name).Value.(flag.Getter).Get()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Args contains apps console arguments
|
|
||||||
type Args []string
|
|
||||||
|
|
||||||
// Args returns the command line arguments associated with the context.
|
// Args returns the command line arguments associated with the context.
|
||||||
func (c *Context) Args() Args {
|
func (c *Context) Args() Args {
|
||||||
args := Args(c.flagSet.Args())
|
ret := args(c.flagSet.Args())
|
||||||
return args
|
return &ret
|
||||||
}
|
}
|
||||||
|
|
||||||
// NArg returns the number of the command line arguments.
|
// NArg returns the number of the command line arguments.
|
||||||
func (c *Context) NArg() int {
|
func (c *Context) NArg() int {
|
||||||
return len(c.Args())
|
return c.Args().Len()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the nth argument, or else a blank string
|
func lookupFlag(name string, ctx *Context) Flag {
|
||||||
func (a Args) Get(n int) string {
|
for _, c := range ctx.Lineage() {
|
||||||
if len(a) > n {
|
if c.Command == nil {
|
||||||
return a[n]
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, f := range c.Command.Flags {
|
||||||
|
for _, n := range f.Names() {
|
||||||
|
if n == name {
|
||||||
|
return f
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// First returns the first argument, or else a blank string
|
if ctx.App != nil {
|
||||||
func (a Args) First() string {
|
for _, f := range ctx.App.Flags {
|
||||||
return a.Get(0)
|
for _, n := range f.Names() {
|
||||||
}
|
if n == name {
|
||||||
|
return f
|
||||||
// Tail returns the rest of the arguments (not the first one)
|
}
|
||||||
// or else an empty string slice
|
}
|
||||||
func (a Args) Tail() []string {
|
}
|
||||||
if len(a) >= 2 {
|
|
||||||
return []string(a)[1:]
|
|
||||||
}
|
}
|
||||||
return []string{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Present checks if there are any arguments present
|
|
||||||
func (a Args) Present() bool {
|
|
||||||
return len(a) != 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// Swap swaps arguments at the given indexes
|
|
||||||
func (a Args) Swap(from, to int) error {
|
|
||||||
if from >= len(a) || to >= len(a) {
|
|
||||||
return errors.New("index out of range")
|
|
||||||
}
|
|
||||||
a[from], a[to] = a[to], a[from]
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func globalContext(ctx *Context) *Context {
|
func lookupFlagSet(name string, ctx *Context) *flag.FlagSet {
|
||||||
if ctx == nil {
|
for _, c := range ctx.Lineage() {
|
||||||
return nil
|
if f := c.flagSet.Lookup(name); f != nil {
|
||||||
}
|
return c.flagSet
|
||||||
|
|
||||||
for {
|
|
||||||
if ctx.parentContext == nil {
|
|
||||||
return ctx
|
|
||||||
}
|
|
||||||
ctx = ctx.parentContext
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func lookupGlobalFlagSet(name string, ctx *Context) *flag.FlagSet {
|
|
||||||
if ctx.parentContext != nil {
|
|
||||||
ctx = ctx.parentContext
|
|
||||||
}
|
|
||||||
for ; ctx != nil; ctx = ctx.parentContext {
|
|
||||||
if f := ctx.flagSet.Lookup(name); f != nil {
|
|
||||||
return ctx.flagSet
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func copyFlag(name string, ff *flag.Flag, set *flag.FlagSet) {
|
func copyFlag(name string, ff *flag.Flag, set *flag.FlagSet) {
|
||||||
switch ff.Value.(type) {
|
switch ff.Value.(type) {
|
||||||
case *StringSlice:
|
case Serializer:
|
||||||
|
_ = set.Set(name, ff.Value.(Serializer).Serialize())
|
||||||
default:
|
default:
|
||||||
_ = set.Set(name, ff.Value.String())
|
_ = set.Set(name, ff.Value.String())
|
||||||
}
|
}
|
||||||
@ -260,7 +183,7 @@ func normalizeFlags(flags []Flag, set *flag.FlagSet) error {
|
|||||||
visited[f.Name] = true
|
visited[f.Name] = true
|
||||||
})
|
})
|
||||||
for _, f := range flags {
|
for _, f := range flags {
|
||||||
parts := strings.Split(f.GetName(), ",")
|
parts := f.Names()
|
||||||
if len(parts) == 1 {
|
if len(parts) == 1 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -287,6 +210,24 @@ func normalizeFlags(flags []Flag, set *flag.FlagSet) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func makeFlagNameVisitor(names *[]string) func(*flag.Flag) {
|
||||||
|
return func(f *flag.Flag) {
|
||||||
|
nameParts := strings.Split(f.Name, ",")
|
||||||
|
name := strings.TrimSpace(nameParts[0])
|
||||||
|
|
||||||
|
for _, part := range nameParts {
|
||||||
|
part = strings.TrimSpace(part)
|
||||||
|
if len(part) > len(name) {
|
||||||
|
name = part
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if name != "" {
|
||||||
|
*names = append(*names, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type requiredFlagsErr interface {
|
type requiredFlagsErr interface {
|
||||||
error
|
error
|
||||||
getMissingFlags() []string
|
getMissingFlags() []string
|
||||||
@ -315,7 +256,8 @@ func checkRequiredFlags(flags []Flag, context *Context) requiredFlagsErr {
|
|||||||
if rf, ok := f.(RequiredFlag); ok && rf.IsRequired() {
|
if rf, ok := f.(RequiredFlag); ok && rf.IsRequired() {
|
||||||
var flagPresent bool
|
var flagPresent bool
|
||||||
var flagName string
|
var flagName string
|
||||||
for _, key := range strings.Split(f.GetName(), ",") {
|
|
||||||
|
for _, key := range f.Names() {
|
||||||
if len(key) > 1 {
|
if len(key) > 1 {
|
||||||
flagName = key
|
flagName = key
|
||||||
}
|
}
|
||||||
|
486
context_test.go
486
context_test.go
@ -1,7 +1,9 @@
|
|||||||
package cli
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"flag"
|
"flag"
|
||||||
|
"sort"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@ -22,7 +24,7 @@ func TestNewContext(t *testing.T) {
|
|||||||
globalSet.Uint64("myflagUint64", uint64(33), "doc")
|
globalSet.Uint64("myflagUint64", uint64(33), "doc")
|
||||||
globalSet.Float64("myflag64", float64(47), "doc")
|
globalSet.Float64("myflag64", float64(47), "doc")
|
||||||
globalCtx := NewContext(nil, globalSet, nil)
|
globalCtx := NewContext(nil, globalSet, nil)
|
||||||
command := Command{Name: "mycommand"}
|
command := &Command{Name: "mycommand"}
|
||||||
c := NewContext(nil, set, globalCtx)
|
c := NewContext(nil, set, globalCtx)
|
||||||
c.Command = command
|
c.Command = command
|
||||||
expect(t, c.Int("myflag"), 12)
|
expect(t, c.Int("myflag"), 12)
|
||||||
@ -30,123 +32,108 @@ func TestNewContext(t *testing.T) {
|
|||||||
expect(t, c.Uint("myflagUint"), uint(93))
|
expect(t, c.Uint("myflagUint"), uint(93))
|
||||||
expect(t, c.Uint64("myflagUint64"), uint64(93))
|
expect(t, c.Uint64("myflagUint64"), uint64(93))
|
||||||
expect(t, c.Float64("myflag64"), float64(17))
|
expect(t, c.Float64("myflag64"), float64(17))
|
||||||
expect(t, c.GlobalInt("myflag"), 42)
|
|
||||||
expect(t, c.GlobalInt64("myflagInt64"), int64(42))
|
|
||||||
expect(t, c.GlobalUint("myflagUint"), uint(33))
|
|
||||||
expect(t, c.GlobalUint64("myflagUint64"), uint64(33))
|
|
||||||
expect(t, c.GlobalFloat64("myflag64"), float64(47))
|
|
||||||
expect(t, c.Command.Name, "mycommand")
|
expect(t, c.Command.Name, "mycommand")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContext_Int(t *testing.T) {
|
func TestContext_Int(t *testing.T) {
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
set.Int("myflag", 12, "doc")
|
set.Int("myflag", 12, "doc")
|
||||||
c := NewContext(nil, set, nil)
|
parentSet := flag.NewFlagSet("test", 0)
|
||||||
|
parentSet.Int("top-flag", 13, "doc")
|
||||||
|
parentCtx := NewContext(nil, parentSet, nil)
|
||||||
|
c := NewContext(nil, set, parentCtx)
|
||||||
expect(t, c.Int("myflag"), 12)
|
expect(t, c.Int("myflag"), 12)
|
||||||
|
expect(t, c.Int("top-flag"), 13)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContext_Int64(t *testing.T) {
|
func TestContext_Int64(t *testing.T) {
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
set.Int64("myflagInt64", 12, "doc")
|
set.Int64("myflagInt64", 12, "doc")
|
||||||
c := NewContext(nil, set, nil)
|
parentSet := flag.NewFlagSet("test", 0)
|
||||||
|
parentSet.Int64("top-flag", 13, "doc")
|
||||||
|
parentCtx := NewContext(nil, parentSet, nil)
|
||||||
|
c := NewContext(nil, set, parentCtx)
|
||||||
expect(t, c.Int64("myflagInt64"), int64(12))
|
expect(t, c.Int64("myflagInt64"), int64(12))
|
||||||
|
expect(t, c.Int64("top-flag"), int64(13))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContext_Uint(t *testing.T) {
|
func TestContext_Uint(t *testing.T) {
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
set.Uint("myflagUint", uint(13), "doc")
|
set.Uint("myflagUint", uint(13), "doc")
|
||||||
c := NewContext(nil, set, nil)
|
parentSet := flag.NewFlagSet("test", 0)
|
||||||
|
parentSet.Uint("top-flag", uint(14), "doc")
|
||||||
|
parentCtx := NewContext(nil, parentSet, nil)
|
||||||
|
c := NewContext(nil, set, parentCtx)
|
||||||
expect(t, c.Uint("myflagUint"), uint(13))
|
expect(t, c.Uint("myflagUint"), uint(13))
|
||||||
|
expect(t, c.Uint("top-flag"), uint(14))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContext_Uint64(t *testing.T) {
|
func TestContext_Uint64(t *testing.T) {
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
set.Uint64("myflagUint64", uint64(9), "doc")
|
set.Uint64("myflagUint64", uint64(9), "doc")
|
||||||
c := NewContext(nil, set, nil)
|
parentSet := flag.NewFlagSet("test", 0)
|
||||||
|
parentSet.Uint64("top-flag", uint64(10), "doc")
|
||||||
|
parentCtx := NewContext(nil, parentSet, nil)
|
||||||
|
c := NewContext(nil, set, parentCtx)
|
||||||
expect(t, c.Uint64("myflagUint64"), uint64(9))
|
expect(t, c.Uint64("myflagUint64"), uint64(9))
|
||||||
}
|
expect(t, c.Uint64("top-flag"), uint64(10))
|
||||||
|
|
||||||
func TestContext_GlobalInt(t *testing.T) {
|
|
||||||
set := flag.NewFlagSet("test", 0)
|
|
||||||
set.Int("myflag", 12, "doc")
|
|
||||||
c := NewContext(nil, set, nil)
|
|
||||||
expect(t, c.GlobalInt("myflag"), 12)
|
|
||||||
expect(t, c.GlobalInt("nope"), 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContext_GlobalInt64(t *testing.T) {
|
|
||||||
set := flag.NewFlagSet("test", 0)
|
|
||||||
set.Int64("myflagInt64", 12, "doc")
|
|
||||||
c := NewContext(nil, set, nil)
|
|
||||||
expect(t, c.GlobalInt64("myflagInt64"), int64(12))
|
|
||||||
expect(t, c.GlobalInt64("nope"), int64(0))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContext_Float64(t *testing.T) {
|
func TestContext_Float64(t *testing.T) {
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
set.Float64("myflag", float64(17), "doc")
|
set.Float64("myflag", float64(17), "doc")
|
||||||
c := NewContext(nil, set, nil)
|
parentSet := flag.NewFlagSet("test", 0)
|
||||||
|
parentSet.Float64("top-flag", float64(18), "doc")
|
||||||
|
parentCtx := NewContext(nil, parentSet, nil)
|
||||||
|
c := NewContext(nil, set, parentCtx)
|
||||||
expect(t, c.Float64("myflag"), float64(17))
|
expect(t, c.Float64("myflag"), float64(17))
|
||||||
}
|
expect(t, c.Float64("top-flag"), float64(18))
|
||||||
|
|
||||||
func TestContext_GlobalFloat64(t *testing.T) {
|
|
||||||
set := flag.NewFlagSet("test", 0)
|
|
||||||
set.Float64("myflag", float64(17), "doc")
|
|
||||||
c := NewContext(nil, set, nil)
|
|
||||||
expect(t, c.GlobalFloat64("myflag"), float64(17))
|
|
||||||
expect(t, c.GlobalFloat64("nope"), float64(0))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContext_Duration(t *testing.T) {
|
func TestContext_Duration(t *testing.T) {
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
set.Duration("myflag", 12*time.Second, "doc")
|
set.Duration("myflag", 12*time.Second, "doc")
|
||||||
c := NewContext(nil, set, nil)
|
|
||||||
|
parentSet := flag.NewFlagSet("test", 0)
|
||||||
|
parentSet.Duration("top-flag", 13*time.Second, "doc")
|
||||||
|
parentCtx := NewContext(nil, parentSet, nil)
|
||||||
|
|
||||||
|
c := NewContext(nil, set, parentCtx)
|
||||||
expect(t, c.Duration("myflag"), 12*time.Second)
|
expect(t, c.Duration("myflag"), 12*time.Second)
|
||||||
|
expect(t, c.Duration("top-flag"), 13*time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContext_String(t *testing.T) {
|
func TestContext_String(t *testing.T) {
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
set.String("myflag", "hello world", "doc")
|
set.String("myflag", "hello world", "doc")
|
||||||
c := NewContext(nil, set, nil)
|
parentSet := flag.NewFlagSet("test", 0)
|
||||||
|
parentSet.String("top-flag", "hai veld", "doc")
|
||||||
|
parentCtx := NewContext(nil, parentSet, nil)
|
||||||
|
c := NewContext(nil, set, parentCtx)
|
||||||
expect(t, c.String("myflag"), "hello world")
|
expect(t, c.String("myflag"), "hello world")
|
||||||
|
expect(t, c.String("top-flag"), "hai veld")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContext_Path(t *testing.T) {
|
||||||
|
set := flag.NewFlagSet("test", 0)
|
||||||
|
set.String("path", "path/to/file", "path to file")
|
||||||
|
parentSet := flag.NewFlagSet("test", 0)
|
||||||
|
parentSet.String("top-path", "path/to/top/file", "doc")
|
||||||
|
parentCtx := NewContext(nil, parentSet, nil)
|
||||||
|
c := NewContext(nil, set, parentCtx)
|
||||||
|
expect(t, c.Path("path"), "path/to/file")
|
||||||
|
expect(t, c.Path("top-path"), "path/to/top/file")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContext_Bool(t *testing.T) {
|
func TestContext_Bool(t *testing.T) {
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
set.Bool("myflag", false, "doc")
|
set.Bool("myflag", false, "doc")
|
||||||
c := NewContext(nil, set, nil)
|
parentSet := flag.NewFlagSet("test", 0)
|
||||||
|
parentSet.Bool("top-flag", true, "doc")
|
||||||
|
parentCtx := NewContext(nil, parentSet, nil)
|
||||||
|
c := NewContext(nil, set, parentCtx)
|
||||||
expect(t, c.Bool("myflag"), false)
|
expect(t, c.Bool("myflag"), false)
|
||||||
}
|
expect(t, c.Bool("top-flag"), true)
|
||||||
|
|
||||||
func TestContext_BoolT(t *testing.T) {
|
|
||||||
set := flag.NewFlagSet("test", 0)
|
|
||||||
set.Bool("myflag", true, "doc")
|
|
||||||
c := NewContext(nil, set, nil)
|
|
||||||
expect(t, c.BoolT("myflag"), true)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContext_GlobalBool(t *testing.T) {
|
|
||||||
set := flag.NewFlagSet("test", 0)
|
|
||||||
|
|
||||||
globalSet := flag.NewFlagSet("test-global", 0)
|
|
||||||
globalSet.Bool("myflag", false, "doc")
|
|
||||||
globalCtx := NewContext(nil, globalSet, nil)
|
|
||||||
|
|
||||||
c := NewContext(nil, set, globalCtx)
|
|
||||||
expect(t, c.GlobalBool("myflag"), false)
|
|
||||||
expect(t, c.GlobalBool("nope"), false)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContext_GlobalBoolT(t *testing.T) {
|
|
||||||
set := flag.NewFlagSet("test", 0)
|
|
||||||
|
|
||||||
globalSet := flag.NewFlagSet("test-global", 0)
|
|
||||||
globalSet.Bool("myflag", true, "doc")
|
|
||||||
globalCtx := NewContext(nil, globalSet, nil)
|
|
||||||
|
|
||||||
c := NewContext(nil, set, globalCtx)
|
|
||||||
expect(t, c.GlobalBoolT("myflag"), true)
|
|
||||||
expect(t, c.GlobalBoolT("nope"), false)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContext_Args(t *testing.T) {
|
func TestContext_Args(t *testing.T) {
|
||||||
@ -154,7 +141,7 @@ func TestContext_Args(t *testing.T) {
|
|||||||
set.Bool("myflag", false, "doc")
|
set.Bool("myflag", false, "doc")
|
||||||
c := NewContext(nil, set, nil)
|
c := NewContext(nil, set, nil)
|
||||||
_ = set.Parse([]string{"--myflag", "bat", "baz"})
|
_ = set.Parse([]string{"--myflag", "bat", "baz"})
|
||||||
expect(t, len(c.Args()), 2)
|
expect(t, c.Args().Len(), 2)
|
||||||
expect(t, c.Bool("myflag"), true)
|
expect(t, c.Bool("myflag"), true)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,18 +155,22 @@ func TestContext_NArg(t *testing.T) {
|
|||||||
|
|
||||||
func TestContext_IsSet(t *testing.T) {
|
func TestContext_IsSet(t *testing.T) {
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
set.Bool("myflag", false, "doc")
|
set.Bool("one-flag", false, "doc")
|
||||||
set.String("otherflag", "hello world", "doc")
|
set.Bool("two-flag", false, "doc")
|
||||||
globalSet := flag.NewFlagSet("test", 0)
|
set.String("three-flag", "hello world", "doc")
|
||||||
globalSet.Bool("myflagGlobal", true, "doc")
|
parentSet := flag.NewFlagSet("test", 0)
|
||||||
globalCtx := NewContext(nil, globalSet, nil)
|
parentSet.Bool("top-flag", true, "doc")
|
||||||
c := NewContext(nil, set, globalCtx)
|
parentCtx := NewContext(nil, parentSet, nil)
|
||||||
_ = set.Parse([]string{"--myflag", "bat", "baz"})
|
ctx := NewContext(nil, set, parentCtx)
|
||||||
_ = globalSet.Parse([]string{"--myflagGlobal", "bat", "baz"})
|
|
||||||
expect(t, c.IsSet("myflag"), true)
|
_ = set.Parse([]string{"--one-flag", "--two-flag", "--three-flag", "frob"})
|
||||||
expect(t, c.IsSet("otherflag"), false)
|
_ = parentSet.Parse([]string{"--top-flag"})
|
||||||
expect(t, c.IsSet("bogusflag"), false)
|
|
||||||
expect(t, c.IsSet("myflagGlobal"), false)
|
expect(t, ctx.IsSet("one-flag"), true)
|
||||||
|
expect(t, ctx.IsSet("two-flag"), true)
|
||||||
|
expect(t, ctx.IsSet("three-flag"), true)
|
||||||
|
expect(t, ctx.IsSet("top-flag"), true)
|
||||||
|
expect(t, ctx.IsSet("bogus"), false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// XXX Corresponds to hack in context.IsSet for flags with EnvVar field
|
// XXX Corresponds to hack in context.IsSet for flags with EnvVar field
|
||||||
@ -192,15 +183,15 @@ func TestContext_IsSet_fromEnv(t *testing.T) {
|
|||||||
unparsableIsSet, uIsSet bool
|
unparsableIsSet, uIsSet bool
|
||||||
)
|
)
|
||||||
|
|
||||||
clearenv()
|
os.Clearenv()
|
||||||
_ = os.Setenv("APP_TIMEOUT_SECONDS", "15.5")
|
_ = os.Setenv("APP_TIMEOUT_SECONDS", "15.5")
|
||||||
_ = os.Setenv("APP_PASSWORD", "")
|
_ = os.Setenv("APP_PASSWORD", "")
|
||||||
a := App{
|
a := App{
|
||||||
Flags: []Flag{
|
Flags: []Flag{
|
||||||
Float64Flag{Name: "timeout, t", EnvVar: "APP_TIMEOUT_SECONDS"},
|
&Float64Flag{Name: "timeout", Aliases: []string{"t"}, EnvVars: []string{"APP_TIMEOUT_SECONDS"}},
|
||||||
StringFlag{Name: "password, p", EnvVar: "APP_PASSWORD"},
|
&StringFlag{Name: "password", Aliases: []string{"p"}, EnvVars: []string{"APP_PASSWORD"}},
|
||||||
Float64Flag{Name: "unparsable, u", EnvVar: "APP_UNPARSABLE"},
|
&Float64Flag{Name: "unparsable", Aliases: []string{"u"}, EnvVars: []string{"APP_UNPARSABLE"}},
|
||||||
Float64Flag{Name: "no-env-var, n"},
|
&Float64Flag{Name: "no-env-var", Aliases: []string{"n"}},
|
||||||
},
|
},
|
||||||
Action: func(ctx *Context) error {
|
Action: func(ctx *Context) error {
|
||||||
timeoutIsSet = ctx.IsSet("timeout")
|
timeoutIsSet = ctx.IsSet("timeout")
|
||||||
@ -228,94 +219,6 @@ func TestContext_IsSet_fromEnv(t *testing.T) {
|
|||||||
expect(t, uIsSet, false)
|
expect(t, uIsSet, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContext_GlobalIsSet(t *testing.T) {
|
|
||||||
set := flag.NewFlagSet("test", 0)
|
|
||||||
set.Bool("myflag", false, "doc")
|
|
||||||
set.String("otherflag", "hello world", "doc")
|
|
||||||
globalSet := flag.NewFlagSet("test", 0)
|
|
||||||
globalSet.Bool("myflagGlobal", true, "doc")
|
|
||||||
globalSet.Bool("myflagGlobalUnset", true, "doc")
|
|
||||||
globalCtx := NewContext(nil, globalSet, nil)
|
|
||||||
c := NewContext(nil, set, globalCtx)
|
|
||||||
_ = set.Parse([]string{"--myflag", "bat", "baz"})
|
|
||||||
_ = globalSet.Parse([]string{"--myflagGlobal", "bat", "baz"})
|
|
||||||
expect(t, c.GlobalIsSet("myflag"), false)
|
|
||||||
expect(t, c.GlobalIsSet("otherflag"), false)
|
|
||||||
expect(t, c.GlobalIsSet("bogusflag"), false)
|
|
||||||
expect(t, c.GlobalIsSet("myflagGlobal"), true)
|
|
||||||
expect(t, c.GlobalIsSet("myflagGlobalUnset"), false)
|
|
||||||
expect(t, c.GlobalIsSet("bogusGlobal"), false)
|
|
||||||
}
|
|
||||||
|
|
||||||
// XXX Corresponds to hack in context.IsSet for flags with EnvVar field
|
|
||||||
// Should be moved to `flag_test` in v2
|
|
||||||
func TestContext_GlobalIsSet_fromEnv(t *testing.T) {
|
|
||||||
var (
|
|
||||||
timeoutIsSet, tIsSet bool
|
|
||||||
noEnvVarIsSet, nIsSet bool
|
|
||||||
passwordIsSet, pIsSet bool
|
|
||||||
passwordValue string
|
|
||||||
unparsableIsSet, uIsSet bool
|
|
||||||
overrideIsSet, oIsSet bool
|
|
||||||
overrideValue string
|
|
||||||
)
|
|
||||||
|
|
||||||
clearenv()
|
|
||||||
_ = os.Setenv("APP_TIMEOUT_SECONDS", "15.5")
|
|
||||||
_ = os.Setenv("APP_PASSWORD", "badpass")
|
|
||||||
_ = os.Setenv("APP_OVERRIDE", "overridden")
|
|
||||||
a := App{
|
|
||||||
Flags: []Flag{
|
|
||||||
Float64Flag{Name: "timeout, t", EnvVar: "APP_TIMEOUT_SECONDS"},
|
|
||||||
StringFlag{Name: "password, p", EnvVar: "APP_PASSWORD"},
|
|
||||||
Float64Flag{Name: "no-env-var, n"},
|
|
||||||
Float64Flag{Name: "unparsable, u", EnvVar: "APP_UNPARSABLE"},
|
|
||||||
StringFlag{Name: "overrides-default, o", Value: "default", EnvVar: "APP_OVERRIDE"},
|
|
||||||
},
|
|
||||||
Commands: []Command{
|
|
||||||
{
|
|
||||||
Name: "hello",
|
|
||||||
Action: func(ctx *Context) error {
|
|
||||||
timeoutIsSet = ctx.GlobalIsSet("timeout")
|
|
||||||
tIsSet = ctx.GlobalIsSet("t")
|
|
||||||
passwordIsSet = ctx.GlobalIsSet("password")
|
|
||||||
pIsSet = ctx.GlobalIsSet("p")
|
|
||||||
passwordValue = ctx.GlobalString("password")
|
|
||||||
unparsableIsSet = ctx.GlobalIsSet("unparsable")
|
|
||||||
uIsSet = ctx.GlobalIsSet("u")
|
|
||||||
noEnvVarIsSet = ctx.GlobalIsSet("no-env-var")
|
|
||||||
nIsSet = ctx.GlobalIsSet("n")
|
|
||||||
overrideIsSet = ctx.GlobalIsSet("overrides-default")
|
|
||||||
oIsSet = ctx.GlobalIsSet("o")
|
|
||||||
overrideValue = ctx.GlobalString("overrides-default")
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
if err := a.Run([]string{"run", "hello"}); err != nil {
|
|
||||||
t.Logf("error running Run(): %+v", err)
|
|
||||||
}
|
|
||||||
expect(t, timeoutIsSet, true)
|
|
||||||
expect(t, tIsSet, true)
|
|
||||||
expect(t, passwordIsSet, true)
|
|
||||||
expect(t, pIsSet, true)
|
|
||||||
expect(t, passwordValue, "badpass")
|
|
||||||
expect(t, unparsableIsSet, false)
|
|
||||||
expect(t, noEnvVarIsSet, false)
|
|
||||||
expect(t, nIsSet, false)
|
|
||||||
expect(t, overrideIsSet, true)
|
|
||||||
expect(t, oIsSet, true)
|
|
||||||
expect(t, overrideValue, "overridden")
|
|
||||||
|
|
||||||
_ = os.Setenv("APP_UNPARSABLE", "foobar")
|
|
||||||
if err := a.Run([]string{"run"}); err != nil {
|
|
||||||
t.Logf("error running Run(): %+v", err)
|
|
||||||
}
|
|
||||||
expect(t, unparsableIsSet, false)
|
|
||||||
expect(t, uIsSet, false)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContext_NumFlags(t *testing.T) {
|
func TestContext_NumFlags(t *testing.T) {
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
set.Bool("myflag", false, "doc")
|
set.Bool("myflag", false, "doc")
|
||||||
@ -329,62 +232,6 @@ func TestContext_NumFlags(t *testing.T) {
|
|||||||
expect(t, c.NumFlags(), 2)
|
expect(t, c.NumFlags(), 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContext_GlobalFlag(t *testing.T) {
|
|
||||||
var globalFlag string
|
|
||||||
var globalFlagSet bool
|
|
||||||
app := NewApp()
|
|
||||||
app.Flags = []Flag{
|
|
||||||
StringFlag{Name: "global, g", Usage: "global"},
|
|
||||||
}
|
|
||||||
app.Action = func(c *Context) error {
|
|
||||||
globalFlag = c.GlobalString("global")
|
|
||||||
globalFlagSet = c.GlobalIsSet("global")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
_ = app.Run([]string{"command", "-g", "foo"})
|
|
||||||
expect(t, globalFlag, "foo")
|
|
||||||
expect(t, globalFlagSet, true)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContext_GlobalFlagsInSubcommands(t *testing.T) {
|
|
||||||
subcommandRun := false
|
|
||||||
parentFlag := false
|
|
||||||
app := NewApp()
|
|
||||||
|
|
||||||
app.Flags = []Flag{
|
|
||||||
BoolFlag{Name: "debug, d", Usage: "Enable debugging"},
|
|
||||||
}
|
|
||||||
|
|
||||||
app.Commands = []Command{
|
|
||||||
{
|
|
||||||
Name: "foo",
|
|
||||||
Flags: []Flag{
|
|
||||||
BoolFlag{Name: "parent, p", Usage: "Parent flag"},
|
|
||||||
},
|
|
||||||
Subcommands: []Command{
|
|
||||||
{
|
|
||||||
Name: "bar",
|
|
||||||
Action: func(c *Context) error {
|
|
||||||
if c.GlobalBool("debug") {
|
|
||||||
subcommandRun = true
|
|
||||||
}
|
|
||||||
if c.GlobalBool("parent") {
|
|
||||||
parentFlag = true
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = app.Run([]string{"command", "-d", "foo", "-p", "bar"})
|
|
||||||
|
|
||||||
expect(t, subcommandRun, true)
|
|
||||||
expect(t, parentFlag, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestContext_Set(t *testing.T) {
|
func TestContext_Set(t *testing.T) {
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
set.Int("int", 5, "an int")
|
set.Int("int", 5, "an int")
|
||||||
@ -396,25 +243,105 @@ func TestContext_Set(t *testing.T) {
|
|||||||
expect(t, c.IsSet("int"), true)
|
expect(t, c.IsSet("int"), true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContext_GlobalSet(t *testing.T) {
|
func TestContext_LocalFlagNames(t *testing.T) {
|
||||||
gSet := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
gSet.Int("int", 5, "an int")
|
set.Bool("one-flag", false, "doc")
|
||||||
|
set.String("two-flag", "hello world", "doc")
|
||||||
|
parentSet := flag.NewFlagSet("test", 0)
|
||||||
|
parentSet.Bool("top-flag", true, "doc")
|
||||||
|
parentCtx := NewContext(nil, parentSet, nil)
|
||||||
|
ctx := NewContext(nil, set, parentCtx)
|
||||||
|
_ = set.Parse([]string{"--one-flag", "--two-flag=foo"})
|
||||||
|
_ = parentSet.Parse([]string{"--top-flag"})
|
||||||
|
|
||||||
set := flag.NewFlagSet("sub", 0)
|
actualFlags := ctx.LocalFlagNames()
|
||||||
set.Int("int", 3, "an int")
|
sort.Strings(actualFlags)
|
||||||
|
|
||||||
pc := NewContext(nil, gSet, nil)
|
expect(t, actualFlags, []string{"one-flag", "two-flag"})
|
||||||
c := NewContext(nil, set, pc)
|
}
|
||||||
|
|
||||||
_ = c.Set("int", "1")
|
func TestContext_FlagNames(t *testing.T) {
|
||||||
expect(t, c.Int("int"), 1)
|
set := flag.NewFlagSet("test", 0)
|
||||||
expect(t, c.GlobalInt("int"), 5)
|
set.Bool("one-flag", false, "doc")
|
||||||
|
set.String("two-flag", "hello world", "doc")
|
||||||
|
parentSet := flag.NewFlagSet("test", 0)
|
||||||
|
parentSet.Bool("top-flag", true, "doc")
|
||||||
|
parentCtx := NewContext(nil, parentSet, nil)
|
||||||
|
ctx := NewContext(nil, set, parentCtx)
|
||||||
|
_ = set.Parse([]string{"--one-flag", "--two-flag=foo"})
|
||||||
|
_ = parentSet.Parse([]string{"--top-flag"})
|
||||||
|
|
||||||
expect(t, c.GlobalIsSet("int"), false)
|
actualFlags := ctx.FlagNames()
|
||||||
_ = c.GlobalSet("int", "1")
|
sort.Strings(actualFlags)
|
||||||
expect(t, c.Int("int"), 1)
|
|
||||||
expect(t, c.GlobalInt("int"), 1)
|
expect(t, actualFlags, []string{"one-flag", "top-flag", "two-flag"})
|
||||||
expect(t, c.GlobalIsSet("int"), true)
|
}
|
||||||
|
|
||||||
|
func TestContext_Lineage(t *testing.T) {
|
||||||
|
set := flag.NewFlagSet("test", 0)
|
||||||
|
set.Bool("local-flag", false, "doc")
|
||||||
|
parentSet := flag.NewFlagSet("test", 0)
|
||||||
|
parentSet.Bool("top-flag", true, "doc")
|
||||||
|
parentCtx := NewContext(nil, parentSet, nil)
|
||||||
|
ctx := NewContext(nil, set, parentCtx)
|
||||||
|
_ = set.Parse([]string{"--local-flag"})
|
||||||
|
_ = parentSet.Parse([]string{"--top-flag"})
|
||||||
|
|
||||||
|
lineage := ctx.Lineage()
|
||||||
|
expect(t, len(lineage), 2)
|
||||||
|
expect(t, lineage[0], ctx)
|
||||||
|
expect(t, lineage[1], parentCtx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestContext_lookupFlagSet(t *testing.T) {
|
||||||
|
set := flag.NewFlagSet("test", 0)
|
||||||
|
set.Bool("local-flag", false, "doc")
|
||||||
|
parentSet := flag.NewFlagSet("test", 0)
|
||||||
|
parentSet.Bool("top-flag", true, "doc")
|
||||||
|
parentCtx := NewContext(nil, parentSet, nil)
|
||||||
|
ctx := NewContext(nil, set, parentCtx)
|
||||||
|
_ = set.Parse([]string{"--local-flag"})
|
||||||
|
_ = parentSet.Parse([]string{"--top-flag"})
|
||||||
|
|
||||||
|
fs := lookupFlagSet("top-flag", ctx)
|
||||||
|
expect(t, fs, parentCtx.flagSet)
|
||||||
|
|
||||||
|
fs = lookupFlagSet("local-flag", ctx)
|
||||||
|
expect(t, fs, ctx.flagSet)
|
||||||
|
|
||||||
|
if fs := lookupFlagSet("frob", ctx); fs != nil {
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNonNilContext(t *testing.T) {
|
||||||
|
ctx := NewContext(nil, nil, nil)
|
||||||
|
if ctx.Context == nil {
|
||||||
|
t.Fatal("expected a non nil context when no parent is present")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestContextPropagation tests that
|
||||||
|
// *cli.Context always has a valid
|
||||||
|
// context.Context
|
||||||
|
func TestContextPropagation(t *testing.T) {
|
||||||
|
parent := NewContext(nil, nil, nil)
|
||||||
|
parent.Context = context.WithValue(context.Background(), "key", "val")
|
||||||
|
ctx := NewContext(nil, nil, parent)
|
||||||
|
val := ctx.Value("key")
|
||||||
|
if val == nil {
|
||||||
|
t.Fatal("expected a parent context to be inherited but got nil")
|
||||||
|
}
|
||||||
|
valstr, _ := val.(string)
|
||||||
|
if valstr != "val" {
|
||||||
|
t.Fatalf("expected the context value to be %q but got %q", "val", valstr)
|
||||||
|
}
|
||||||
|
parent = NewContext(nil, nil, nil)
|
||||||
|
parent.Context = nil
|
||||||
|
ctx = NewContext(nil, nil, parent)
|
||||||
|
if ctx.Context == nil {
|
||||||
|
t.Fatal("expected context to not be nil even if the parent's context is nil")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCheckRequiredFlags(t *testing.T) {
|
func TestCheckRequiredFlags(t *testing.T) {
|
||||||
@ -432,13 +359,13 @@ func TestCheckRequiredFlags(t *testing.T) {
|
|||||||
{
|
{
|
||||||
testCase: "optional",
|
testCase: "optional",
|
||||||
flags: []Flag{
|
flags: []Flag{
|
||||||
StringFlag{Name: "optionalFlag"},
|
&StringFlag{Name: "optionalFlag"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testCase: "required",
|
testCase: "required",
|
||||||
flags: []Flag{
|
flags: []Flag{
|
||||||
StringFlag{Name: "requiredFlag", Required: true},
|
&StringFlag{Name: "requiredFlag", Required: true},
|
||||||
},
|
},
|
||||||
expectedAnError: true,
|
expectedAnError: true,
|
||||||
expectedErrorContents: []string{"requiredFlag"},
|
expectedErrorContents: []string{"requiredFlag"},
|
||||||
@ -446,30 +373,30 @@ func TestCheckRequiredFlags(t *testing.T) {
|
|||||||
{
|
{
|
||||||
testCase: "required_and_present",
|
testCase: "required_and_present",
|
||||||
flags: []Flag{
|
flags: []Flag{
|
||||||
StringFlag{Name: "requiredFlag", Required: true},
|
&StringFlag{Name: "requiredFlag", Required: true},
|
||||||
},
|
},
|
||||||
parseInput: []string{"--requiredFlag", "myinput"},
|
parseInput: []string{"--requiredFlag", "myinput"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testCase: "required_and_present_via_env_var",
|
testCase: "required_and_present_via_env_var",
|
||||||
flags: []Flag{
|
flags: []Flag{
|
||||||
StringFlag{Name: "requiredFlag", Required: true, EnvVar: "REQUIRED_FLAG"},
|
&StringFlag{Name: "requiredFlag", Required: true, EnvVars: []string{"REQUIRED_FLAG"}},
|
||||||
},
|
},
|
||||||
envVarInput: [2]string{"REQUIRED_FLAG", "true"},
|
envVarInput: [2]string{"REQUIRED_FLAG", "true"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testCase: "required_and_optional",
|
testCase: "required_and_optional",
|
||||||
flags: []Flag{
|
flags: []Flag{
|
||||||
StringFlag{Name: "requiredFlag", Required: true},
|
&StringFlag{Name: "requiredFlag", Required: true},
|
||||||
StringFlag{Name: "optionalFlag"},
|
&StringFlag{Name: "optionalFlag"},
|
||||||
},
|
},
|
||||||
expectedAnError: true,
|
expectedAnError: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testCase: "required_and_optional_and_optional_present",
|
testCase: "required_and_optional_and_optional_present",
|
||||||
flags: []Flag{
|
flags: []Flag{
|
||||||
StringFlag{Name: "requiredFlag", Required: true},
|
&StringFlag{Name: "requiredFlag", Required: true},
|
||||||
StringFlag{Name: "optionalFlag"},
|
&StringFlag{Name: "optionalFlag"},
|
||||||
},
|
},
|
||||||
parseInput: []string{"--optionalFlag", "myinput"},
|
parseInput: []string{"--optionalFlag", "myinput"},
|
||||||
expectedAnError: true,
|
expectedAnError: true,
|
||||||
@ -477,8 +404,8 @@ func TestCheckRequiredFlags(t *testing.T) {
|
|||||||
{
|
{
|
||||||
testCase: "required_and_optional_and_optional_present_via_env_var",
|
testCase: "required_and_optional_and_optional_present_via_env_var",
|
||||||
flags: []Flag{
|
flags: []Flag{
|
||||||
StringFlag{Name: "requiredFlag", Required: true},
|
&StringFlag{Name: "requiredFlag", Required: true},
|
||||||
StringFlag{Name: "optionalFlag", EnvVar: "OPTIONAL_FLAG"},
|
&StringFlag{Name: "optionalFlag", EnvVars: []string{"OPTIONAL_FLAG"}},
|
||||||
},
|
},
|
||||||
envVarInput: [2]string{"OPTIONAL_FLAG", "true"},
|
envVarInput: [2]string{"OPTIONAL_FLAG", "true"},
|
||||||
expectedAnError: true,
|
expectedAnError: true,
|
||||||
@ -486,16 +413,16 @@ func TestCheckRequiredFlags(t *testing.T) {
|
|||||||
{
|
{
|
||||||
testCase: "required_and_optional_and_required_present",
|
testCase: "required_and_optional_and_required_present",
|
||||||
flags: []Flag{
|
flags: []Flag{
|
||||||
StringFlag{Name: "requiredFlag", Required: true},
|
&StringFlag{Name: "requiredFlag", Required: true},
|
||||||
StringFlag{Name: "optionalFlag"},
|
&StringFlag{Name: "optionalFlag"},
|
||||||
},
|
},
|
||||||
parseInput: []string{"--requiredFlag", "myinput"},
|
parseInput: []string{"--requiredFlag", "myinput"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testCase: "two_required",
|
testCase: "two_required",
|
||||||
flags: []Flag{
|
flags: []Flag{
|
||||||
StringFlag{Name: "requiredFlagOne", Required: true},
|
&StringFlag{Name: "requiredFlagOne", Required: true},
|
||||||
StringFlag{Name: "requiredFlagTwo", Required: true},
|
&StringFlag{Name: "requiredFlagTwo", Required: true},
|
||||||
},
|
},
|
||||||
expectedAnError: true,
|
expectedAnError: true,
|
||||||
expectedErrorContents: []string{"requiredFlagOne", "requiredFlagTwo"},
|
expectedErrorContents: []string{"requiredFlagOne", "requiredFlagTwo"},
|
||||||
@ -503,8 +430,8 @@ func TestCheckRequiredFlags(t *testing.T) {
|
|||||||
{
|
{
|
||||||
testCase: "two_required_and_one_present",
|
testCase: "two_required_and_one_present",
|
||||||
flags: []Flag{
|
flags: []Flag{
|
||||||
StringFlag{Name: "requiredFlag", Required: true},
|
&StringFlag{Name: "requiredFlag", Required: true},
|
||||||
StringFlag{Name: "requiredFlagTwo", Required: true},
|
&StringFlag{Name: "requiredFlagTwo", Required: true},
|
||||||
},
|
},
|
||||||
parseInput: []string{"--requiredFlag", "myinput"},
|
parseInput: []string{"--requiredFlag", "myinput"},
|
||||||
expectedAnError: true,
|
expectedAnError: true,
|
||||||
@ -512,44 +439,47 @@ func TestCheckRequiredFlags(t *testing.T) {
|
|||||||
{
|
{
|
||||||
testCase: "two_required_and_both_present",
|
testCase: "two_required_and_both_present",
|
||||||
flags: []Flag{
|
flags: []Flag{
|
||||||
StringFlag{Name: "requiredFlag", Required: true},
|
&StringFlag{Name: "requiredFlag", Required: true},
|
||||||
StringFlag{Name: "requiredFlagTwo", Required: true},
|
&StringFlag{Name: "requiredFlagTwo", Required: true},
|
||||||
},
|
},
|
||||||
parseInput: []string{"--requiredFlag", "myinput", "--requiredFlagTwo", "myinput"},
|
parseInput: []string{"--requiredFlag", "myinput", "--requiredFlagTwo", "myinput"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testCase: "required_flag_with_short_name",
|
testCase: "required_flag_with_short_name",
|
||||||
flags: []Flag{
|
flags: []Flag{
|
||||||
StringSliceFlag{Name: "names, N", Required: true},
|
&StringSliceFlag{Name: "names", Aliases: []string{"N"}, Required: true},
|
||||||
},
|
},
|
||||||
parseInput: []string{"-N", "asd", "-N", "qwe"},
|
parseInput: []string{"-N", "asd", "-N", "qwe"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
testCase: "required_flag_with_multiple_short_names",
|
testCase: "required_flag_with_multiple_short_names",
|
||||||
flags: []Flag{
|
flags: []Flag{
|
||||||
StringSliceFlag{Name: "names, N, n", Required: true},
|
&StringSliceFlag{Name: "names", Aliases: []string{"N", "n"}, Required: true},
|
||||||
},
|
},
|
||||||
parseInput: []string{"-n", "asd", "-n", "qwe"},
|
parseInput: []string{"-n", "asd", "-n", "qwe"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tdata {
|
for _, test := range tdata {
|
||||||
t.Run(test.testCase, func(t *testing.T) {
|
t.Run(test.testCase, func(t *testing.T) {
|
||||||
// setup
|
// setup
|
||||||
set := flag.NewFlagSet("test", 0)
|
|
||||||
for _, flags := range test.flags {
|
|
||||||
flags.Apply(set)
|
|
||||||
}
|
|
||||||
_ = set.Parse(test.parseInput)
|
|
||||||
if test.envVarInput[0] != "" {
|
if test.envVarInput[0] != "" {
|
||||||
os.Clearenv()
|
os.Clearenv()
|
||||||
_ = os.Setenv(test.envVarInput[0], test.envVarInput[1])
|
_ = os.Setenv(test.envVarInput[0], test.envVarInput[1])
|
||||||
}
|
}
|
||||||
ctx := &Context{}
|
|
||||||
context := NewContext(ctx.App, set, ctx)
|
set := flag.NewFlagSet("test", 0)
|
||||||
context.Command.Flags = test.flags
|
for _, flags := range test.flags {
|
||||||
|
_ = flags.Apply(set)
|
||||||
|
}
|
||||||
|
_ = set.Parse(test.parseInput)
|
||||||
|
|
||||||
|
c := &Context{}
|
||||||
|
ctx := NewContext(c.App, set, c)
|
||||||
|
ctx.Command.Flags = test.flags
|
||||||
|
|
||||||
// logic under test
|
// logic under test
|
||||||
err := checkRequiredFlags(test.flags, context)
|
err := checkRequiredFlags(test.flags, ctx)
|
||||||
|
|
||||||
// assertions
|
// assertions
|
||||||
if test.expectedAnError && err == nil {
|
if test.expectedAnError && err == nil {
|
||||||
@ -559,8 +489,10 @@ func TestCheckRequiredFlags(t *testing.T) {
|
|||||||
t.Errorf("did not expected an error, but there was one: %s", err)
|
t.Errorf("did not expected an error, but there was one: %s", err)
|
||||||
}
|
}
|
||||||
for _, errString := range test.expectedErrorContents {
|
for _, errString := range test.expectedErrorContents {
|
||||||
if !strings.Contains(err.Error(), errString) {
|
if err != nil {
|
||||||
t.Errorf("expected error %q to contain %q, but it didn't!", err.Error(), errString)
|
if !strings.Contains(err.Error(), errString) {
|
||||||
|
t.Errorf("expected error %q to contain %q, but it didn't!", err.Error(), errString)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
10
docs.go
10
docs.go
@ -53,10 +53,9 @@ func (a *App) writeDocTemplate(w io.Writer) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func prepareCommands(commands []Command, level int) []string {
|
func prepareCommands(commands []*Command, level int) []string {
|
||||||
coms := []string{}
|
var coms []string
|
||||||
for i := range commands {
|
for _, command := range commands {
|
||||||
command := &commands[i]
|
|
||||||
if command.Hidden {
|
if command.Hidden {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -110,7 +109,8 @@ func prepareFlags(
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
modifiedArg := opener
|
modifiedArg := opener
|
||||||
for _, s := range strings.Split(flag.GetName(), ",") {
|
|
||||||
|
for _, s := range flag.Names() {
|
||||||
trimmed := strings.TrimSpace(s)
|
trimmed := strings.TrimSpace(s)
|
||||||
if len(modifiedArg) > len(opener) {
|
if len(modifiedArg) > len(opener) {
|
||||||
modifiedArg += sep
|
modifiedArg += sep
|
||||||
|
@ -2,7 +2,34 @@
|
|||||||
|
|
||||||
**ATTN**: This project uses [semantic versioning](http://semver.org/).
|
**ATTN**: This project uses [semantic versioning](http://semver.org/).
|
||||||
|
|
||||||
## [Unreleased]
|
## 2.0.0 - (unreleased 2.x series)
|
||||||
|
### Added
|
||||||
|
- `NewStringSlice` and `NewIntSlice` for creating their related types
|
||||||
|
- `Float64SliceFlag` for unmarshaling a list of floats from the user
|
||||||
|
- `Context.Lineage` to get all contexts from current up to global
|
||||||
|
- `Context.LocalFlagNames` to get the flag names from *only* the current context
|
||||||
|
- `BoolFlag.Value` to handle both default-false and default-true
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
- `Context.FlagNames` now returns all flags in the context lineage
|
||||||
|
- `Context.IsSet` now considers the full context lineage
|
||||||
|
- Added `IsSet` method to the `Flag` interface which allows us to detect whether or not a flag has been set
|
||||||
|
|
||||||
|
### Removed
|
||||||
|
- the ability to specify `&StringSlice{...string}` or `&IntSlice{...int}`.
|
||||||
|
- adapter code for deprecated `Action` func signature
|
||||||
|
- deprecated `App.Author`, `App.Email`, and `Command.ShortName` fields
|
||||||
|
- All `Context.Global*` methods, as the non-global versions now traverse up
|
||||||
|
the context lineage automatically.
|
||||||
|
- `Context.Parent` method, as this is now available via `Context.Lineage`
|
||||||
|
- `BoolTFlag` and related code, as this is now available via `BoolFlag.Value`
|
||||||
|
|
||||||
|
## [Unreleased] - (1.x series)
|
||||||
|
### Added
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
### Removed
|
||||||
|
|
||||||
## [1.22.1] - 2019-09-11
|
## [1.22.1] - 2019-09-11
|
||||||
|
|
1489
docs/v2/manual.md
Normal file
1489
docs/v2/manual.md
Normal file
File diff suppressed because it is too large
Load Diff
43
docs_test.go
43
docs_test.go
@ -9,39 +9,44 @@ func testApp() *App {
|
|||||||
app := NewApp()
|
app := NewApp()
|
||||||
app.Name = "greet"
|
app.Name = "greet"
|
||||||
app.Flags = []Flag{
|
app.Flags = []Flag{
|
||||||
StringFlag{
|
&StringFlag{
|
||||||
Name: "socket, s",
|
Name: "socket",
|
||||||
|
Aliases: []string{"s"},
|
||||||
Usage: "some 'usage' text",
|
Usage: "some 'usage' text",
|
||||||
Value: "value",
|
Value: "value",
|
||||||
TakesFile: true,
|
TakesFile: true,
|
||||||
},
|
},
|
||||||
StringFlag{Name: "flag, fl, f"},
|
&StringFlag{Name: "flag", Aliases: []string{"fl", "f"}},
|
||||||
BoolFlag{
|
&BoolFlag{
|
||||||
Name: "another-flag, b",
|
Name: "another-flag",
|
||||||
Usage: "another usage text",
|
Aliases: []string{"b"},
|
||||||
|
Usage: "another usage text",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
app.Commands = []Command{{
|
app.Commands = []*Command{{
|
||||||
Aliases: []string{"c"},
|
Aliases: []string{"c"},
|
||||||
Flags: []Flag{
|
Flags: []Flag{
|
||||||
StringFlag{
|
&StringFlag{
|
||||||
Name: "flag, fl, f",
|
Name: "flag",
|
||||||
|
Aliases: []string{"fl", "f"},
|
||||||
TakesFile: true,
|
TakesFile: true,
|
||||||
},
|
},
|
||||||
BoolFlag{
|
&BoolFlag{
|
||||||
Name: "another-flag, b",
|
Name: "another-flag",
|
||||||
Usage: "another usage text",
|
Aliases: []string{"b"},
|
||||||
|
Usage: "another usage text",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Name: "config",
|
Name: "config",
|
||||||
Usage: "another usage test",
|
Usage: "another usage test",
|
||||||
Subcommands: []Command{{
|
Subcommands: []*Command{{
|
||||||
Aliases: []string{"s", "ss"},
|
Aliases: []string{"s", "ss"},
|
||||||
Flags: []Flag{
|
Flags: []Flag{
|
||||||
StringFlag{Name: "sub-flag, sub-fl, s"},
|
&StringFlag{Name: "sub-flag", Aliases: []string{"sub-fl", "s"}},
|
||||||
BoolFlag{
|
&BoolFlag{
|
||||||
Name: "sub-command-flag, s",
|
Name: "sub-command-flag",
|
||||||
Usage: "some usage text",
|
Aliases: []string{"s"},
|
||||||
|
Usage: "some usage text",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Name: "sub-config",
|
Name: "sub-config",
|
||||||
@ -59,9 +64,7 @@ func testApp() *App {
|
|||||||
}}
|
}}
|
||||||
app.UsageText = "app [first_arg] [second_arg]"
|
app.UsageText = "app [first_arg] [second_arg]"
|
||||||
app.Usage = "Some app"
|
app.Usage = "Some app"
|
||||||
app.Author = "Harrison"
|
app.Authors = []*Author{{Name: "Harrison", Email: "harrison@lolwut.com"}}
|
||||||
app.Email = "harrison@lolwut.com"
|
|
||||||
app.Authors = []Author{{Name: "Oliver Allen", Email: "oliver@toyshop.com"}}
|
|
||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
|
61
errors.go
61
errors.go
@ -15,25 +15,39 @@ var OsExiter = os.Exit
|
|||||||
var ErrWriter io.Writer = os.Stderr
|
var ErrWriter io.Writer = os.Stderr
|
||||||
|
|
||||||
// MultiError is an error that wraps multiple errors.
|
// MultiError is an error that wraps multiple errors.
|
||||||
type MultiError struct {
|
type MultiError interface {
|
||||||
Errors []error
|
error
|
||||||
|
// Errors returns a copy of the errors slice
|
||||||
|
Errors() []error
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMultiError creates a new MultiError. Pass in one or more errors.
|
// NewMultiError creates a new MultiError. Pass in one or more errors.
|
||||||
func NewMultiError(err ...error) MultiError {
|
func newMultiError(err ...error) MultiError {
|
||||||
return MultiError{Errors: err}
|
ret := multiError(err)
|
||||||
|
return &ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type multiError []error
|
||||||
|
|
||||||
// Error implements the error interface.
|
// Error implements the error interface.
|
||||||
func (m MultiError) Error() string {
|
func (m *multiError) Error() string {
|
||||||
errs := make([]string, len(m.Errors))
|
errs := make([]string, len(*m))
|
||||||
for i, err := range m.Errors {
|
for i, err := range *m {
|
||||||
errs[i] = err.Error()
|
errs[i] = err.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.Join(errs, "\n")
|
return strings.Join(errs, "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Errors returns a copy of the errors slice
|
||||||
|
func (m *multiError) Errors() []error {
|
||||||
|
errs := make([]error, len(*m))
|
||||||
|
for _, err := range *m {
|
||||||
|
errs = append(errs, err)
|
||||||
|
}
|
||||||
|
return errs
|
||||||
|
}
|
||||||
|
|
||||||
type ErrorFormatter interface {
|
type ErrorFormatter interface {
|
||||||
Format(s fmt.State, verb rune)
|
Format(s fmt.State, verb rune)
|
||||||
}
|
}
|
||||||
@ -45,29 +59,30 @@ type ExitCoder interface {
|
|||||||
ExitCode() int
|
ExitCode() int
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExitError fulfills both the builtin `error` interface and `ExitCoder`
|
type exitError struct {
|
||||||
type ExitError struct {
|
|
||||||
exitCode int
|
exitCode int
|
||||||
message interface{}
|
message interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewExitError makes a new *ExitError
|
// NewExitError makes a new *exitError
|
||||||
func NewExitError(message interface{}, exitCode int) *ExitError {
|
func NewExitError(message interface{}, exitCode int) ExitCoder {
|
||||||
return &ExitError{
|
return Exit(message, exitCode)
|
||||||
exitCode: exitCode,
|
}
|
||||||
|
|
||||||
|
// Exit wraps a message and exit code into an ExitCoder suitable for handling by
|
||||||
|
// HandleExitCoder
|
||||||
|
func Exit(message interface{}, exitCode int) ExitCoder {
|
||||||
|
return &exitError{
|
||||||
message: message,
|
message: message,
|
||||||
|
exitCode: exitCode,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error returns the string message, fulfilling the interface required by
|
func (ee *exitError) Error() string {
|
||||||
// `error`
|
|
||||||
func (ee *ExitError) Error() string {
|
|
||||||
return fmt.Sprintf("%v", ee.message)
|
return fmt.Sprintf("%v", ee.message)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExitCode returns the exit code, fulfilling the interface required by
|
func (ee *exitError) ExitCode() int {
|
||||||
// `ExitCoder`
|
|
||||||
func (ee *ExitError) ExitCode() int {
|
|
||||||
return ee.exitCode
|
return ee.exitCode
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -83,9 +98,9 @@ func HandleExitCoder(err error) {
|
|||||||
if exitErr, ok := err.(ExitCoder); ok {
|
if exitErr, ok := err.(ExitCoder); ok {
|
||||||
if err.Error() != "" {
|
if err.Error() != "" {
|
||||||
if _, ok := exitErr.(ErrorFormatter); ok {
|
if _, ok := exitErr.(ErrorFormatter); ok {
|
||||||
fmt.Fprintf(ErrWriter, "%+v\n", err)
|
_, _ = fmt.Fprintf(ErrWriter, "%+v\n", err)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintln(ErrWriter, err)
|
_, _ = fmt.Fprintln(ErrWriter, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
OsExiter(exitErr.ExitCode())
|
OsExiter(exitErr.ExitCode())
|
||||||
@ -101,10 +116,10 @@ func HandleExitCoder(err error) {
|
|||||||
|
|
||||||
func handleMultiError(multiErr MultiError) int {
|
func handleMultiError(multiErr MultiError) int {
|
||||||
code := 1
|
code := 1
|
||||||
for _, merr := range multiErr.Errors {
|
for _, merr := range multiErr.Errors() {
|
||||||
if multiErr2, ok := merr.(MultiError); ok {
|
if multiErr2, ok := merr.(MultiError); ok {
|
||||||
code = handleMultiError(multiErr2)
|
code = handleMultiError(multiErr2)
|
||||||
} else {
|
} else if merr != nil {
|
||||||
fmt.Fprintln(ErrWriter, merr)
|
fmt.Fprintln(ErrWriter, merr)
|
||||||
if exitErr, ok := merr.(ExitCoder); ok {
|
if exitErr, ok := merr.(ExitCoder); ok {
|
||||||
code = exitErr.ExitCode()
|
code = exitErr.ExitCode()
|
||||||
|
@ -39,7 +39,7 @@ func TestHandleExitCoder_ExitCoder(t *testing.T) {
|
|||||||
|
|
||||||
defer func() { OsExiter = fakeOsExiter }()
|
defer func() { OsExiter = fakeOsExiter }()
|
||||||
|
|
||||||
HandleExitCoder(NewExitError("galactic perimeter breach", 9))
|
HandleExitCoder(Exit("galactic perimeter breach", 9))
|
||||||
|
|
||||||
expect(t, exitCode, 9)
|
expect(t, exitCode, 9)
|
||||||
expect(t, called, true)
|
expect(t, called, true)
|
||||||
@ -58,9 +58,9 @@ func TestHandleExitCoder_MultiErrorWithExitCoder(t *testing.T) {
|
|||||||
|
|
||||||
defer func() { OsExiter = fakeOsExiter }()
|
defer func() { OsExiter = fakeOsExiter }()
|
||||||
|
|
||||||
exitErr := NewExitError("galactic perimeter breach", 9)
|
exitErr := Exit("galactic perimeter breach", 9)
|
||||||
exitErr2 := NewExitError("last ExitCoder", 11)
|
exitErr2 := Exit("last ExitCoder", 11)
|
||||||
err := NewMultiError(errors.New("wowsa"), errors.New("egad"), exitErr, exitErr2)
|
err := newMultiError(errors.New("wowsa"), errors.New("egad"), exitErr, exitErr2)
|
||||||
HandleExitCoder(err)
|
HandleExitCoder(err)
|
||||||
|
|
||||||
expect(t, exitCode, 11)
|
expect(t, exitCode, 11)
|
||||||
@ -95,7 +95,7 @@ func TestHandleExitCoder_ErrorWithFormat(t *testing.T) {
|
|||||||
ErrWriter = fakeErrWriter
|
ErrWriter = fakeErrWriter
|
||||||
}()
|
}()
|
||||||
|
|
||||||
err := NewExitError(NewErrorWithFormat("I am formatted"), 1)
|
err := Exit(NewErrorWithFormat("I am formatted"), 1)
|
||||||
HandleExitCoder(err)
|
HandleExitCoder(err)
|
||||||
|
|
||||||
expect(t, called, true)
|
expect(t, called, true)
|
||||||
@ -114,7 +114,7 @@ func TestHandleExitCoder_MultiErrorWithFormat(t *testing.T) {
|
|||||||
|
|
||||||
defer func() { OsExiter = fakeOsExiter }()
|
defer func() { OsExiter = fakeOsExiter }()
|
||||||
|
|
||||||
err := NewMultiError(NewErrorWithFormat("err1"), NewErrorWithFormat("err2"))
|
err := newMultiError(NewErrorWithFormat("err1"), NewErrorWithFormat("err2"))
|
||||||
HandleExitCoder(err)
|
HandleExitCoder(err)
|
||||||
|
|
||||||
expect(t, called, true)
|
expect(t, called, true)
|
||||||
|
14
fish.go
14
fish.go
@ -64,11 +64,9 @@ func (a *App) writeFishCompletionTemplate(w io.Writer) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) prepareFishCommands(commands []Command, allCommands *[]string, previousCommands []string) []string {
|
func (a *App) prepareFishCommands(commands []*Command, allCommands *[]string, previousCommands []string) []string {
|
||||||
completions := []string{}
|
completions := []string{}
|
||||||
for i := range commands {
|
for _, command := range commands {
|
||||||
command := &commands[i]
|
|
||||||
|
|
||||||
if command.Hidden {
|
if command.Hidden {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -131,7 +129,7 @@ func (a *App) prepareFishFlags(flags []Flag, previousCommands []string) []string
|
|||||||
|
|
||||||
fishAddFileFlag(f, completion)
|
fishAddFileFlag(f, completion)
|
||||||
|
|
||||||
for idx, opt := range strings.Split(flag.GetName(), ",") {
|
for idx, opt := range flag.Names() {
|
||||||
if idx == 0 {
|
if idx == 0 {
|
||||||
completion.WriteString(fmt.Sprintf(
|
completion.WriteString(fmt.Sprintf(
|
||||||
" -l %s", strings.TrimSpace(opt),
|
" -l %s", strings.TrimSpace(opt),
|
||||||
@ -161,15 +159,15 @@ func (a *App) prepareFishFlags(flags []Flag, previousCommands []string) []string
|
|||||||
|
|
||||||
func fishAddFileFlag(flag Flag, completion *strings.Builder) {
|
func fishAddFileFlag(flag Flag, completion *strings.Builder) {
|
||||||
switch f := flag.(type) {
|
switch f := flag.(type) {
|
||||||
case GenericFlag:
|
case *GenericFlag:
|
||||||
if f.TakesFile {
|
if f.TakesFile {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case StringFlag:
|
case *StringFlag:
|
||||||
if f.TakesFile {
|
if f.TakesFile {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case StringSliceFlag:
|
case *StringSliceFlag:
|
||||||
if f.TakesFile {
|
if f.TakesFile {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
236
flag.go
236
flag.go
@ -5,38 +5,53 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"regexp"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultPlaceholder = "value"
|
const defaultPlaceholder = "value"
|
||||||
|
|
||||||
|
var (
|
||||||
|
slPfx = fmt.Sprintf("sl:::%d:::", time.Now().UTC().UnixNano())
|
||||||
|
|
||||||
|
commaWhitespace = regexp.MustCompile("[, ]+.*")
|
||||||
|
)
|
||||||
|
|
||||||
// BashCompletionFlag enables bash-completion for all commands and subcommands
|
// BashCompletionFlag enables bash-completion for all commands and subcommands
|
||||||
var BashCompletionFlag Flag = BoolFlag{
|
var BashCompletionFlag Flag = &BoolFlag{
|
||||||
Name: "generate-bash-completion",
|
Name: "generate-bash-completion",
|
||||||
Hidden: true,
|
Hidden: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
// VersionFlag prints the version for the application
|
// VersionFlag prints the version for the application
|
||||||
var VersionFlag Flag = BoolFlag{
|
var VersionFlag Flag = &BoolFlag{
|
||||||
Name: "version, v",
|
Name: "version",
|
||||||
Usage: "print the version",
|
Aliases: []string{"v"},
|
||||||
|
Usage: "print the version",
|
||||||
}
|
}
|
||||||
|
|
||||||
// HelpFlag prints the help for all commands and subcommands
|
// HelpFlag prints the help for all commands and subcommands.
|
||||||
// Set to the zero value (BoolFlag{}) to disable flag -- keeps subcommand
|
// Set to nil to disable the flag. The subcommand
|
||||||
// unless HideHelp is set to true)
|
// will still be added unless HideHelp is set to true.
|
||||||
var HelpFlag Flag = BoolFlag{
|
var HelpFlag Flag = &BoolFlag{
|
||||||
Name: "help, h",
|
Name: "help",
|
||||||
Usage: "show help",
|
Aliases: []string{"h"},
|
||||||
|
Usage: "show help",
|
||||||
}
|
}
|
||||||
|
|
||||||
// FlagStringer converts a flag definition to a string. This is used by help
|
// FlagStringer converts a flag definition to a string. This is used by help
|
||||||
// to display a flag.
|
// to display a flag.
|
||||||
var FlagStringer FlagStringFunc = stringifyFlag
|
var FlagStringer FlagStringFunc = stringifyFlag
|
||||||
|
|
||||||
|
// Serializer is used to circumvent the limitations of flag.FlagSet.Set
|
||||||
|
type Serializer interface {
|
||||||
|
Serialize() string
|
||||||
|
}
|
||||||
|
|
||||||
// FlagNamePrefixer converts a full flag name and its placeholder into the help
|
// FlagNamePrefixer converts a full flag name and its placeholder into the help
|
||||||
// message flag prefix. This is used by the default FlagStringer.
|
// message flag prefix. This is used by the default FlagStringer.
|
||||||
var FlagNamePrefixer FlagNamePrefixFunc = prefixedNames
|
var FlagNamePrefixer FlagNamePrefixFunc = prefixedNames
|
||||||
@ -57,7 +72,12 @@ func (f FlagsByName) Len() int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (f FlagsByName) Less(i, j int) bool {
|
func (f FlagsByName) Less(i, j int) bool {
|
||||||
return lexicographicLess(f[i].GetName(), f[j].GetName())
|
if len(f[j].Names()) == 0 {
|
||||||
|
return false
|
||||||
|
} else if len(f[i].Names()) == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return lexicographicLess(f[i].Names()[0], f[j].Names()[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f FlagsByName) Swap(i, j int) {
|
func (f FlagsByName) Swap(i, j int) {
|
||||||
@ -70,8 +90,9 @@ func (f FlagsByName) Swap(i, j int) {
|
|||||||
type Flag interface {
|
type Flag interface {
|
||||||
fmt.Stringer
|
fmt.Stringer
|
||||||
// Apply Flag settings to the given flag set
|
// Apply Flag settings to the given flag set
|
||||||
Apply(*flag.FlagSet)
|
Apply(*flag.FlagSet) error
|
||||||
GetName() string
|
Names() []string
|
||||||
|
IsSet() bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// RequiredFlag is an interface that allows us to mark flags as required
|
// RequiredFlag is an interface that allows us to mark flags as required
|
||||||
@ -97,40 +118,18 @@ type DocGenerationFlag interface {
|
|||||||
GetValue() string
|
GetValue() string
|
||||||
}
|
}
|
||||||
|
|
||||||
// errorableFlag is an interface that allows us to return errors during apply
|
|
||||||
// it allows flags defined in this library to return errors in a fashion backwards compatible
|
|
||||||
// TODO remove in v2 and modify the existing Flag interface to return errors
|
|
||||||
type errorableFlag interface {
|
|
||||||
Flag
|
|
||||||
|
|
||||||
ApplyWithError(*flag.FlagSet) error
|
|
||||||
}
|
|
||||||
|
|
||||||
func flagSet(name string, flags []Flag) (*flag.FlagSet, error) {
|
func flagSet(name string, flags []Flag) (*flag.FlagSet, error) {
|
||||||
set := flag.NewFlagSet(name, flag.ContinueOnError)
|
set := flag.NewFlagSet(name, flag.ContinueOnError)
|
||||||
|
|
||||||
for _, f := range flags {
|
for _, f := range flags {
|
||||||
//TODO remove in v2 when errorableFlag is removed
|
if err := f.Apply(set); err != nil {
|
||||||
if ef, ok := f.(errorableFlag); ok {
|
return nil, err
|
||||||
if err := ef.ApplyWithError(set); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
f.Apply(set)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
set.SetOutput(ioutil.Discard)
|
set.SetOutput(ioutil.Discard)
|
||||||
return set, nil
|
return set, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func eachName(longName string, fn func(string)) {
|
|
||||||
parts := strings.Split(longName, ",")
|
|
||||||
for _, name := range parts {
|
|
||||||
name = strings.Trim(name, " ")
|
|
||||||
fn(name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func visibleFlags(fl []Flag) []Flag {
|
func visibleFlags(fl []Flag) []Flag {
|
||||||
var visible []Flag
|
var visible []Flag
|
||||||
for _, f := range fl {
|
for _, f := range fl {
|
||||||
@ -169,25 +168,27 @@ func unquoteUsage(usage string) (string, string) {
|
|||||||
return "", usage
|
return "", usage
|
||||||
}
|
}
|
||||||
|
|
||||||
func prefixedNames(fullName, placeholder string) string {
|
func prefixedNames(names []string, placeholder string) string {
|
||||||
var prefixed string
|
var prefixed string
|
||||||
parts := strings.Split(fullName, ",")
|
for i, name := range names {
|
||||||
for i, name := range parts {
|
if name == "" {
|
||||||
name = strings.Trim(name, " ")
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
prefixed += prefixFor(name) + name
|
prefixed += prefixFor(name) + name
|
||||||
if placeholder != "" {
|
if placeholder != "" {
|
||||||
prefixed += " " + placeholder
|
prefixed += " " + placeholder
|
||||||
}
|
}
|
||||||
if i < len(parts)-1 {
|
if i < len(names)-1 {
|
||||||
prefixed += ", "
|
prefixed += ", "
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return prefixed
|
return prefixed
|
||||||
}
|
}
|
||||||
|
|
||||||
func withEnvHint(envVar, str string) string {
|
func withEnvHint(envVars []string, str string) string {
|
||||||
envText := ""
|
envText := ""
|
||||||
if envVar != "" {
|
if envVars != nil && len(envVars) > 0 {
|
||||||
prefix := "$"
|
prefix := "$"
|
||||||
suffix := ""
|
suffix := ""
|
||||||
sep := ", $"
|
sep := ", $"
|
||||||
@ -196,11 +197,51 @@ func withEnvHint(envVar, str string) string {
|
|||||||
suffix = "%"
|
suffix = "%"
|
||||||
sep = "%, %"
|
sep = "%, %"
|
||||||
}
|
}
|
||||||
envText = " [" + prefix + strings.Join(strings.Split(envVar, ","), sep) + suffix + "]"
|
|
||||||
|
envText = fmt.Sprintf(" [%s%s%s]", prefix, strings.Join(envVars, sep), suffix)
|
||||||
}
|
}
|
||||||
return str + envText
|
return str + envText
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func flagNames(f Flag) []string {
|
||||||
|
var ret []string
|
||||||
|
|
||||||
|
name := flagStringField(f, "Name")
|
||||||
|
aliases := flagStringSliceField(f, "Aliases")
|
||||||
|
|
||||||
|
for _, part := range append([]string{name}, aliases...) {
|
||||||
|
// v1 -> v2 migration warning zone:
|
||||||
|
// Strip off anything after the first found comma or space, which
|
||||||
|
// *hopefully* makes it a tiny bit more obvious that unexpected behavior is
|
||||||
|
// caused by using the v1 form of stringly typed "Name".
|
||||||
|
ret = append(ret, commaWhitespace.ReplaceAllString(part, ""))
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func flagStringSliceField(f Flag, name string) []string {
|
||||||
|
fv := flagValue(f)
|
||||||
|
field := fv.FieldByName(name)
|
||||||
|
|
||||||
|
if field.IsValid() {
|
||||||
|
return field.Interface().([]string)
|
||||||
|
}
|
||||||
|
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func flagStringField(f Flag, name string) string {
|
||||||
|
fv := flagValue(f)
|
||||||
|
field := fv.FieldByName(name)
|
||||||
|
|
||||||
|
if field.IsValid() {
|
||||||
|
return field.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
func withFileHint(filePath, str string) string {
|
func withFileHint(filePath, str string) string {
|
||||||
fileText := ""
|
fileText := ""
|
||||||
if filePath != "" {
|
if filePath != "" {
|
||||||
@ -221,39 +262,27 @@ func stringifyFlag(f Flag) string {
|
|||||||
fv := flagValue(f)
|
fv := flagValue(f)
|
||||||
|
|
||||||
switch f.(type) {
|
switch f.(type) {
|
||||||
case IntSliceFlag:
|
case *IntSliceFlag:
|
||||||
return FlagFileHinter(
|
return withEnvHint(flagStringSliceField(f, "EnvVars"),
|
||||||
fv.FieldByName("FilePath").String(),
|
stringifyIntSliceFlag(f.(*IntSliceFlag)))
|
||||||
FlagEnvHinter(
|
case *Int64SliceFlag:
|
||||||
fv.FieldByName("EnvVar").String(),
|
return withEnvHint(flagStringSliceField(f, "EnvVars"),
|
||||||
stringifyIntSliceFlag(f.(IntSliceFlag)),
|
stringifyInt64SliceFlag(f.(*Int64SliceFlag)))
|
||||||
),
|
case *Float64SliceFlag:
|
||||||
)
|
return withEnvHint(flagStringSliceField(f, "EnvVars"),
|
||||||
case Int64SliceFlag:
|
stringifyFloat64SliceFlag(f.(*Float64SliceFlag)))
|
||||||
return FlagFileHinter(
|
case *StringSliceFlag:
|
||||||
fv.FieldByName("FilePath").String(),
|
return withEnvHint(flagStringSliceField(f, "EnvVars"),
|
||||||
FlagEnvHinter(
|
stringifyStringSliceFlag(f.(*StringSliceFlag)))
|
||||||
fv.FieldByName("EnvVar").String(),
|
|
||||||
stringifyInt64SliceFlag(f.(Int64SliceFlag)),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
case StringSliceFlag:
|
|
||||||
return FlagFileHinter(
|
|
||||||
fv.FieldByName("FilePath").String(),
|
|
||||||
FlagEnvHinter(
|
|
||||||
fv.FieldByName("EnvVar").String(),
|
|
||||||
stringifyStringSliceFlag(f.(StringSliceFlag)),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
placeholder, usage := unquoteUsage(fv.FieldByName("Usage").String())
|
placeholder, usage := unquoteUsage(fv.FieldByName("Usage").String())
|
||||||
|
|
||||||
needsPlaceholder := false
|
needsPlaceholder := false
|
||||||
defaultValueString := ""
|
defaultValueString := ""
|
||||||
|
val := fv.FieldByName("Value")
|
||||||
if val := fv.FieldByName("Value"); val.IsValid() {
|
if val.IsValid() {
|
||||||
needsPlaceholder = true
|
needsPlaceholder = val.Kind() != reflect.Bool
|
||||||
defaultValueString = fmt.Sprintf(" (default: %v)", val.Interface())
|
defaultValueString = fmt.Sprintf(" (default: %v)", val.Interface())
|
||||||
|
|
||||||
if val.Kind() == reflect.String && val.String() != "" {
|
if val.Kind() == reflect.String && val.String() != "" {
|
||||||
@ -261,6 +290,12 @@ func stringifyFlag(f Flag) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
helpText := fv.FieldByName("DefaultText")
|
||||||
|
if helpText.IsValid() && helpText.String() != "" {
|
||||||
|
needsPlaceholder = val.Kind() != reflect.Bool
|
||||||
|
defaultValueString = fmt.Sprintf(" (default: %s)", helpText.String())
|
||||||
|
}
|
||||||
|
|
||||||
if defaultValueString == " (default: )" {
|
if defaultValueString == " (default: )" {
|
||||||
defaultValueString = ""
|
defaultValueString = ""
|
||||||
}
|
}
|
||||||
@ -271,16 +306,11 @@ func stringifyFlag(f Flag) string {
|
|||||||
|
|
||||||
usageWithDefault := strings.TrimSpace(usage + defaultValueString)
|
usageWithDefault := strings.TrimSpace(usage + defaultValueString)
|
||||||
|
|
||||||
return FlagFileHinter(
|
return withEnvHint(flagStringSliceField(f, "EnvVars"),
|
||||||
fv.FieldByName("FilePath").String(),
|
fmt.Sprintf("%s\t%s", prefixedNames(f.Names(), placeholder), usageWithDefault))
|
||||||
FlagEnvHinter(
|
|
||||||
fv.FieldByName("EnvVar").String(),
|
|
||||||
FlagNamePrefixer(fv.FieldByName("Name").String(), placeholder)+"\t"+usageWithDefault,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func stringifyIntSliceFlag(f IntSliceFlag) string {
|
func stringifyIntSliceFlag(f *IntSliceFlag) string {
|
||||||
var defaultVals []string
|
var defaultVals []string
|
||||||
if f.Value != nil && len(f.Value.Value()) > 0 {
|
if f.Value != nil && len(f.Value.Value()) > 0 {
|
||||||
for _, i := range f.Value.Value() {
|
for _, i := range f.Value.Value() {
|
||||||
@ -288,10 +318,10 @@ func stringifyIntSliceFlag(f IntSliceFlag) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return stringifySliceFlag(f.Usage, f.Name, defaultVals)
|
return stringifySliceFlag(f.Usage, f.Names(), defaultVals)
|
||||||
}
|
}
|
||||||
|
|
||||||
func stringifyInt64SliceFlag(f Int64SliceFlag) string {
|
func stringifyInt64SliceFlag(f *Int64SliceFlag) string {
|
||||||
var defaultVals []string
|
var defaultVals []string
|
||||||
if f.Value != nil && len(f.Value.Value()) > 0 {
|
if f.Value != nil && len(f.Value.Value()) > 0 {
|
||||||
for _, i := range f.Value.Value() {
|
for _, i := range f.Value.Value() {
|
||||||
@ -299,10 +329,22 @@ func stringifyInt64SliceFlag(f Int64SliceFlag) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return stringifySliceFlag(f.Usage, f.Name, defaultVals)
|
return stringifySliceFlag(f.Usage, f.Names(), defaultVals)
|
||||||
}
|
}
|
||||||
|
|
||||||
func stringifyStringSliceFlag(f StringSliceFlag) string {
|
func stringifyFloat64SliceFlag(f *Float64SliceFlag) string {
|
||||||
|
var defaultVals []string
|
||||||
|
|
||||||
|
if f.Value != nil && len(f.Value.Value()) > 0 {
|
||||||
|
for _, i := range f.Value.Value() {
|
||||||
|
defaultVals = append(defaultVals, strings.TrimRight(strings.TrimRight(fmt.Sprintf("%f", i), "0"), "."))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return stringifySliceFlag(f.Usage, f.Names(), defaultVals)
|
||||||
|
}
|
||||||
|
|
||||||
|
func stringifyStringSliceFlag(f *StringSliceFlag) string {
|
||||||
var defaultVals []string
|
var defaultVals []string
|
||||||
if f.Value != nil && len(f.Value.Value()) > 0 {
|
if f.Value != nil && len(f.Value.Value()) > 0 {
|
||||||
for _, s := range f.Value.Value() {
|
for _, s := range f.Value.Value() {
|
||||||
@ -312,10 +354,10 @@ func stringifyStringSliceFlag(f StringSliceFlag) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return stringifySliceFlag(f.Usage, f.Name, defaultVals)
|
return stringifySliceFlag(f.Usage, f.Names(), defaultVals)
|
||||||
}
|
}
|
||||||
|
|
||||||
func stringifySliceFlag(usage, name string, defaultVals []string) string {
|
func stringifySliceFlag(usage string, names, defaultVals []string) string {
|
||||||
placeholder, usage := unquoteUsage(usage)
|
placeholder, usage := unquoteUsage(usage)
|
||||||
if placeholder == "" {
|
if placeholder == "" {
|
||||||
placeholder = defaultPlaceholder
|
placeholder = defaultPlaceholder
|
||||||
@ -326,15 +368,25 @@ func stringifySliceFlag(usage, name string, defaultVals []string) string {
|
|||||||
defaultVal = fmt.Sprintf(" (default: %s)", strings.Join(defaultVals, ", "))
|
defaultVal = fmt.Sprintf(" (default: %s)", strings.Join(defaultVals, ", "))
|
||||||
}
|
}
|
||||||
|
|
||||||
usageWithDefault := strings.TrimSpace(usage + defaultVal)
|
usageWithDefault := strings.TrimSpace(fmt.Sprintf("%s%s", usage, defaultVal))
|
||||||
return FlagNamePrefixer(name, placeholder) + "\t" + usageWithDefault
|
return fmt.Sprintf("%s\t%s", prefixedNames(names, placeholder), usageWithDefault)
|
||||||
}
|
}
|
||||||
|
|
||||||
func flagFromFileEnv(filePath, envName string) (val string, ok bool) {
|
func hasFlag(flags []Flag, fl Flag) bool {
|
||||||
for _, envVar := range strings.Split(envName, ",") {
|
for _, existing := range flags {
|
||||||
|
if fl == existing {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func flagFromEnvOrFile(envVars []string, filePath string) (val string, ok bool) {
|
||||||
|
for _, envVar := range envVars {
|
||||||
envVar = strings.TrimSpace(envVar)
|
envVar = strings.TrimSpace(envVar)
|
||||||
if envVal, ok := syscall.Getenv(envVar); ok {
|
if val, ok := syscall.Getenv(envVar); ok {
|
||||||
return envVal, true
|
return val, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, fileVar := range strings.Split(filePath, ",") {
|
for _, fileVar := range strings.Split(filePath, ",") {
|
||||||
|
93
flag_bool.go
93
flag_bool.go
@ -9,93 +9,90 @@ import (
|
|||||||
// BoolFlag is a flag with type bool
|
// BoolFlag is a flag with type bool
|
||||||
type BoolFlag struct {
|
type BoolFlag struct {
|
||||||
Name string
|
Name string
|
||||||
|
Aliases []string
|
||||||
Usage string
|
Usage string
|
||||||
EnvVar string
|
EnvVars []string
|
||||||
FilePath string
|
FilePath string
|
||||||
Required bool
|
Required bool
|
||||||
Hidden bool
|
Hidden bool
|
||||||
|
Value bool
|
||||||
|
DefaultText string
|
||||||
Destination *bool
|
Destination *bool
|
||||||
|
HasBeenSet bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsSet returns whether or not the flag has been set through env or file
|
||||||
|
func (f *BoolFlag) IsSet() bool {
|
||||||
|
return f.HasBeenSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns a readable representation of this value
|
// String returns a readable representation of this value
|
||||||
// (for usage defaults)
|
// (for usage defaults)
|
||||||
func (f BoolFlag) String() string {
|
func (f *BoolFlag) String() string {
|
||||||
return FlagStringer(f)
|
return FlagStringer(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetName returns the name of the flag
|
// Names returns the names of the flag
|
||||||
func (f BoolFlag) GetName() string {
|
func (f *BoolFlag) Names() []string {
|
||||||
return f.Name
|
return flagNames(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// IsRequired returns whether or not the flag is required
|
||||||
func (f BoolFlag) IsRequired() bool {
|
func (f *BoolFlag) IsRequired() bool {
|
||||||
return f.Required
|
return f.Required
|
||||||
}
|
}
|
||||||
|
|
||||||
// TakesValue returns true of the flag takes a value, otherwise false
|
// TakesValue returns true of the flag takes a value, otherwise false
|
||||||
func (f BoolFlag) TakesValue() bool {
|
func (f *BoolFlag) TakesValue() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUsage returns the usage string for the flag
|
// GetUsage returns the usage string for the flag
|
||||||
func (f BoolFlag) GetUsage() string {
|
func (f *BoolFlag) GetUsage() string {
|
||||||
return f.Usage
|
return f.Usage
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetValue returns the flags value as string representation and an empty
|
// GetValue returns the flags value as string representation and an empty
|
||||||
// string if the flag takes no value at all.
|
// string if the flag takes no value at all.
|
||||||
func (f BoolFlag) GetValue() string {
|
func (f *BoolFlag) GetValue() string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply populates the flag given the flag set and environment
|
||||||
|
func (f *BoolFlag) Apply(set *flag.FlagSet) error {
|
||||||
|
if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok {
|
||||||
|
if val != "" {
|
||||||
|
valBool, err := strconv.ParseBool(val)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not parse %q as bool value for flag %s: %s", val, f.Name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
f.Value = valBool
|
||||||
|
f.HasBeenSet = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, name := range f.Names() {
|
||||||
|
if f.Destination != nil {
|
||||||
|
set.BoolVar(f.Destination, name, f.Value, f.Usage)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
set.Bool(name, f.Value, f.Usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Bool looks up the value of a local BoolFlag, returns
|
// Bool looks up the value of a local BoolFlag, returns
|
||||||
// false if not found
|
// false if not found
|
||||||
func (c *Context) Bool(name string) bool {
|
func (c *Context) Bool(name string) bool {
|
||||||
return lookupBool(name, c.flagSet)
|
if fs := lookupFlagSet(name, c); fs != nil {
|
||||||
}
|
|
||||||
|
|
||||||
// GlobalBool looks up the value of a global BoolFlag, returns
|
|
||||||
// false if not found
|
|
||||||
func (c *Context) GlobalBool(name string) bool {
|
|
||||||
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
||||||
return lookupBool(name, fs)
|
return lookupBool(name, fs)
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply populates the flag given the flag set and environment
|
|
||||||
// Ignores errors
|
|
||||||
func (f BoolFlag) Apply(set *flag.FlagSet) {
|
|
||||||
_ = f.ApplyWithError(set)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ApplyWithError populates the flag given the flag set and environment
|
|
||||||
func (f BoolFlag) ApplyWithError(set *flag.FlagSet) error {
|
|
||||||
val := false
|
|
||||||
if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
|
||||||
if envVal == "" {
|
|
||||||
val = false
|
|
||||||
} else {
|
|
||||||
envValBool, err := strconv.ParseBool(envVal)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("could not parse %s as bool value for flag %s: %s", envVal, f.Name, err)
|
|
||||||
}
|
|
||||||
val = envValBool
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
eachName(f.Name, func(name string) {
|
|
||||||
if f.Destination != nil {
|
|
||||||
set.BoolVar(f.Destination, name, val, f.Usage)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
set.Bool(name, val, f.Usage)
|
|
||||||
})
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func lookupBool(name string, set *flag.FlagSet) bool {
|
func lookupBool(name string, set *flag.FlagSet) bool {
|
||||||
f := set.Lookup(name)
|
f := set.Lookup(name)
|
||||||
if f != nil {
|
if f != nil {
|
||||||
|
110
flag_bool_t.go
110
flag_bool_t.go
@ -1,110 +0,0 @@
|
|||||||
package cli
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
)
|
|
||||||
|
|
||||||
// BoolTFlag is a flag with type bool that is true by default
|
|
||||||
type BoolTFlag struct {
|
|
||||||
Name string
|
|
||||||
Usage string
|
|
||||||
EnvVar string
|
|
||||||
FilePath string
|
|
||||||
Required bool
|
|
||||||
Hidden bool
|
|
||||||
Destination *bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// String returns a readable representation of this value
|
|
||||||
// (for usage defaults)
|
|
||||||
func (f BoolTFlag) String() string {
|
|
||||||
return FlagStringer(f)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetName returns the name of the flag
|
|
||||||
func (f BoolTFlag) GetName() string {
|
|
||||||
return f.Name
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
|
||||||
func (f BoolTFlag) IsRequired() bool {
|
|
||||||
return f.Required
|
|
||||||
}
|
|
||||||
|
|
||||||
// TakesValue returns true of the flag takes a value, otherwise false
|
|
||||||
func (f BoolTFlag) TakesValue() bool {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetUsage returns the usage string for the flag
|
|
||||||
func (f BoolTFlag) GetUsage() string {
|
|
||||||
return f.Usage
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetValue returns the flags value as string representation and an empty
|
|
||||||
// string if the flag takes no value at all.
|
|
||||||
func (f BoolTFlag) GetValue() string {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
// BoolT looks up the value of a local BoolTFlag, returns
|
|
||||||
// false if not found
|
|
||||||
func (c *Context) BoolT(name string) bool {
|
|
||||||
return lookupBoolT(name, c.flagSet)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GlobalBoolT looks up the value of a global BoolTFlag, returns
|
|
||||||
// false if not found
|
|
||||||
func (c *Context) GlobalBoolT(name string) bool {
|
|
||||||
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
||||||
return lookupBoolT(name, fs)
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// Apply populates the flag given the flag set and environment
|
|
||||||
// Ignores errors
|
|
||||||
func (f BoolTFlag) Apply(set *flag.FlagSet) {
|
|
||||||
_ = f.ApplyWithError(set)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ApplyWithError populates the flag given the flag set and environment
|
|
||||||
func (f BoolTFlag) ApplyWithError(set *flag.FlagSet) error {
|
|
||||||
val := true
|
|
||||||
|
|
||||||
if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
|
||||||
if envVal == "" {
|
|
||||||
val = false
|
|
||||||
} else {
|
|
||||||
envValBool, err := strconv.ParseBool(envVal)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("could not parse %s as bool value for flag %s: %s", envVal, f.Name, err)
|
|
||||||
}
|
|
||||||
val = envValBool
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
eachName(f.Name, func(name string) {
|
|
||||||
if f.Destination != nil {
|
|
||||||
set.BoolVar(f.Destination, name, val, f.Usage)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
set.Bool(name, val, f.Usage)
|
|
||||||
})
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func lookupBoolT(name string, set *flag.FlagSet) bool {
|
|
||||||
f := set.Lookup(name)
|
|
||||||
if f != nil {
|
|
||||||
parsed, err := strconv.ParseBool(f.Value.String())
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return parsed
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
@ -9,90 +9,89 @@ import (
|
|||||||
// DurationFlag is a flag with type time.Duration (see https://golang.org/pkg/time/#ParseDuration)
|
// DurationFlag is a flag with type time.Duration (see https://golang.org/pkg/time/#ParseDuration)
|
||||||
type DurationFlag struct {
|
type DurationFlag struct {
|
||||||
Name string
|
Name string
|
||||||
|
Aliases []string
|
||||||
Usage string
|
Usage string
|
||||||
EnvVar string
|
EnvVars []string
|
||||||
FilePath string
|
FilePath string
|
||||||
Required bool
|
Required bool
|
||||||
Hidden bool
|
Hidden bool
|
||||||
Value time.Duration
|
Value time.Duration
|
||||||
|
DefaultText string
|
||||||
Destination *time.Duration
|
Destination *time.Duration
|
||||||
|
HasBeenSet bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsSet returns whether or not the flag has been set through env or file
|
||||||
|
func (f *DurationFlag) IsSet() bool {
|
||||||
|
return f.HasBeenSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns a readable representation of this value
|
// String returns a readable representation of this value
|
||||||
// (for usage defaults)
|
// (for usage defaults)
|
||||||
func (f DurationFlag) String() string {
|
func (f *DurationFlag) String() string {
|
||||||
return FlagStringer(f)
|
return FlagStringer(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetName returns the name of the flag
|
// Names returns the names of the flag
|
||||||
func (f DurationFlag) GetName() string {
|
func (f *DurationFlag) Names() []string {
|
||||||
return f.Name
|
return flagNames(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// IsRequired returns whether or not the flag is required
|
||||||
func (f DurationFlag) IsRequired() bool {
|
func (f *DurationFlag) IsRequired() bool {
|
||||||
return f.Required
|
return f.Required
|
||||||
}
|
}
|
||||||
|
|
||||||
// TakesValue returns true of the flag takes a value, otherwise false
|
// TakesValue returns true of the flag takes a value, otherwise false
|
||||||
func (f DurationFlag) TakesValue() bool {
|
func (f *DurationFlag) TakesValue() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUsage returns the usage string for the flag
|
// GetUsage returns the usage string for the flag
|
||||||
func (f DurationFlag) GetUsage() string {
|
func (f *DurationFlag) GetUsage() string {
|
||||||
return f.Usage
|
return f.Usage
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetValue returns the flags value as string representation and an empty
|
// GetValue returns the flags value as string representation and an empty
|
||||||
// string if the flag takes no value at all.
|
// string if the flag takes no value at all.
|
||||||
func (f DurationFlag) GetValue() string {
|
func (f *DurationFlag) GetValue() string {
|
||||||
return f.Value.String()
|
return f.Value.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply populates the flag given the flag set and environment
|
||||||
|
func (f *DurationFlag) Apply(set *flag.FlagSet) error {
|
||||||
|
if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok {
|
||||||
|
if val != "" {
|
||||||
|
valDuration, err := time.ParseDuration(val)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not parse %q as duration value for flag %s: %s", val, f.Name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
f.Value = valDuration
|
||||||
|
f.HasBeenSet = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, name := range f.Names() {
|
||||||
|
if f.Destination != nil {
|
||||||
|
set.DurationVar(f.Destination, name, f.Value, f.Usage)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
set.Duration(name, f.Value, f.Usage)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Duration looks up the value of a local DurationFlag, returns
|
// Duration looks up the value of a local DurationFlag, returns
|
||||||
// 0 if not found
|
// 0 if not found
|
||||||
func (c *Context) Duration(name string) time.Duration {
|
func (c *Context) Duration(name string) time.Duration {
|
||||||
return lookupDuration(name, c.flagSet)
|
if fs := lookupFlagSet(name, c); fs != nil {
|
||||||
}
|
|
||||||
|
|
||||||
// GlobalDuration looks up the value of a global DurationFlag, returns
|
|
||||||
// 0 if not found
|
|
||||||
func (c *Context) GlobalDuration(name string) time.Duration {
|
|
||||||
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
||||||
return lookupDuration(name, fs)
|
return lookupDuration(name, fs)
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply populates the flag given the flag set and environment
|
|
||||||
// Ignores errors
|
|
||||||
func (f DurationFlag) Apply(set *flag.FlagSet) {
|
|
||||||
_ = f.ApplyWithError(set)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ApplyWithError populates the flag given the flag set and environment
|
|
||||||
func (f DurationFlag) ApplyWithError(set *flag.FlagSet) error {
|
|
||||||
if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
|
||||||
envValDuration, err := time.ParseDuration(envVal)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("could not parse %s as duration for flag %s: %s", envVal, f.Name, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
f.Value = envValDuration
|
|
||||||
}
|
|
||||||
|
|
||||||
eachName(f.Name, func(name string) {
|
|
||||||
if f.Destination != nil {
|
|
||||||
set.DurationVar(f.Destination, name, f.Value, f.Usage)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
set.Duration(name, f.Value, f.Usage)
|
|
||||||
})
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func lookupDuration(name string, set *flag.FlagSet) time.Duration {
|
func lookupDuration(name string, set *flag.FlagSet) time.Duration {
|
||||||
f := set.Lookup(name)
|
f := set.Lookup(name)
|
||||||
if f != nil {
|
if f != nil {
|
||||||
|
@ -9,90 +9,90 @@ import (
|
|||||||
// Float64Flag is a flag with type float64
|
// Float64Flag is a flag with type float64
|
||||||
type Float64Flag struct {
|
type Float64Flag struct {
|
||||||
Name string
|
Name string
|
||||||
|
Aliases []string
|
||||||
Usage string
|
Usage string
|
||||||
EnvVar string
|
EnvVars []string
|
||||||
FilePath string
|
FilePath string
|
||||||
Required bool
|
Required bool
|
||||||
Hidden bool
|
Hidden bool
|
||||||
Value float64
|
Value float64
|
||||||
|
DefaultText string
|
||||||
Destination *float64
|
Destination *float64
|
||||||
|
HasBeenSet bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsSet returns whether or not the flag has been set through env or file
|
||||||
|
func (f *Float64Flag)IsSet() bool {
|
||||||
|
return f.HasBeenSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns a readable representation of this value
|
// String returns a readable representation of this value
|
||||||
// (for usage defaults)
|
// (for usage defaults)
|
||||||
func (f Float64Flag) String() string {
|
func (f *Float64Flag) String() string {
|
||||||
return FlagStringer(f)
|
return FlagStringer(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetName returns the name of the flag
|
// Names returns the names of the flag
|
||||||
func (f Float64Flag) GetName() string {
|
func (f *Float64Flag) Names() []string {
|
||||||
return f.Name
|
return flagNames(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// IsRequired returns whether or not the flag is required
|
||||||
func (f Float64Flag) IsRequired() bool {
|
func (f *Float64Flag) IsRequired() bool {
|
||||||
return f.Required
|
return f.Required
|
||||||
}
|
}
|
||||||
|
|
||||||
// TakesValue returns true of the flag takes a value, otherwise false
|
// TakesValue returns true of the flag takes a value, otherwise false
|
||||||
func (f Float64Flag) TakesValue() bool {
|
func (f *Float64Flag) TakesValue() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUsage returns the usage string for the flag
|
// GetUsage returns the usage string for the flag
|
||||||
func (f Float64Flag) GetUsage() string {
|
func (f *Float64Flag) GetUsage() string {
|
||||||
return f.Usage
|
return f.Usage
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetValue returns the flags value as string representation and an empty
|
// GetValue returns the flags value as string representation and an empty
|
||||||
// string if the flag takes no value at all.
|
// string if the flag takes no value at all.
|
||||||
func (f Float64Flag) GetValue() string {
|
func (f *Float64Flag) GetValue() string {
|
||||||
return fmt.Sprintf("%f", f.Value)
|
return fmt.Sprintf("%f", f.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply populates the flag given the flag set and environment
|
||||||
|
func (f *Float64Flag) Apply(set *flag.FlagSet) error {
|
||||||
|
if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok {
|
||||||
|
if val != "" {
|
||||||
|
valFloat, err := strconv.ParseFloat(val, 10)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not parse %q as float64 value for flag %s: %s", val, f.Name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
f.Value = valFloat
|
||||||
|
f.HasBeenSet = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, name := range f.Names() {
|
||||||
|
if f.Destination != nil {
|
||||||
|
set.Float64Var(f.Destination, name, f.Value, f.Usage)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
set.Float64(name, f.Value, f.Usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Float64 looks up the value of a local Float64Flag, returns
|
// Float64 looks up the value of a local Float64Flag, returns
|
||||||
// 0 if not found
|
// 0 if not found
|
||||||
func (c *Context) Float64(name string) float64 {
|
func (c *Context) Float64(name string) float64 {
|
||||||
return lookupFloat64(name, c.flagSet)
|
if fs := lookupFlagSet(name, c); fs != nil {
|
||||||
}
|
|
||||||
|
|
||||||
// GlobalFloat64 looks up the value of a global Float64Flag, returns
|
|
||||||
// 0 if not found
|
|
||||||
func (c *Context) GlobalFloat64(name string) float64 {
|
|
||||||
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
||||||
return lookupFloat64(name, fs)
|
return lookupFloat64(name, fs)
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply populates the flag given the flag set and environment
|
|
||||||
// Ignores errors
|
|
||||||
func (f Float64Flag) Apply(set *flag.FlagSet) {
|
|
||||||
_ = f.ApplyWithError(set)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ApplyWithError populates the flag given the flag set and environment
|
|
||||||
func (f Float64Flag) ApplyWithError(set *flag.FlagSet) error {
|
|
||||||
if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
|
||||||
envValFloat, err := strconv.ParseFloat(envVal, 10)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("could not parse %s as float64 value for flag %s: %s", envVal, f.Name, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
f.Value = envValFloat
|
|
||||||
}
|
|
||||||
|
|
||||||
eachName(f.Name, func(name string) {
|
|
||||||
if f.Destination != nil {
|
|
||||||
set.Float64Var(f.Destination, name, f.Value, f.Usage)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
set.Float64(name, f.Value, f.Usage)
|
|
||||||
})
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func lookupFloat64(name string, set *flag.FlagSet) float64 {
|
func lookupFloat64(name string, set *flag.FlagSet) float64 {
|
||||||
f := set.Lookup(name)
|
f := set.Lookup(name)
|
||||||
if f != nil {
|
if f != nil {
|
||||||
|
165
flag_float64_slice.go
Normal file
165
flag_float64_slice.go
Normal file
@ -0,0 +1,165 @@
|
|||||||
|
package cli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Float64Slice wraps []float64 to satisfy flag.Value
|
||||||
|
type Float64Slice struct {
|
||||||
|
slice []float64
|
||||||
|
hasBeenSet bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewFloat64Slice makes a *Float64Slice with default values
|
||||||
|
func NewFloat64Slice(defaults ...float64) *Float64Slice {
|
||||||
|
return &Float64Slice{slice: append([]float64{}, defaults...)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set parses the value into a float64 and appends it to the list of values
|
||||||
|
func (f *Float64Slice) Set(value string) error {
|
||||||
|
if !f.hasBeenSet {
|
||||||
|
f.slice = []float64{}
|
||||||
|
f.hasBeenSet = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(value, slPfx) {
|
||||||
|
// Deserializing assumes overwrite
|
||||||
|
_ = json.Unmarshal([]byte(strings.Replace(value, slPfx, "", 1)), &f.slice)
|
||||||
|
f.hasBeenSet = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp, err := strconv.ParseFloat(value, 64)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
f.slice = append(f.slice, tmp)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns a readable representation of this value (for usage defaults)
|
||||||
|
func (f *Float64Slice) String() string {
|
||||||
|
return fmt.Sprintf("%#v", f.slice)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serialize allows Float64Slice to fulfill Serializer
|
||||||
|
func (f *Float64Slice) Serialize() string {
|
||||||
|
jsonBytes, _ := json.Marshal(f.slice)
|
||||||
|
return fmt.Sprintf("%s%s", slPfx, string(jsonBytes))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Value returns the slice of float64s set by this flag
|
||||||
|
func (f *Float64Slice) Value() []float64 {
|
||||||
|
return f.slice
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get returns the slice of float64s set by this flag
|
||||||
|
func (f *Float64Slice) Get() interface{} {
|
||||||
|
return *f
|
||||||
|
}
|
||||||
|
|
||||||
|
// Float64SliceFlag is a flag with type *Float64Slice
|
||||||
|
type Float64SliceFlag struct {
|
||||||
|
Name string
|
||||||
|
Aliases []string
|
||||||
|
Usage string
|
||||||
|
EnvVars []string
|
||||||
|
FilePath string
|
||||||
|
Required bool
|
||||||
|
Hidden bool
|
||||||
|
Value *Float64Slice
|
||||||
|
DefaultText string
|
||||||
|
HasBeenSet bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsSet returns whether or not the flag has been set through env or file
|
||||||
|
func (f *Float64SliceFlag) IsSet() bool {
|
||||||
|
return f.HasBeenSet
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns a readable representation of this value
|
||||||
|
// (for usage defaults)
|
||||||
|
func (f *Float64SliceFlag) String() string {
|
||||||
|
return FlagStringer(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Names returns the names of the flag
|
||||||
|
func (f *Float64SliceFlag) Names() []string {
|
||||||
|
return flagNames(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsRequired returns whether or not the flag is required
|
||||||
|
func (f *Float64SliceFlag) IsRequired() bool {
|
||||||
|
return f.Required
|
||||||
|
}
|
||||||
|
|
||||||
|
// TakesValue returns true if the flag takes a value, otherwise false
|
||||||
|
func (f *Float64SliceFlag) TakesValue() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetUsage returns the usage string for the flag
|
||||||
|
func (f *Float64SliceFlag) GetUsage() string {
|
||||||
|
return f.Usage
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetValue returns the flags value as string representation and an empty
|
||||||
|
// string if the flag takes no value at all.
|
||||||
|
func (f *Float64SliceFlag) GetValue() string {
|
||||||
|
if f.Value != nil {
|
||||||
|
return f.Value.String()
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply populates the flag given the flag set and environment
|
||||||
|
func (f *Float64SliceFlag) Apply(set *flag.FlagSet) error {
|
||||||
|
if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok {
|
||||||
|
if val != "" {
|
||||||
|
f.Value = &Float64Slice{}
|
||||||
|
|
||||||
|
for _, s := range strings.Split(val, ",") {
|
||||||
|
if err := f.Value.Set(strings.TrimSpace(s)); err != nil {
|
||||||
|
return fmt.Errorf("could not parse %q as float64 slice value for flag %s: %s", f.Value, f.Name, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
f.HasBeenSet = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, name := range f.Names() {
|
||||||
|
if f.Value == nil {
|
||||||
|
f.Value = &Float64Slice{}
|
||||||
|
}
|
||||||
|
set.Var(f.Value, name, f.Usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Float64Slice looks up the value of a local Float64SliceFlag, returns
|
||||||
|
// nil if not found
|
||||||
|
func (c *Context) Float64Slice(name string) []float64 {
|
||||||
|
if fs := lookupFlagSet(name, c); fs != nil {
|
||||||
|
return lookupFloat64Slice(name, fs)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func lookupFloat64Slice(name string, set *flag.FlagSet) []float64 {
|
||||||
|
f := set.Lookup(name)
|
||||||
|
if f != nil {
|
||||||
|
parsed, err := (f.Value.(*Float64Slice)).Value(), error(nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return parsed
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
@ -13,45 +13,53 @@ type Generic interface {
|
|||||||
|
|
||||||
// GenericFlag is a flag with type Generic
|
// GenericFlag is a flag with type Generic
|
||||||
type GenericFlag struct {
|
type GenericFlag struct {
|
||||||
Name string
|
Name string
|
||||||
Usage string
|
Aliases []string
|
||||||
EnvVar string
|
Usage string
|
||||||
FilePath string
|
EnvVars []string
|
||||||
Required bool
|
FilePath string
|
||||||
Hidden bool
|
Required bool
|
||||||
TakesFile bool
|
Hidden bool
|
||||||
Value Generic
|
TakesFile bool
|
||||||
|
Value Generic
|
||||||
|
DefaultText string
|
||||||
|
HasBeenSet bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsSet returns whether or not the flag has been set through env or file
|
||||||
|
func (f *GenericFlag) IsSet() bool {
|
||||||
|
return f.HasBeenSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns a readable representation of this value
|
// String returns a readable representation of this value
|
||||||
// (for usage defaults)
|
// (for usage defaults)
|
||||||
func (f GenericFlag) String() string {
|
func (f *GenericFlag) String() string {
|
||||||
return FlagStringer(f)
|
return FlagStringer(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetName returns the name of the flag
|
// Names returns the names of the flag
|
||||||
func (f GenericFlag) GetName() string {
|
func (f *GenericFlag) Names() []string {
|
||||||
return f.Name
|
return flagNames(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// IsRequired returns whether or not the flag is required
|
||||||
func (f GenericFlag) IsRequired() bool {
|
func (f *GenericFlag) IsRequired() bool {
|
||||||
return f.Required
|
return f.Required
|
||||||
}
|
}
|
||||||
|
|
||||||
// TakesValue returns true of the flag takes a value, otherwise false
|
// TakesValue returns true of the flag takes a value, otherwise false
|
||||||
func (f GenericFlag) TakesValue() bool {
|
func (f *GenericFlag) TakesValue() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUsage returns the usage string for the flag
|
// GetUsage returns the usage string for the flag
|
||||||
func (f GenericFlag) GetUsage() string {
|
func (f *GenericFlag) GetUsage() string {
|
||||||
return f.Usage
|
return f.Usage
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetValue returns the flags value as string representation and an empty
|
// GetValue returns the flags value as string representation and an empty
|
||||||
// string if the flag takes no value at all.
|
// string if the flag takes no value at all.
|
||||||
func (f GenericFlag) GetValue() string {
|
func (f *GenericFlag) GetValue() string {
|
||||||
if f.Value != nil {
|
if f.Value != nil {
|
||||||
return f.Value.String()
|
return f.Value.String()
|
||||||
}
|
}
|
||||||
@ -60,24 +68,20 @@ func (f GenericFlag) GetValue() string {
|
|||||||
|
|
||||||
// Apply takes the flagset and calls Set on the generic flag with the value
|
// Apply takes the flagset and calls Set on the generic flag with the value
|
||||||
// provided by the user for parsing by the flag
|
// provided by the user for parsing by the flag
|
||||||
// Ignores parsing errors
|
func (f GenericFlag) Apply(set *flag.FlagSet) error {
|
||||||
func (f GenericFlag) Apply(set *flag.FlagSet) {
|
if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok {
|
||||||
_ = f.ApplyWithError(set)
|
if val != "" {
|
||||||
}
|
if err := f.Value.Set(val); err != nil {
|
||||||
|
return fmt.Errorf("could not parse %q as value for flag %s: %s", val, f.Name, err)
|
||||||
|
}
|
||||||
|
|
||||||
// ApplyWithError takes the flagset and calls Set on the generic flag with the value
|
f.HasBeenSet = true
|
||||||
// provided by the user for parsing by the flag
|
|
||||||
func (f GenericFlag) ApplyWithError(set *flag.FlagSet) error {
|
|
||||||
val := f.Value
|
|
||||||
if fileEnvVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
|
||||||
if err := val.Set(fileEnvVal); err != nil {
|
|
||||||
return fmt.Errorf("could not parse %s as value for flag %s: %s", fileEnvVal, f.Name, err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
eachName(f.Name, func(name string) {
|
for _, name := range f.Names() {
|
||||||
set.Var(f.Value, name, f.Usage)
|
set.Var(f.Value, name, f.Usage)
|
||||||
})
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -85,13 +89,7 @@ func (f GenericFlag) ApplyWithError(set *flag.FlagSet) error {
|
|||||||
// Generic looks up the value of a local GenericFlag, returns
|
// Generic looks up the value of a local GenericFlag, returns
|
||||||
// nil if not found
|
// nil if not found
|
||||||
func (c *Context) Generic(name string) interface{} {
|
func (c *Context) Generic(name string) interface{} {
|
||||||
return lookupGeneric(name, c.flagSet)
|
if fs := lookupFlagSet(name, c); fs != nil {
|
||||||
}
|
|
||||||
|
|
||||||
// GlobalGeneric looks up the value of a global GenericFlag, returns
|
|
||||||
// nil if not found
|
|
||||||
func (c *Context) GlobalGeneric(name string) interface{} {
|
|
||||||
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
||||||
return lookupGeneric(name, fs)
|
return lookupGeneric(name, fs)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
61
flag_int.go
61
flag_int.go
@ -9,70 +9,77 @@ import (
|
|||||||
// IntFlag is a flag with type int
|
// IntFlag is a flag with type int
|
||||||
type IntFlag struct {
|
type IntFlag struct {
|
||||||
Name string
|
Name string
|
||||||
|
Aliases []string
|
||||||
Usage string
|
Usage string
|
||||||
EnvVar string
|
EnvVars []string
|
||||||
FilePath string
|
FilePath string
|
||||||
Required bool
|
Required bool
|
||||||
Hidden bool
|
Hidden bool
|
||||||
Value int
|
Value int
|
||||||
|
DefaultText string
|
||||||
Destination *int
|
Destination *int
|
||||||
|
HasBeenSet bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsSet returns whether or not the flag has been set through env or file
|
||||||
|
func (f *IntFlag) IsSet() bool {
|
||||||
|
return f.HasBeenSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns a readable representation of this value
|
// String returns a readable representation of this value
|
||||||
// (for usage defaults)
|
// (for usage defaults)
|
||||||
func (f IntFlag) String() string {
|
func (f *IntFlag) String() string {
|
||||||
return FlagStringer(f)
|
return FlagStringer(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetName returns the name of the flag
|
// Names returns the names of the flag
|
||||||
func (f IntFlag) GetName() string {
|
func (f *IntFlag) Names() []string {
|
||||||
return f.Name
|
return flagNames(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// IsRequired returns whether or not the flag is required
|
||||||
func (f IntFlag) IsRequired() bool {
|
func (f *IntFlag) IsRequired() bool {
|
||||||
return f.Required
|
return f.Required
|
||||||
}
|
}
|
||||||
|
|
||||||
// TakesValue returns true of the flag takes a value, otherwise false
|
// TakesValue returns true of the flag takes a value, otherwise false
|
||||||
func (f IntFlag) TakesValue() bool {
|
func (f *IntFlag) TakesValue() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUsage returns the usage string for the flag
|
// GetUsage returns the usage string for the flag
|
||||||
func (f IntFlag) GetUsage() string {
|
func (f *IntFlag) GetUsage() string {
|
||||||
return f.Usage
|
return f.Usage
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetValue returns the flags value as string representation and an empty
|
// GetValue returns the flags value as string representation and an empty
|
||||||
// string if the flag takes no value at all.
|
// string if the flag takes no value at all.
|
||||||
func (f IntFlag) GetValue() string {
|
func (f *IntFlag) GetValue() string {
|
||||||
return fmt.Sprintf("%d", f.Value)
|
return fmt.Sprintf("%d", f.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply populates the flag given the flag set and environment
|
// Apply populates the flag given the flag set and environment
|
||||||
// Ignores errors
|
func (f *IntFlag) Apply(set *flag.FlagSet) error {
|
||||||
func (f IntFlag) Apply(set *flag.FlagSet) {
|
if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok {
|
||||||
_ = f.ApplyWithError(set)
|
if val != "" {
|
||||||
}
|
valInt, err := strconv.ParseInt(val, 0, 64)
|
||||||
|
|
||||||
// ApplyWithError populates the flag given the flag set and environment
|
if err != nil {
|
||||||
func (f IntFlag) ApplyWithError(set *flag.FlagSet) error {
|
return fmt.Errorf("could not parse %q as int value for flag %s: %s", val, f.Name, err)
|
||||||
if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
}
|
||||||
envValInt, err := strconv.ParseInt(envVal, 0, 64)
|
|
||||||
if err != nil {
|
f.Value = int(valInt)
|
||||||
return fmt.Errorf("could not parse %s as int value for flag %s: %s", envVal, f.Name, err)
|
f.HasBeenSet = true
|
||||||
}
|
}
|
||||||
f.Value = int(envValInt)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
eachName(f.Name, func(name string) {
|
for _, name := range f.Names() {
|
||||||
if f.Destination != nil {
|
if f.Destination != nil {
|
||||||
set.IntVar(f.Destination, name, f.Value, f.Usage)
|
set.IntVar(f.Destination, name, f.Value, f.Usage)
|
||||||
return
|
continue
|
||||||
}
|
}
|
||||||
set.Int(name, f.Value, f.Usage)
|
set.Int(name, f.Value, f.Usage)
|
||||||
})
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -80,13 +87,7 @@ func (f IntFlag) ApplyWithError(set *flag.FlagSet) error {
|
|||||||
// Int looks up the value of a local IntFlag, returns
|
// Int looks up the value of a local IntFlag, returns
|
||||||
// 0 if not found
|
// 0 if not found
|
||||||
func (c *Context) Int(name string) int {
|
func (c *Context) Int(name string) int {
|
||||||
return lookupInt(name, c.flagSet)
|
if fs := lookupFlagSet(name, c); fs != nil {
|
||||||
}
|
|
||||||
|
|
||||||
// GlobalInt looks up the value of a global IntFlag, returns
|
|
||||||
// 0 if not found
|
|
||||||
func (c *Context) GlobalInt(name string) int {
|
|
||||||
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
||||||
return lookupInt(name, fs)
|
return lookupInt(name, fs)
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
|
@ -9,85 +9,84 @@ import (
|
|||||||
// Int64Flag is a flag with type int64
|
// Int64Flag is a flag with type int64
|
||||||
type Int64Flag struct {
|
type Int64Flag struct {
|
||||||
Name string
|
Name string
|
||||||
|
Aliases []string
|
||||||
Usage string
|
Usage string
|
||||||
EnvVar string
|
EnvVars []string
|
||||||
FilePath string
|
FilePath string
|
||||||
Required bool
|
Required bool
|
||||||
Hidden bool
|
Hidden bool
|
||||||
Value int64
|
Value int64
|
||||||
|
DefaultText string
|
||||||
Destination *int64
|
Destination *int64
|
||||||
|
HasBeenSet bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsSet returns whether or not the flag has been set through env or file
|
||||||
|
func (f *Int64Flag) IsSet() bool {
|
||||||
|
return f.HasBeenSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns a readable representation of this value
|
// String returns a readable representation of this value
|
||||||
// (for usage defaults)
|
// (for usage defaults)
|
||||||
func (f Int64Flag) String() string {
|
func (f *Int64Flag) String() string {
|
||||||
return FlagStringer(f)
|
return FlagStringer(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetName returns the name of the flag
|
// Names returns the names of the flag
|
||||||
func (f Int64Flag) GetName() string {
|
func (f *Int64Flag) Names() []string {
|
||||||
return f.Name
|
return flagNames(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// IsRequired returns whether or not the flag is required
|
||||||
func (f Int64Flag) IsRequired() bool {
|
func (f *Int64Flag) IsRequired() bool {
|
||||||
return f.Required
|
return f.Required
|
||||||
}
|
}
|
||||||
|
|
||||||
// TakesValue returns true of the flag takes a value, otherwise false
|
// TakesValue returns true of the flag takes a value, otherwise false
|
||||||
func (f Int64Flag) TakesValue() bool {
|
func (f *Int64Flag) TakesValue() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUsage returns the usage string for the flag
|
// GetUsage returns the usage string for the flag
|
||||||
func (f Int64Flag) GetUsage() string {
|
func (f *Int64Flag) GetUsage() string {
|
||||||
return f.Usage
|
return f.Usage
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetValue returns the flags value as string representation and an empty
|
// GetValue returns the flags value as string representation and an empty
|
||||||
// string if the flag takes no value at all.
|
// string if the flag takes no value at all.
|
||||||
func (f Int64Flag) GetValue() string {
|
func (f *Int64Flag) GetValue() string {
|
||||||
return fmt.Sprintf("%d", f.Value)
|
return fmt.Sprintf("%d", f.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply populates the flag given the flag set and environment
|
// Apply populates the flag given the flag set and environment
|
||||||
// Ignores errors
|
func (f *Int64Flag) Apply(set *flag.FlagSet) error {
|
||||||
func (f Int64Flag) Apply(set *flag.FlagSet) {
|
if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok {
|
||||||
_ = f.ApplyWithError(set)
|
if val != "" {
|
||||||
}
|
valInt, err := strconv.ParseInt(val, 0, 64)
|
||||||
|
|
||||||
// ApplyWithError populates the flag given the flag set and environment
|
if err != nil {
|
||||||
func (f Int64Flag) ApplyWithError(set *flag.FlagSet) error {
|
return fmt.Errorf("could not parse %q as int value for flag %s: %s", val, f.Name, err)
|
||||||
if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
}
|
||||||
envValInt, err := strconv.ParseInt(envVal, 0, 64)
|
|
||||||
if err != nil {
|
f.Value = valInt
|
||||||
return fmt.Errorf("could not parse %s as int value for flag %s: %s", envVal, f.Name, err)
|
f.HasBeenSet = true
|
||||||
}
|
}
|
||||||
|
|
||||||
f.Value = envValInt
|
|
||||||
}
|
}
|
||||||
|
|
||||||
eachName(f.Name, func(name string) {
|
for _, name := range f.Names() {
|
||||||
if f.Destination != nil {
|
if f.Destination != nil {
|
||||||
set.Int64Var(f.Destination, name, f.Value, f.Usage)
|
set.Int64Var(f.Destination, name, f.Value, f.Usage)
|
||||||
return
|
continue
|
||||||
}
|
}
|
||||||
set.Int64(name, f.Value, f.Usage)
|
set.Int64(name, f.Value, f.Usage)
|
||||||
})
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Int64 looks up the value of a local Int64Flag, returns
|
// Int64 looks up the value of a local Int64Flag, returns
|
||||||
// 0 if not found
|
// 0 if not found
|
||||||
func (c *Context) Int64(name string) int64 {
|
func (c *Context) Int64(name string) int64 {
|
||||||
return lookupInt64(name, c.flagSet)
|
if fs := lookupFlagSet(name, c); fs != nil {
|
||||||
}
|
|
||||||
|
|
||||||
// GlobalInt64 looks up the value of a global Int64Flag, returns
|
|
||||||
// 0 if not found
|
|
||||||
func (c *Context) GlobalInt64(name string) int64 {
|
|
||||||
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
||||||
return lookupInt64(name, fs)
|
return lookupInt64(name, fs)
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
|
@ -1,69 +1,106 @@
|
|||||||
package cli
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Int64Slice is an opaque type for []int to satisfy flag.Value and flag.Getter
|
// Int64Slice wraps []int64 to satisfy flag.Value
|
||||||
type Int64Slice []int64
|
type Int64Slice struct {
|
||||||
|
slice []int64
|
||||||
|
hasBeenSet bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewInt64Slice makes an *Int64Slice with default values
|
||||||
|
func NewInt64Slice(defaults ...int64) *Int64Slice {
|
||||||
|
return &Int64Slice{slice: append([]int64{}, defaults...)}
|
||||||
|
}
|
||||||
|
|
||||||
// Set parses the value into an integer and appends it to the list of values
|
// Set parses the value into an integer and appends it to the list of values
|
||||||
func (f *Int64Slice) Set(value string) error {
|
func (i *Int64Slice) Set(value string) error {
|
||||||
tmp, err := strconv.ParseInt(value, 10, 64)
|
if !i.hasBeenSet {
|
||||||
|
i.slice = []int64{}
|
||||||
|
i.hasBeenSet = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(value, slPfx) {
|
||||||
|
// Deserializing assumes overwrite
|
||||||
|
_ = json.Unmarshal([]byte(strings.Replace(value, slPfx, "", 1)), &i.slice)
|
||||||
|
i.hasBeenSet = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp, err := strconv.ParseInt(value, 0, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
*f = append(*f, tmp)
|
|
||||||
|
i.slice = append(i.slice, tmp)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns a readable representation of this value (for usage defaults)
|
// String returns a readable representation of this value (for usage defaults)
|
||||||
func (f *Int64Slice) String() string {
|
func (i *Int64Slice) String() string {
|
||||||
return fmt.Sprintf("%#v", *f)
|
return fmt.Sprintf("%#v", i.slice)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serialize allows Int64Slice to fulfill Serializer
|
||||||
|
func (i *Int64Slice) Serialize() string {
|
||||||
|
jsonBytes, _ := json.Marshal(i.slice)
|
||||||
|
return fmt.Sprintf("%s%s", slPfx, string(jsonBytes))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Value returns the slice of ints set by this flag
|
// Value returns the slice of ints set by this flag
|
||||||
func (f *Int64Slice) Value() []int64 {
|
func (i *Int64Slice) Value() []int64 {
|
||||||
return *f
|
return i.slice
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the slice of ints set by this flag
|
// Get returns the slice of ints set by this flag
|
||||||
func (f *Int64Slice) Get() interface{} {
|
func (i *Int64Slice) Get() interface{} {
|
||||||
return *f
|
return *i
|
||||||
}
|
}
|
||||||
|
|
||||||
// Int64SliceFlag is a flag with type *Int64Slice
|
// Int64SliceFlag is a flag with type *Int64Slice
|
||||||
type Int64SliceFlag struct {
|
type Int64SliceFlag struct {
|
||||||
Name string
|
Name string
|
||||||
Usage string
|
Aliases []string
|
||||||
EnvVar string
|
Usage string
|
||||||
FilePath string
|
EnvVars []string
|
||||||
Required bool
|
FilePath string
|
||||||
Hidden bool
|
Required bool
|
||||||
Value *Int64Slice
|
Hidden bool
|
||||||
|
Value *Int64Slice
|
||||||
|
DefaultText string
|
||||||
|
HasBeenSet bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsSet returns whether or not the flag has been set through env or file
|
||||||
|
func (f *Int64SliceFlag) IsSet() bool {
|
||||||
|
return f.HasBeenSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns a readable representation of this value
|
// String returns a readable representation of this value
|
||||||
// (for usage defaults)
|
// (for usage defaults)
|
||||||
func (f Int64SliceFlag) String() string {
|
func (f *Int64SliceFlag) String() string {
|
||||||
return FlagStringer(f)
|
return FlagStringer(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetName returns the name of the flag
|
// Names returns the names of the flag
|
||||||
func (f Int64SliceFlag) GetName() string {
|
func (f *Int64SliceFlag) Names() []string {
|
||||||
return f.Name
|
return flagNames(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// IsRequired returns whether or not the flag is required
|
||||||
func (f Int64SliceFlag) IsRequired() bool {
|
func (f *Int64SliceFlag) IsRequired() bool {
|
||||||
return f.Required
|
return f.Required
|
||||||
}
|
}
|
||||||
|
|
||||||
// TakesValue returns true of the flag takes a value, otherwise false
|
// TakesValue returns true of the flag takes a value, otherwise false
|
||||||
func (f Int64SliceFlag) TakesValue() bool {
|
func (f *Int64SliceFlag) TakesValue() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,7 +111,7 @@ func (f Int64SliceFlag) GetUsage() string {
|
|||||||
|
|
||||||
// GetValue returns the flags value as string representation and an empty
|
// GetValue returns the flags value as string representation and an empty
|
||||||
// string if the flag takes no value at all.
|
// string if the flag takes no value at all.
|
||||||
func (f Int64SliceFlag) GetValue() string {
|
func (f *Int64SliceFlag) GetValue() string {
|
||||||
if f.Value != nil {
|
if f.Value != nil {
|
||||||
return f.Value.String()
|
return f.Value.String()
|
||||||
}
|
}
|
||||||
@ -82,34 +119,26 @@ func (f Int64SliceFlag) GetValue() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Apply populates the flag given the flag set and environment
|
// Apply populates the flag given the flag set and environment
|
||||||
// Ignores errors
|
func (f *Int64SliceFlag) Apply(set *flag.FlagSet) error {
|
||||||
func (f Int64SliceFlag) Apply(set *flag.FlagSet) {
|
if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok {
|
||||||
_ = f.ApplyWithError(set)
|
f.Value = &Int64Slice{}
|
||||||
}
|
|
||||||
|
|
||||||
// ApplyWithError populates the flag given the flag set and environment
|
for _, s := range strings.Split(val, ",") {
|
||||||
func (f Int64SliceFlag) ApplyWithError(set *flag.FlagSet) error {
|
if err := f.Value.Set(strings.TrimSpace(s)); err != nil {
|
||||||
if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
return fmt.Errorf("could not parse %q as int64 slice value for flag %s: %s", val, f.Name, err)
|
||||||
newVal := &Int64Slice{}
|
|
||||||
for _, s := range strings.Split(envVal, ",") {
|
|
||||||
s = strings.TrimSpace(s)
|
|
||||||
if err := newVal.Set(s); err != nil {
|
|
||||||
return fmt.Errorf("could not parse %s as int64 slice value for flag %s: %s", envVal, f.Name, err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if f.Value == nil {
|
|
||||||
f.Value = newVal
|
f.HasBeenSet = true
|
||||||
} else {
|
|
||||||
*f.Value = *newVal
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
eachName(f.Name, func(name string) {
|
for _, name := range f.Names() {
|
||||||
if f.Value == nil {
|
if f.Value == nil {
|
||||||
f.Value = &Int64Slice{}
|
f.Value = &Int64Slice{}
|
||||||
}
|
}
|
||||||
set.Var(f.Value, name, f.Usage)
|
set.Var(f.Value, name, f.Usage)
|
||||||
})
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,15 +148,6 @@ func (c *Context) Int64Slice(name string) []int64 {
|
|||||||
return lookupInt64Slice(name, c.flagSet)
|
return lookupInt64Slice(name, c.flagSet)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GlobalInt64Slice looks up the value of a global Int64SliceFlag, returns
|
|
||||||
// nil if not found
|
|
||||||
func (c *Context) GlobalInt64Slice(name string) []int64 {
|
|
||||||
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
||||||
return lookupInt64Slice(name, fs)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func lookupInt64Slice(name string, set *flag.FlagSet) []int64 {
|
func lookupInt64Slice(name string, set *flag.FlagSet) []int64 {
|
||||||
f := set.Lookup(name)
|
f := set.Lookup(name)
|
||||||
if f != nil {
|
if f != nil {
|
||||||
|
@ -1,69 +1,117 @@
|
|||||||
package cli
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// IntSlice is an opaque type for []int to satisfy flag.Value and flag.Getter
|
// IntSlice wraps []int to satisfy flag.Value
|
||||||
type IntSlice []int
|
type IntSlice struct {
|
||||||
|
slice []int
|
||||||
|
hasBeenSet bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewIntSlice makes an *IntSlice with default values
|
||||||
|
func NewIntSlice(defaults ...int) *IntSlice {
|
||||||
|
return &IntSlice{slice: append([]int{}, defaults...)}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Consistently have specific Set function for Int64 and Float64 ?
|
||||||
|
// SetInt directly adds an integer to the list of values
|
||||||
|
func (i *IntSlice) SetInt(value int) {
|
||||||
|
if !i.hasBeenSet {
|
||||||
|
i.slice = []int{}
|
||||||
|
i.hasBeenSet = true
|
||||||
|
}
|
||||||
|
|
||||||
|
i.slice = append(i.slice, value)
|
||||||
|
}
|
||||||
|
|
||||||
// Set parses the value into an integer and appends it to the list of values
|
// Set parses the value into an integer and appends it to the list of values
|
||||||
func (f *IntSlice) Set(value string) error {
|
func (i *IntSlice) Set(value string) error {
|
||||||
tmp, err := strconv.Atoi(value)
|
if !i.hasBeenSet {
|
||||||
|
i.slice = []int{}
|
||||||
|
i.hasBeenSet = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(value, slPfx) {
|
||||||
|
// Deserializing assumes overwrite
|
||||||
|
_ = json.Unmarshal([]byte(strings.Replace(value, slPfx, "", 1)), &i.slice)
|
||||||
|
i.hasBeenSet = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp, err := strconv.ParseInt(value, 0, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
*f = append(*f, tmp)
|
|
||||||
|
i.slice = append(i.slice, int(tmp))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns a readable representation of this value (for usage defaults)
|
// String returns a readable representation of this value (for usage defaults)
|
||||||
func (f *IntSlice) String() string {
|
func (i *IntSlice) String() string {
|
||||||
return fmt.Sprintf("%#v", *f)
|
return fmt.Sprintf("%#v", i.slice)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serialize allows IntSlice to fulfill Serializer
|
||||||
|
func (i *IntSlice) Serialize() string {
|
||||||
|
jsonBytes, _ := json.Marshal(i.slice)
|
||||||
|
return fmt.Sprintf("%s%s", slPfx, string(jsonBytes))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Value returns the slice of ints set by this flag
|
// Value returns the slice of ints set by this flag
|
||||||
func (f *IntSlice) Value() []int {
|
func (i *IntSlice) Value() []int {
|
||||||
return *f
|
return i.slice
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the slice of ints set by this flag
|
// Get returns the slice of ints set by this flag
|
||||||
func (f *IntSlice) Get() interface{} {
|
func (i *IntSlice) Get() interface{} {
|
||||||
return *f
|
return *i
|
||||||
}
|
}
|
||||||
|
|
||||||
// IntSliceFlag is a flag with type *IntSlice
|
// IntSliceFlag is a flag with type *IntSlice
|
||||||
type IntSliceFlag struct {
|
type IntSliceFlag struct {
|
||||||
Name string
|
Name string
|
||||||
Usage string
|
Aliases []string
|
||||||
EnvVar string
|
Usage string
|
||||||
FilePath string
|
EnvVars []string
|
||||||
Required bool
|
FilePath string
|
||||||
Hidden bool
|
Required bool
|
||||||
Value *IntSlice
|
Hidden bool
|
||||||
|
Value *IntSlice
|
||||||
|
DefaultText string
|
||||||
|
HasBeenSet bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsSet returns whether or not the flag has been set through env or file
|
||||||
|
func (f *IntSliceFlag) IsSet() bool {
|
||||||
|
return f.HasBeenSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns a readable representation of this value
|
// String returns a readable representation of this value
|
||||||
// (for usage defaults)
|
// (for usage defaults)
|
||||||
func (f IntSliceFlag) String() string {
|
func (f *IntSliceFlag) String() string {
|
||||||
return FlagStringer(f)
|
return FlagStringer(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetName returns the name of the flag
|
// Names returns the names of the flag
|
||||||
func (f IntSliceFlag) GetName() string {
|
func (f *IntSliceFlag) Names() []string {
|
||||||
return f.Name
|
return flagNames(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// IsRequired returns whether or not the flag is required
|
||||||
func (f IntSliceFlag) IsRequired() bool {
|
func (f *IntSliceFlag) IsRequired() bool {
|
||||||
return f.Required
|
return f.Required
|
||||||
}
|
}
|
||||||
|
|
||||||
// TakesValue returns true of the flag takes a value, otherwise false
|
// TakesValue returns true of the flag takes a value, otherwise false
|
||||||
func (f IntSliceFlag) TakesValue() bool {
|
func (f *IntSliceFlag) TakesValue() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -74,7 +122,7 @@ func (f IntSliceFlag) GetUsage() string {
|
|||||||
|
|
||||||
// GetValue returns the flags value as string representation and an empty
|
// GetValue returns the flags value as string representation and an empty
|
||||||
// string if the flag takes no value at all.
|
// string if the flag takes no value at all.
|
||||||
func (f IntSliceFlag) GetValue() string {
|
func (f *IntSliceFlag) GetValue() string {
|
||||||
if f.Value != nil {
|
if f.Value != nil {
|
||||||
return f.Value.String()
|
return f.Value.String()
|
||||||
}
|
}
|
||||||
@ -82,34 +130,25 @@ func (f IntSliceFlag) GetValue() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Apply populates the flag given the flag set and environment
|
// Apply populates the flag given the flag set and environment
|
||||||
// Ignores errors
|
func (f *IntSliceFlag) Apply(set *flag.FlagSet) error {
|
||||||
func (f IntSliceFlag) Apply(set *flag.FlagSet) {
|
if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok {
|
||||||
_ = f.ApplyWithError(set)
|
f.Value = &IntSlice{}
|
||||||
}
|
|
||||||
|
|
||||||
// ApplyWithError populates the flag given the flag set and environment
|
for _, s := range strings.Split(val, ",") {
|
||||||
func (f IntSliceFlag) ApplyWithError(set *flag.FlagSet) error {
|
if err := f.Value.Set(strings.TrimSpace(s)); err != nil {
|
||||||
if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
return fmt.Errorf("could not parse %q as int slice value for flag %s: %s", val, f.Name, err)
|
||||||
newVal := &IntSlice{}
|
|
||||||
for _, s := range strings.Split(envVal, ",") {
|
|
||||||
s = strings.TrimSpace(s)
|
|
||||||
if err := newVal.Set(s); err != nil {
|
|
||||||
return fmt.Errorf("could not parse %s as int slice value for flag %s: %s", envVal, f.Name, err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if f.Value == nil {
|
|
||||||
f.Value = newVal
|
f.HasBeenSet = true
|
||||||
} else {
|
|
||||||
*f.Value = *newVal
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
eachName(f.Name, func(name string) {
|
for _, name := range f.Names() {
|
||||||
if f.Value == nil {
|
if f.Value == nil {
|
||||||
f.Value = &IntSlice{}
|
f.Value = &IntSlice{}
|
||||||
}
|
}
|
||||||
set.Var(f.Value, name, f.Usage)
|
set.Var(f.Value, name, f.Usage)
|
||||||
})
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -117,14 +156,8 @@ func (f IntSliceFlag) ApplyWithError(set *flag.FlagSet) error {
|
|||||||
// IntSlice looks up the value of a local IntSliceFlag, returns
|
// IntSlice looks up the value of a local IntSliceFlag, returns
|
||||||
// nil if not found
|
// nil if not found
|
||||||
func (c *Context) IntSlice(name string) []int {
|
func (c *Context) IntSlice(name string) []int {
|
||||||
return lookupIntSlice(name, c.flagSet)
|
if fs := lookupFlagSet(name, c); fs != nil {
|
||||||
}
|
return lookupIntSlice(name, c.flagSet)
|
||||||
|
|
||||||
// GlobalIntSlice looks up the value of a global IntSliceFlag, returns
|
|
||||||
// nil if not found
|
|
||||||
func (c *Context) GlobalIntSlice(name string) []int {
|
|
||||||
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
||||||
return lookupIntSlice(name, fs)
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
95
flag_path.go
Normal file
95
flag_path.go
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
package cli
|
||||||
|
|
||||||
|
import "flag"
|
||||||
|
|
||||||
|
type PathFlag struct {
|
||||||
|
Name string
|
||||||
|
Aliases []string
|
||||||
|
Usage string
|
||||||
|
EnvVars []string
|
||||||
|
FilePath string
|
||||||
|
Required bool
|
||||||
|
Hidden bool
|
||||||
|
TakesFile bool
|
||||||
|
Value string
|
||||||
|
DefaultText string
|
||||||
|
Destination *string
|
||||||
|
HasBeenSet bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsSet returns whether or not the flag has been set through env or file
|
||||||
|
func (f *PathFlag) IsSet() bool {
|
||||||
|
return f.HasBeenSet
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns a readable representation of this value
|
||||||
|
// (for usage defaults)
|
||||||
|
func (f *PathFlag) String() string {
|
||||||
|
return FlagStringer(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Names returns the names of the flag
|
||||||
|
func (f *PathFlag) Names() []string {
|
||||||
|
return flagNames(f)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsRequired returns whether or not the flag is required
|
||||||
|
func (f *PathFlag) IsRequired() bool {
|
||||||
|
return f.Required
|
||||||
|
}
|
||||||
|
|
||||||
|
// TakesValue returns true of the flag takes a value, otherwise false
|
||||||
|
func (f *PathFlag) TakesValue() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetUsage returns the usage string for the flag
|
||||||
|
func (f *PathFlag) GetUsage() string {
|
||||||
|
return f.Usage
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetValue returns the flags value as string representation and an empty
|
||||||
|
// string if the flag takes no value at all.
|
||||||
|
func (f *PathFlag) GetValue() string {
|
||||||
|
return f.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply populates the flag given the flag set and environment
|
||||||
|
func (f *PathFlag) Apply(set *flag.FlagSet) error {
|
||||||
|
if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok {
|
||||||
|
f.Value = val
|
||||||
|
f.HasBeenSet = true
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, name := range f.Names() {
|
||||||
|
if f.Destination != nil {
|
||||||
|
set.StringVar(f.Destination, name, f.Value, f.Usage)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
set.String(name, f.Value, f.Usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// String looks up the value of a local PathFlag, returns
|
||||||
|
// "" if not found
|
||||||
|
func (c *Context) Path(name string) string {
|
||||||
|
if fs := lookupFlagSet(name, c); fs != nil {
|
||||||
|
return lookupPath(name, fs)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func lookupPath(name string, set *flag.FlagSet) string {
|
||||||
|
f := set.Lookup(name)
|
||||||
|
if f != nil {
|
||||||
|
parsed, err := f.Value.String(), error(nil)
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return parsed
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
@ -5,67 +5,70 @@ import "flag"
|
|||||||
// StringFlag is a flag with type string
|
// StringFlag is a flag with type string
|
||||||
type StringFlag struct {
|
type StringFlag struct {
|
||||||
Name string
|
Name string
|
||||||
|
Aliases []string
|
||||||
Usage string
|
Usage string
|
||||||
EnvVar string
|
EnvVars []string
|
||||||
FilePath string
|
FilePath string
|
||||||
Required bool
|
Required bool
|
||||||
Hidden bool
|
Hidden bool
|
||||||
TakesFile bool
|
TakesFile bool
|
||||||
Value string
|
Value string
|
||||||
|
DefaultText string
|
||||||
Destination *string
|
Destination *string
|
||||||
|
HasBeenSet bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsSet returns whether or not the flag has been set through env or file
|
||||||
|
func (f *StringFlag) IsSet() bool {
|
||||||
|
return f.HasBeenSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns a readable representation of this value
|
// String returns a readable representation of this value
|
||||||
// (for usage defaults)
|
// (for usage defaults)
|
||||||
func (f StringFlag) String() string {
|
func (f *StringFlag) String() string {
|
||||||
return FlagStringer(f)
|
return FlagStringer(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetName returns the name of the flag
|
// Names returns the names of the flag
|
||||||
func (f StringFlag) GetName() string {
|
func (f *StringFlag) Names() []string {
|
||||||
return f.Name
|
return flagNames(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// IsRequired returns whether or not the flag is required
|
||||||
func (f StringFlag) IsRequired() bool {
|
func (f *StringFlag) IsRequired() bool {
|
||||||
return f.Required
|
return f.Required
|
||||||
}
|
}
|
||||||
|
|
||||||
// TakesValue returns true of the flag takes a value, otherwise false
|
// TakesValue returns true of the flag takes a value, otherwise false
|
||||||
func (f StringFlag) TakesValue() bool {
|
func (f *StringFlag) TakesValue() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUsage returns the usage string for the flag
|
// GetUsage returns the usage string for the flag
|
||||||
func (f StringFlag) GetUsage() string {
|
func (f *StringFlag) GetUsage() string {
|
||||||
return f.Usage
|
return f.Usage
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetValue returns the flags value as string representation and an empty
|
// GetValue returns the flags value as string representation and an empty
|
||||||
// string if the flag takes no value at all.
|
// string if the flag takes no value at all.
|
||||||
func (f StringFlag) GetValue() string {
|
func (f *StringFlag) GetValue() string {
|
||||||
return f.Value
|
return f.Value
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply populates the flag given the flag set and environment
|
// Apply populates the flag given the flag set and environment
|
||||||
// Ignores errors
|
func (f *StringFlag) Apply(set *flag.FlagSet) error {
|
||||||
func (f StringFlag) Apply(set *flag.FlagSet) {
|
if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok {
|
||||||
_ = f.ApplyWithError(set)
|
f.Value = val
|
||||||
}
|
f.HasBeenSet = true
|
||||||
|
|
||||||
// ApplyWithError populates the flag given the flag set and environment
|
|
||||||
func (f StringFlag) ApplyWithError(set *flag.FlagSet) error {
|
|
||||||
if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
|
||||||
f.Value = envVal
|
|
||||||
}
|
}
|
||||||
|
|
||||||
eachName(f.Name, func(name string) {
|
for _, name := range f.Names() {
|
||||||
if f.Destination != nil {
|
if f.Destination != nil {
|
||||||
set.StringVar(f.Destination, name, f.Value, f.Usage)
|
set.StringVar(f.Destination, name, f.Value, f.Usage)
|
||||||
return
|
continue
|
||||||
}
|
}
|
||||||
set.String(name, f.Value, f.Usage)
|
set.String(name, f.Value, f.Usage)
|
||||||
})
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -73,13 +76,7 @@ func (f StringFlag) ApplyWithError(set *flag.FlagSet) error {
|
|||||||
// String looks up the value of a local StringFlag, returns
|
// String looks up the value of a local StringFlag, returns
|
||||||
// "" if not found
|
// "" if not found
|
||||||
func (c *Context) String(name string) string {
|
func (c *Context) String(name string) string {
|
||||||
return lookupString(name, c.flagSet)
|
if fs := lookupFlagSet(name, c); fs != nil {
|
||||||
}
|
|
||||||
|
|
||||||
// GlobalString looks up the value of a global StringFlag, returns
|
|
||||||
// "" if not found
|
|
||||||
func (c *Context) GlobalString(name string) string {
|
|
||||||
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
||||||
return lookupString(name, fs)
|
return lookupString(name, fs)
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
|
@ -1,76 +1,112 @@
|
|||||||
package cli
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// StringSlice is an opaque type for []string to satisfy flag.Value and flag.Getter
|
// StringSlice wraps a []string to satisfy flag.Value
|
||||||
type StringSlice []string
|
type StringSlice struct {
|
||||||
|
slice []string
|
||||||
|
hasBeenSet bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewStringSlice creates a *StringSlice with default values
|
||||||
|
func NewStringSlice(defaults ...string) *StringSlice {
|
||||||
|
return &StringSlice{slice: append([]string{}, defaults...)}
|
||||||
|
}
|
||||||
|
|
||||||
// Set appends the string value to the list of values
|
// Set appends the string value to the list of values
|
||||||
func (f *StringSlice) Set(value string) error {
|
func (s *StringSlice) Set(value string) error {
|
||||||
*f = append(*f, value)
|
if !s.hasBeenSet {
|
||||||
|
s.slice = []string{}
|
||||||
|
s.hasBeenSet = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasPrefix(value, slPfx) {
|
||||||
|
// Deserializing assumes overwrite
|
||||||
|
_ = json.Unmarshal([]byte(strings.Replace(value, slPfx, "", 1)), &s.slice)
|
||||||
|
s.hasBeenSet = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
s.slice = append(s.slice, value)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns a readable representation of this value (for usage defaults)
|
// String returns a readable representation of this value (for usage defaults)
|
||||||
func (f *StringSlice) String() string {
|
func (s *StringSlice) String() string {
|
||||||
return fmt.Sprintf("%s", *f)
|
return fmt.Sprintf("%s", s.slice)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serialize allows StringSlice to fulfill Serializer
|
||||||
|
func (s *StringSlice) Serialize() string {
|
||||||
|
jsonBytes, _ := json.Marshal(s.slice)
|
||||||
|
return fmt.Sprintf("%s%s", slPfx, string(jsonBytes))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Value returns the slice of strings set by this flag
|
// Value returns the slice of strings set by this flag
|
||||||
func (f *StringSlice) Value() []string {
|
func (s *StringSlice) Value() []string {
|
||||||
return *f
|
return s.slice
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the slice of strings set by this flag
|
// Get returns the slice of strings set by this flag
|
||||||
func (f *StringSlice) Get() interface{} {
|
func (s *StringSlice) Get() interface{} {
|
||||||
return *f
|
return *s
|
||||||
}
|
}
|
||||||
|
|
||||||
// StringSliceFlag is a flag with type *StringSlice
|
// StringSliceFlag is a flag with type *StringSlice
|
||||||
type StringSliceFlag struct {
|
type StringSliceFlag struct {
|
||||||
Name string
|
Name string
|
||||||
Usage string
|
Aliases []string
|
||||||
EnvVar string
|
Usage string
|
||||||
FilePath string
|
EnvVars []string
|
||||||
Required bool
|
FilePath string
|
||||||
Hidden bool
|
Required bool
|
||||||
TakesFile bool
|
Hidden bool
|
||||||
Value *StringSlice
|
TakesFile bool
|
||||||
|
Value *StringSlice
|
||||||
|
DefaultText string
|
||||||
|
HasBeenSet bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsSet returns whether or not the flag has been set through env or file
|
||||||
|
func (f *StringSliceFlag) IsSet() bool {
|
||||||
|
return f.HasBeenSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns a readable representation of this value
|
// String returns a readable representation of this value
|
||||||
// (for usage defaults)
|
// (for usage defaults)
|
||||||
func (f StringSliceFlag) String() string {
|
func (f *StringSliceFlag) String() string {
|
||||||
return FlagStringer(f)
|
return FlagStringer(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetName returns the name of the flag
|
// Names returns the names of the flag
|
||||||
func (f StringSliceFlag) GetName() string {
|
func (f *StringSliceFlag) Names() []string {
|
||||||
return f.Name
|
return flagNames(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// IsRequired returns whether or not the flag is required
|
||||||
func (f StringSliceFlag) IsRequired() bool {
|
func (f *StringSliceFlag) IsRequired() bool {
|
||||||
return f.Required
|
return f.Required
|
||||||
}
|
}
|
||||||
|
|
||||||
// TakesValue returns true of the flag takes a value, otherwise false
|
// TakesValue returns true of the flag takes a value, otherwise false
|
||||||
func (f StringSliceFlag) TakesValue() bool {
|
func (f *StringSliceFlag) TakesValue() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUsage returns the usage string for the flag
|
// GetUsage returns the usage string for the flag
|
||||||
func (f StringSliceFlag) GetUsage() string {
|
func (f *StringSliceFlag) GetUsage() string {
|
||||||
return f.Usage
|
return f.Usage
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetValue returns the flags value as string representation and an empty
|
// GetValue returns the flags value as string representation and an empty
|
||||||
// string if the flag takes no value at all.
|
// string if the flag takes no value at all.
|
||||||
func (f StringSliceFlag) GetValue() string {
|
func (f *StringSliceFlag) GetValue() string {
|
||||||
if f.Value != nil {
|
if f.Value != nil {
|
||||||
return f.Value.String()
|
return f.Value.String()
|
||||||
}
|
}
|
||||||
@ -78,34 +114,25 @@ func (f StringSliceFlag) GetValue() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Apply populates the flag given the flag set and environment
|
// Apply populates the flag given the flag set and environment
|
||||||
// Ignores errors
|
func (f *StringSliceFlag) Apply(set *flag.FlagSet) error {
|
||||||
func (f StringSliceFlag) Apply(set *flag.FlagSet) {
|
if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok {
|
||||||
_ = f.ApplyWithError(set)
|
f.Value = &StringSlice{}
|
||||||
}
|
|
||||||
|
|
||||||
// ApplyWithError populates the flag given the flag set and environment
|
for _, s := range strings.Split(val, ",") {
|
||||||
func (f StringSliceFlag) ApplyWithError(set *flag.FlagSet) error {
|
if err := f.Value.Set(strings.TrimSpace(s)); err != nil {
|
||||||
if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
return fmt.Errorf("could not parse %q as string value for flag %s: %s", val, f.Name, err)
|
||||||
newVal := &StringSlice{}
|
|
||||||
for _, s := range strings.Split(envVal, ",") {
|
|
||||||
s = strings.TrimSpace(s)
|
|
||||||
if err := newVal.Set(s); err != nil {
|
|
||||||
return fmt.Errorf("could not parse %s as string value for flag %s: %s", envVal, f.Name, err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if f.Value == nil {
|
|
||||||
f.Value = newVal
|
f.HasBeenSet = true
|
||||||
} else {
|
|
||||||
*f.Value = *newVal
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
eachName(f.Name, func(name string) {
|
for _, name := range f.Names() {
|
||||||
if f.Value == nil {
|
if f.Value == nil {
|
||||||
f.Value = &StringSlice{}
|
f.Value = &StringSlice{}
|
||||||
}
|
}
|
||||||
set.Var(f.Value, name, f.Usage)
|
set.Var(f.Value, name, f.Usage)
|
||||||
})
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -113,13 +140,7 @@ func (f StringSliceFlag) ApplyWithError(set *flag.FlagSet) error {
|
|||||||
// StringSlice looks up the value of a local StringSliceFlag, returns
|
// StringSlice looks up the value of a local StringSliceFlag, returns
|
||||||
// nil if not found
|
// nil if not found
|
||||||
func (c *Context) StringSlice(name string) []string {
|
func (c *Context) StringSlice(name string) []string {
|
||||||
return lookupStringSlice(name, c.flagSet)
|
if fs := lookupFlagSet(name, c); fs != nil {
|
||||||
}
|
|
||||||
|
|
||||||
// GlobalStringSlice looks up the value of a global StringSliceFlag, returns
|
|
||||||
// nil if not found
|
|
||||||
func (c *Context) GlobalStringSlice(name string) []string {
|
|
||||||
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
||||||
return lookupStringSlice(name, fs)
|
return lookupStringSlice(name, fs)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
902
flag_test.go
902
flag_test.go
File diff suppressed because it is too large
Load Diff
61
flag_uint.go
61
flag_uint.go
@ -9,85 +9,84 @@ import (
|
|||||||
// UintFlag is a flag with type uint
|
// UintFlag is a flag with type uint
|
||||||
type UintFlag struct {
|
type UintFlag struct {
|
||||||
Name string
|
Name string
|
||||||
|
Aliases []string
|
||||||
Usage string
|
Usage string
|
||||||
EnvVar string
|
EnvVars []string
|
||||||
FilePath string
|
FilePath string
|
||||||
Required bool
|
Required bool
|
||||||
Hidden bool
|
Hidden bool
|
||||||
Value uint
|
Value uint
|
||||||
|
DefaultText string
|
||||||
Destination *uint
|
Destination *uint
|
||||||
|
HasBeenSet bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsSet returns whether or not the flag has been set through env or file
|
||||||
|
func (f *UintFlag) IsSet() bool {
|
||||||
|
return f.HasBeenSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns a readable representation of this value
|
// String returns a readable representation of this value
|
||||||
// (for usage defaults)
|
// (for usage defaults)
|
||||||
func (f UintFlag) String() string {
|
func (f *UintFlag) String() string {
|
||||||
return FlagStringer(f)
|
return FlagStringer(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetName returns the name of the flag
|
// Names returns the names of the flag
|
||||||
func (f UintFlag) GetName() string {
|
func (f *UintFlag) Names() []string {
|
||||||
return f.Name
|
return flagNames(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// IsRequired returns whether or not the flag is required
|
||||||
func (f UintFlag) IsRequired() bool {
|
func (f *UintFlag) IsRequired() bool {
|
||||||
return f.Required
|
return f.Required
|
||||||
}
|
}
|
||||||
|
|
||||||
// TakesValue returns true of the flag takes a value, otherwise false
|
// TakesValue returns true of the flag takes a value, otherwise false
|
||||||
func (f UintFlag) TakesValue() bool {
|
func (f *UintFlag) TakesValue() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUsage returns the usage string for the flag
|
// GetUsage returns the usage string for the flag
|
||||||
func (f UintFlag) GetUsage() string {
|
func (f *UintFlag) GetUsage() string {
|
||||||
return f.Usage
|
return f.Usage
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply populates the flag given the flag set and environment
|
// Apply populates the flag given the flag set and environment
|
||||||
// Ignores errors
|
func (f *UintFlag) Apply(set *flag.FlagSet) error {
|
||||||
func (f UintFlag) Apply(set *flag.FlagSet) {
|
if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok {
|
||||||
_ = f.ApplyWithError(set)
|
if val != "" {
|
||||||
}
|
valInt, err := strconv.ParseUint(val, 0, 64)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not parse %q as uint value for flag %s: %s", val, f.Name, err)
|
||||||
|
}
|
||||||
|
|
||||||
// ApplyWithError populates the flag given the flag set and environment
|
f.Value = uint(valInt)
|
||||||
func (f UintFlag) ApplyWithError(set *flag.FlagSet) error {
|
f.HasBeenSet = true
|
||||||
if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
|
||||||
envValInt, err := strconv.ParseUint(envVal, 0, 64)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("could not parse %s as uint value for flag %s: %s", envVal, f.Name, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
f.Value = uint(envValInt)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
eachName(f.Name, func(name string) {
|
for _, name := range f.Names() {
|
||||||
if f.Destination != nil {
|
if f.Destination != nil {
|
||||||
set.UintVar(f.Destination, name, f.Value, f.Usage)
|
set.UintVar(f.Destination, name, f.Value, f.Usage)
|
||||||
return
|
continue
|
||||||
}
|
}
|
||||||
set.Uint(name, f.Value, f.Usage)
|
set.Uint(name, f.Value, f.Usage)
|
||||||
})
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetValue returns the flags value as string representation and an empty
|
// GetValue returns the flags value as string representation and an empty
|
||||||
// string if the flag takes no value at all.
|
// string if the flag takes no value at all.
|
||||||
func (f UintFlag) GetValue() string {
|
func (f *UintFlag) GetValue() string {
|
||||||
return fmt.Sprintf("%d", f.Value)
|
return fmt.Sprintf("%d", f.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Uint looks up the value of a local UintFlag, returns
|
// Uint looks up the value of a local UintFlag, returns
|
||||||
// 0 if not found
|
// 0 if not found
|
||||||
func (c *Context) Uint(name string) uint {
|
func (c *Context) Uint(name string) uint {
|
||||||
return lookupUint(name, c.flagSet)
|
if fs := lookupFlagSet(name, c); fs != nil {
|
||||||
}
|
|
||||||
|
|
||||||
// GlobalUint looks up the value of a global UintFlag, returns
|
|
||||||
// 0 if not found
|
|
||||||
func (c *Context) GlobalUint(name string) uint {
|
|
||||||
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
||||||
return lookupUint(name, fs)
|
return lookupUint(name, fs)
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
|
@ -9,85 +9,84 @@ import (
|
|||||||
// Uint64Flag is a flag with type uint64
|
// Uint64Flag is a flag with type uint64
|
||||||
type Uint64Flag struct {
|
type Uint64Flag struct {
|
||||||
Name string
|
Name string
|
||||||
|
Aliases []string
|
||||||
Usage string
|
Usage string
|
||||||
EnvVar string
|
EnvVars []string
|
||||||
FilePath string
|
FilePath string
|
||||||
Required bool
|
Required bool
|
||||||
Hidden bool
|
Hidden bool
|
||||||
Value uint64
|
Value uint64
|
||||||
|
DefaultText string
|
||||||
Destination *uint64
|
Destination *uint64
|
||||||
|
HasBeenSet bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsSet returns whether or not the flag has been set through env or file
|
||||||
|
func (f *Uint64Flag) IsSet() bool {
|
||||||
|
return f.HasBeenSet
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns a readable representation of this value
|
// String returns a readable representation of this value
|
||||||
// (for usage defaults)
|
// (for usage defaults)
|
||||||
func (f Uint64Flag) String() string {
|
func (f *Uint64Flag) String() string {
|
||||||
return FlagStringer(f)
|
return FlagStringer(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetName returns the name of the flag
|
// Names returns the names of the flag
|
||||||
func (f Uint64Flag) GetName() string {
|
func (f *Uint64Flag) Names() []string {
|
||||||
return f.Name
|
return flagNames(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRequired returns whether or not the flag is required
|
// IsRequired returns whether or not the flag is required
|
||||||
func (f Uint64Flag) IsRequired() bool {
|
func (f *Uint64Flag) IsRequired() bool {
|
||||||
return f.Required
|
return f.Required
|
||||||
}
|
}
|
||||||
|
|
||||||
// TakesValue returns true of the flag takes a value, otherwise false
|
// TakesValue returns true of the flag takes a value, otherwise false
|
||||||
func (f Uint64Flag) TakesValue() bool {
|
func (f *Uint64Flag) TakesValue() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUsage returns the usage string for the flag
|
// GetUsage returns the usage string for the flag
|
||||||
func (f Uint64Flag) GetUsage() string {
|
func (f *Uint64Flag) GetUsage() string {
|
||||||
return f.Usage
|
return f.Usage
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply populates the flag given the flag set and environment
|
||||||
|
func (f *Uint64Flag) Apply(set *flag.FlagSet) error {
|
||||||
|
if val, ok := flagFromEnvOrFile(f.EnvVars, f.FilePath); ok {
|
||||||
|
if val != "" {
|
||||||
|
valInt, err := strconv.ParseUint(val, 0, 64)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("could not parse %q as uint64 value for flag %s: %s", val, f.Name, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
f.Value = valInt
|
||||||
|
f.HasBeenSet = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, name := range f.Names() {
|
||||||
|
if f.Destination != nil {
|
||||||
|
set.Uint64Var(f.Destination, name, f.Value, f.Usage)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
set.Uint64(name, f.Value, f.Usage)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetValue returns the flags value as string representation and an empty
|
// GetValue returns the flags value as string representation and an empty
|
||||||
// string if the flag takes no value at all.
|
// string if the flag takes no value at all.
|
||||||
func (f Uint64Flag) GetValue() string {
|
func (f *Uint64Flag) GetValue() string {
|
||||||
return fmt.Sprintf("%d", f.Value)
|
return fmt.Sprintf("%d", f.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Apply populates the flag given the flag set and environment
|
|
||||||
// Ignores errors
|
|
||||||
func (f Uint64Flag) Apply(set *flag.FlagSet) {
|
|
||||||
_ = f.ApplyWithError(set)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ApplyWithError populates the flag given the flag set and environment
|
|
||||||
func (f Uint64Flag) ApplyWithError(set *flag.FlagSet) error {
|
|
||||||
if envVal, ok := flagFromFileEnv(f.FilePath, f.EnvVar); ok {
|
|
||||||
envValInt, err := strconv.ParseUint(envVal, 0, 64)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("could not parse %s as uint64 value for flag %s: %s", envVal, f.Name, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
f.Value = envValInt
|
|
||||||
}
|
|
||||||
|
|
||||||
eachName(f.Name, func(name string) {
|
|
||||||
if f.Destination != nil {
|
|
||||||
set.Uint64Var(f.Destination, name, f.Value, f.Usage)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
set.Uint64(name, f.Value, f.Usage)
|
|
||||||
})
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Uint64 looks up the value of a local Uint64Flag, returns
|
// Uint64 looks up the value of a local Uint64Flag, returns
|
||||||
// 0 if not found
|
// 0 if not found
|
||||||
func (c *Context) Uint64(name string) uint64 {
|
func (c *Context) Uint64(name string) uint64 {
|
||||||
return lookupUint64(name, c.flagSet)
|
if fs := lookupFlagSet(name, c); fs != nil {
|
||||||
}
|
|
||||||
|
|
||||||
// GlobalUint64 looks up the value of a global Uint64Flag, returns
|
|
||||||
// 0 if not found
|
|
||||||
func (c *Context) GlobalUint64(name string) uint64 {
|
|
||||||
if fs := lookupGlobalFlagSet(name, c); fs != nil {
|
|
||||||
return lookupUint64(name, fs)
|
return lookupUint64(name, fs)
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
|
8
funcs.go
8
funcs.go
@ -1,6 +1,6 @@
|
|||||||
package cli
|
package cli
|
||||||
|
|
||||||
// BashCompleteFunc is an action to execute when the bash-completion flag is set
|
// BashCompleteFunc is an action to execute when the shell completion flag is set
|
||||||
type BashCompleteFunc func(*Context)
|
type BashCompleteFunc func(*Context)
|
||||||
|
|
||||||
// BeforeFunc is an action to execute before any subcommands are run, but after
|
// BeforeFunc is an action to execute before any subcommands are run, but after
|
||||||
@ -23,7 +23,7 @@ type CommandNotFoundFunc func(*Context, string)
|
|||||||
// is displayed and the execution is interrupted.
|
// is displayed and the execution is interrupted.
|
||||||
type OnUsageErrorFunc func(context *Context, err error, isSubcommand bool) error
|
type OnUsageErrorFunc func(context *Context, err error, isSubcommand bool) error
|
||||||
|
|
||||||
// ExitErrHandlerFunc is executed if provided in order to handle ExitError values
|
// ExitErrHandlerFunc is executed if provided in order to handle exitError values
|
||||||
// returned by Actions and Before/After functions.
|
// returned by Actions and Before/After functions.
|
||||||
type ExitErrHandlerFunc func(context *Context, err error)
|
type ExitErrHandlerFunc func(context *Context, err error)
|
||||||
|
|
||||||
@ -33,11 +33,11 @@ type FlagStringFunc func(Flag) string
|
|||||||
|
|
||||||
// FlagNamePrefixFunc is used by the default FlagStringFunc to create prefix
|
// FlagNamePrefixFunc is used by the default FlagStringFunc to create prefix
|
||||||
// text for a flag's full name.
|
// text for a flag's full name.
|
||||||
type FlagNamePrefixFunc func(fullName, placeholder string) string
|
type FlagNamePrefixFunc func(fullName []string, placeholder string) string
|
||||||
|
|
||||||
// FlagEnvHintFunc is used by the default FlagStringFunc to annotate flag help
|
// FlagEnvHintFunc is used by the default FlagStringFunc to annotate flag help
|
||||||
// with the environment variable details.
|
// with the environment variable details.
|
||||||
type FlagEnvHintFunc func(envVar, str string) string
|
type FlagEnvHintFunc func(envVars []string, str string) string
|
||||||
|
|
||||||
// FlagFileHintFunc is used by the default FlagStringFunc to annotate flag help
|
// FlagFileHintFunc is used by the default FlagStringFunc to annotate flag help
|
||||||
// with the file path details.
|
// with the file path details.
|
||||||
|
2
go.mod
2
go.mod
@ -1,4 +1,4 @@
|
|||||||
module github.com/urfave/cli
|
module github.com/urfave/cli/v2
|
||||||
|
|
||||||
go 1.11
|
go 1.11
|
||||||
|
|
||||||
|
47
help.go
47
help.go
@ -10,7 +10,7 @@ import (
|
|||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
)
|
)
|
||||||
|
|
||||||
var helpCommand = Command{
|
var helpCommand = &Command{
|
||||||
Name: "help",
|
Name: "help",
|
||||||
Aliases: []string{"h"},
|
Aliases: []string{"h"},
|
||||||
Usage: "Shows a list of commands or help for one command",
|
Usage: "Shows a list of commands or help for one command",
|
||||||
@ -26,7 +26,7 @@ var helpCommand = Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var helpSubcommand = Command{
|
var helpSubcommand = &Command{
|
||||||
Name: "help",
|
Name: "help",
|
||||||
Aliases: []string{"h"},
|
Aliases: []string{"h"},
|
||||||
Usage: "Shows a list of commands or help for one command",
|
Usage: "Shows a list of commands or help for one command",
|
||||||
@ -97,7 +97,7 @@ func DefaultAppComplete(c *Context) {
|
|||||||
DefaultCompleteWithFlags(nil)(c)
|
DefaultCompleteWithFlags(nil)(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func printCommandSuggestions(commands []Command, writer io.Writer) {
|
func printCommandSuggestions(commands []*Command, writer io.Writer) {
|
||||||
for _, command := range commands {
|
for _, command := range commands {
|
||||||
if command.Hidden {
|
if command.Hidden {
|
||||||
continue
|
continue
|
||||||
@ -135,10 +135,10 @@ func printFlagSuggestions(lastArg string, flags []Flag, writer io.Writer) {
|
|||||||
cur := strings.TrimPrefix(lastArg, "-")
|
cur := strings.TrimPrefix(lastArg, "-")
|
||||||
cur = strings.TrimPrefix(cur, "-")
|
cur = strings.TrimPrefix(cur, "-")
|
||||||
for _, flag := range flags {
|
for _, flag := range flags {
|
||||||
if bflag, ok := flag.(BoolFlag); ok && bflag.Hidden {
|
if bflag, ok := flag.(*BoolFlag); ok && bflag.Hidden {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for _, name := range strings.Split(flag.GetName(), ",") {
|
for _, name := range flag.Names(){
|
||||||
name = strings.TrimSpace(name)
|
name = strings.TrimSpace(name)
|
||||||
// this will get total count utf8 letters in flag name
|
// this will get total count utf8 letters in flag name
|
||||||
count := utf8.RuneCountInString(name)
|
count := utf8.RuneCountInString(name)
|
||||||
@ -151,7 +151,7 @@ func printFlagSuggestions(lastArg string, flags []Flag, writer io.Writer) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// match if last argument matches this flag and it is not repeated
|
// match if last argument matches this flag and it is not repeated
|
||||||
if strings.HasPrefix(name, cur) && cur != name && !cliArgContains(flag.GetName()) {
|
if strings.HasPrefix(name, cur) && cur != name && !cliArgContains(name) {
|
||||||
flagCompletion := fmt.Sprintf("%s%s", strings.Repeat("-", count), name)
|
flagCompletion := fmt.Sprintf("%s%s", strings.Repeat("-", count), name)
|
||||||
_, _ = fmt.Fprintln(writer, flagCompletion)
|
_, _ = fmt.Fprintln(writer, flagCompletion)
|
||||||
}
|
}
|
||||||
@ -207,7 +207,7 @@ func ShowCommandHelp(ctx *Context, command string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ctx.App.CommandNotFound == nil {
|
if ctx.App.CommandNotFound == nil {
|
||||||
return NewExitError(fmt.Sprintf("No help topic for '%v'", command), 3)
|
return Exit(fmt.Sprintf("No help topic for '%v'", command), 3)
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.App.CommandNotFound(ctx, command)
|
ctx.App.CommandNotFound(ctx, command)
|
||||||
@ -216,7 +216,15 @@ func ShowCommandHelp(ctx *Context, command string) error {
|
|||||||
|
|
||||||
// ShowSubcommandHelp prints help for the given subcommand
|
// ShowSubcommandHelp prints help for the given subcommand
|
||||||
func ShowSubcommandHelp(c *Context) error {
|
func ShowSubcommandHelp(c *Context) error {
|
||||||
return ShowCommandHelp(c, c.Command.Name)
|
if c == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.Command != nil {
|
||||||
|
return ShowCommandHelp(c, c.Command.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ShowCommandHelp(c, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
// ShowVersion prints the version number of the App
|
// ShowVersion prints the version number of the App
|
||||||
@ -263,6 +271,7 @@ func printHelpCustom(out io.Writer, templ string, data interface{}, customFuncs
|
|||||||
|
|
||||||
w := tabwriter.NewWriter(out, 1, 8, 2, ' ', 0)
|
w := tabwriter.NewWriter(out, 1, 8, 2, ' ', 0)
|
||||||
t := template.Must(template.New("help").Funcs(funcMap).Parse(templ))
|
t := template.Must(template.New("help").Funcs(funcMap).Parse(templ))
|
||||||
|
|
||||||
err := t.Execute(w, data)
|
err := t.Execute(w, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If the writer is closed, t.Execute will fail, and there's nothing
|
// If the writer is closed, t.Execute will fail, and there's nothing
|
||||||
@ -281,24 +290,20 @@ func printHelp(out io.Writer, templ string, data interface{}) {
|
|||||||
|
|
||||||
func checkVersion(c *Context) bool {
|
func checkVersion(c *Context) bool {
|
||||||
found := false
|
found := false
|
||||||
if VersionFlag.GetName() != "" {
|
for _, name := range VersionFlag.Names() {
|
||||||
eachName(VersionFlag.GetName(), func(name string) {
|
if c.Bool(name) {
|
||||||
if c.GlobalBool(name) || c.Bool(name) {
|
found = true
|
||||||
found = true
|
}
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
return found
|
return found
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkHelp(c *Context) bool {
|
func checkHelp(c *Context) bool {
|
||||||
found := false
|
found := false
|
||||||
if HelpFlag.GetName() != "" {
|
for _, name := range HelpFlag.Names() {
|
||||||
eachName(HelpFlag.GetName(), func(name string) {
|
if c.Bool(name) {
|
||||||
if c.GlobalBool(name) || c.Bool(name) {
|
found = true
|
||||||
found = true
|
}
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
return found
|
return found
|
||||||
}
|
}
|
||||||
@ -329,7 +334,7 @@ func checkShellCompleteFlag(a *App, arguments []string) (bool, []string) {
|
|||||||
pos := len(arguments) - 1
|
pos := len(arguments) - 1
|
||||||
lastArg := arguments[pos]
|
lastArg := arguments[pos]
|
||||||
|
|
||||||
if lastArg != "--"+BashCompletionFlag.GetName() {
|
if lastArg != "--generate-bash-completion" {
|
||||||
return false, arguments
|
return false, arguments
|
||||||
}
|
}
|
||||||
|
|
||||||
|
67
help_test.go
67
help_test.go
@ -12,8 +12,7 @@ import (
|
|||||||
|
|
||||||
func Test_ShowAppHelp_NoAuthor(t *testing.T) {
|
func Test_ShowAppHelp_NoAuthor(t *testing.T) {
|
||||||
output := new(bytes.Buffer)
|
output := new(bytes.Buffer)
|
||||||
app := NewApp()
|
app := &App{Writer: output}
|
||||||
app.Writer = output
|
|
||||||
|
|
||||||
c := NewContext(app, nil, nil)
|
c := NewContext(app, nil, nil)
|
||||||
|
|
||||||
@ -26,8 +25,7 @@ func Test_ShowAppHelp_NoAuthor(t *testing.T) {
|
|||||||
|
|
||||||
func Test_ShowAppHelp_NoVersion(t *testing.T) {
|
func Test_ShowAppHelp_NoVersion(t *testing.T) {
|
||||||
output := new(bytes.Buffer)
|
output := new(bytes.Buffer)
|
||||||
app := NewApp()
|
app := &App{Writer: output}
|
||||||
app.Writer = output
|
|
||||||
|
|
||||||
app.Version = ""
|
app.Version = ""
|
||||||
|
|
||||||
@ -42,8 +40,7 @@ func Test_ShowAppHelp_NoVersion(t *testing.T) {
|
|||||||
|
|
||||||
func Test_ShowAppHelp_HideVersion(t *testing.T) {
|
func Test_ShowAppHelp_HideVersion(t *testing.T) {
|
||||||
output := new(bytes.Buffer)
|
output := new(bytes.Buffer)
|
||||||
app := NewApp()
|
app := &App{Writer: output}
|
||||||
app.Writer = output
|
|
||||||
|
|
||||||
app.HideVersion = true
|
app.HideVersion = true
|
||||||
|
|
||||||
@ -62,14 +59,15 @@ func Test_Help_Custom_Flags(t *testing.T) {
|
|||||||
HelpFlag = oldFlag
|
HelpFlag = oldFlag
|
||||||
}()
|
}()
|
||||||
|
|
||||||
HelpFlag = BoolFlag{
|
HelpFlag = &BoolFlag{
|
||||||
Name: "help, x",
|
Name: "help",
|
||||||
Usage: "show help",
|
Aliases: []string{"x"},
|
||||||
|
Usage: "show help",
|
||||||
}
|
}
|
||||||
|
|
||||||
app := App{
|
app := App{
|
||||||
Flags: []Flag{
|
Flags: []Flag{
|
||||||
BoolFlag{Name: "foo, h"},
|
&BoolFlag{Name: "foo", Aliases: []string{"h"}},
|
||||||
},
|
},
|
||||||
Action: func(ctx *Context) error {
|
Action: func(ctx *Context) error {
|
||||||
if ctx.Bool("h") != true {
|
if ctx.Bool("h") != true {
|
||||||
@ -92,14 +90,15 @@ func Test_Version_Custom_Flags(t *testing.T) {
|
|||||||
VersionFlag = oldFlag
|
VersionFlag = oldFlag
|
||||||
}()
|
}()
|
||||||
|
|
||||||
VersionFlag = BoolFlag{
|
VersionFlag = &BoolFlag{
|
||||||
Name: "version, V",
|
Name: "version",
|
||||||
Usage: "show version",
|
Aliases: []string{"V"},
|
||||||
|
Usage: "show version",
|
||||||
}
|
}
|
||||||
|
|
||||||
app := App{
|
app := App{
|
||||||
Flags: []Flag{
|
Flags: []Flag{
|
||||||
BoolFlag{Name: "foo, v"},
|
&BoolFlag{Name: "foo", Aliases: []string{"v"}},
|
||||||
},
|
},
|
||||||
Action: func(ctx *Context) error {
|
Action: func(ctx *Context) error {
|
||||||
if ctx.Bool("v") != true {
|
if ctx.Bool("v") != true {
|
||||||
@ -117,22 +116,22 @@ func Test_Version_Custom_Flags(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_helpCommand_Action_ErrorIfNoTopic(t *testing.T) {
|
func Test_helpCommand_Action_ErrorIfNoTopic(t *testing.T) {
|
||||||
app := NewApp()
|
app := &App{}
|
||||||
|
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
_ = set.Parse([]string{"foo"})
|
_ = set.Parse([]string{"foo"})
|
||||||
|
|
||||||
c := NewContext(app, set, nil)
|
c := NewContext(app, set, nil)
|
||||||
|
|
||||||
err := helpCommand.Action.(func(*Context) error)(c)
|
err := helpCommand.Action(c)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("expected error from helpCommand.Action(), but got nil")
|
t.Fatalf("expected error from helpCommand.Action(), but got nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
exitErr, ok := err.(*ExitError)
|
exitErr, ok := err.(*exitError)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatalf("expected ExitError from helpCommand.Action(), but instead got: %v", err.Error())
|
t.Fatalf("expected *exitError from helpCommand.Action(), but instead got: %v", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.HasPrefix(exitErr.Error(), "No help topic for") {
|
if !strings.HasPrefix(exitErr.Error(), "No help topic for") {
|
||||||
@ -145,7 +144,7 @@ func Test_helpCommand_Action_ErrorIfNoTopic(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_helpCommand_InHelpOutput(t *testing.T) {
|
func Test_helpCommand_InHelpOutput(t *testing.T) {
|
||||||
app := NewApp()
|
app := &App{}
|
||||||
output := &bytes.Buffer{}
|
output := &bytes.Buffer{}
|
||||||
app.Writer = output
|
app.Writer = output
|
||||||
_ = app.Run([]string{"test", "--help"})
|
_ = app.Run([]string{"test", "--help"})
|
||||||
@ -162,22 +161,22 @@ func Test_helpCommand_InHelpOutput(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func Test_helpSubcommand_Action_ErrorIfNoTopic(t *testing.T) {
|
func Test_helpSubcommand_Action_ErrorIfNoTopic(t *testing.T) {
|
||||||
app := NewApp()
|
app := &App{}
|
||||||
|
|
||||||
set := flag.NewFlagSet("test", 0)
|
set := flag.NewFlagSet("test", 0)
|
||||||
_ = set.Parse([]string{"foo"})
|
_ = set.Parse([]string{"foo"})
|
||||||
|
|
||||||
c := NewContext(app, set, nil)
|
c := NewContext(app, set, nil)
|
||||||
|
|
||||||
err := helpSubcommand.Action.(func(*Context) error)(c)
|
err := helpSubcommand.Action(c)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("expected error from helpCommand.Action(), but got nil")
|
t.Fatalf("expected error from helpCommand.Action(), but got nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
exitErr, ok := err.(*ExitError)
|
exitErr, ok := err.(*exitError)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Fatalf("expected ExitError from helpCommand.Action(), but instead got: %v", err.Error())
|
t.Fatalf("expected *exitError from helpCommand.Action(), but instead got: %v", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
if !strings.HasPrefix(exitErr.Error(), "No help topic for") {
|
if !strings.HasPrefix(exitErr.Error(), "No help topic for") {
|
||||||
@ -191,7 +190,7 @@ func Test_helpSubcommand_Action_ErrorIfNoTopic(t *testing.T) {
|
|||||||
|
|
||||||
func TestShowAppHelp_CommandAliases(t *testing.T) {
|
func TestShowAppHelp_CommandAliases(t *testing.T) {
|
||||||
app := &App{
|
app := &App{
|
||||||
Commands: []Command{
|
Commands: []*Command{
|
||||||
{
|
{
|
||||||
Name: "frobbly",
|
Name: "frobbly",
|
||||||
Aliases: []string{"fr", "frob"},
|
Aliases: []string{"fr", "frob"},
|
||||||
@ -275,7 +274,7 @@ func TestShowCommandHelp_HelpPrinter(t *testing.T) {
|
|||||||
app := &App{
|
app := &App{
|
||||||
Name: "my-app",
|
Name: "my-app",
|
||||||
Writer: &buf,
|
Writer: &buf,
|
||||||
Commands: []Command{
|
Commands: []*Command{
|
||||||
{
|
{
|
||||||
Name: "my-command",
|
Name: "my-command",
|
||||||
CustomHelpTemplate: tt.template,
|
CustomHelpTemplate: tt.template,
|
||||||
@ -364,7 +363,7 @@ func TestShowCommandHelp_HelpPrinterCustom(t *testing.T) {
|
|||||||
app := &App{
|
app := &App{
|
||||||
Name: "my-app",
|
Name: "my-app",
|
||||||
Writer: &buf,
|
Writer: &buf,
|
||||||
Commands: []Command{
|
Commands: []*Command{
|
||||||
{
|
{
|
||||||
Name: "my-command",
|
Name: "my-command",
|
||||||
CustomHelpTemplate: tt.template,
|
CustomHelpTemplate: tt.template,
|
||||||
@ -387,7 +386,7 @@ func TestShowCommandHelp_HelpPrinterCustom(t *testing.T) {
|
|||||||
|
|
||||||
func TestShowCommandHelp_CommandAliases(t *testing.T) {
|
func TestShowCommandHelp_CommandAliases(t *testing.T) {
|
||||||
app := &App{
|
app := &App{
|
||||||
Commands: []Command{
|
Commands: []*Command{
|
||||||
{
|
{
|
||||||
Name: "frobbly",
|
Name: "frobbly",
|
||||||
Aliases: []string{"fr", "frob", "bork"},
|
Aliases: []string{"fr", "frob", "bork"},
|
||||||
@ -413,7 +412,7 @@ func TestShowCommandHelp_CommandAliases(t *testing.T) {
|
|||||||
|
|
||||||
func TestShowSubcommandHelp_CommandAliases(t *testing.T) {
|
func TestShowSubcommandHelp_CommandAliases(t *testing.T) {
|
||||||
app := &App{
|
app := &App{
|
||||||
Commands: []Command{
|
Commands: []*Command{
|
||||||
{
|
{
|
||||||
Name: "frobbly",
|
Name: "frobbly",
|
||||||
Aliases: []string{"fr", "frob", "bork"},
|
Aliases: []string{"fr", "frob", "bork"},
|
||||||
@ -435,7 +434,7 @@ func TestShowSubcommandHelp_CommandAliases(t *testing.T) {
|
|||||||
|
|
||||||
func TestShowCommandHelp_Customtemplate(t *testing.T) {
|
func TestShowCommandHelp_Customtemplate(t *testing.T) {
|
||||||
app := &App{
|
app := &App{
|
||||||
Commands: []Command{
|
Commands: []*Command{
|
||||||
{
|
{
|
||||||
Name: "frobbly",
|
Name: "frobbly",
|
||||||
Action: func(ctx *Context) error {
|
Action: func(ctx *Context) error {
|
||||||
@ -477,7 +476,7 @@ EXAMPLES:
|
|||||||
|
|
||||||
func TestShowSubcommandHelp_CommandUsageText(t *testing.T) {
|
func TestShowSubcommandHelp_CommandUsageText(t *testing.T) {
|
||||||
app := &App{
|
app := &App{
|
||||||
Commands: []Command{
|
Commands: []*Command{
|
||||||
{
|
{
|
||||||
Name: "frobbly",
|
Name: "frobbly",
|
||||||
UsageText: "this is usage text",
|
UsageText: "this is usage text",
|
||||||
@ -497,10 +496,10 @@ func TestShowSubcommandHelp_CommandUsageText(t *testing.T) {
|
|||||||
|
|
||||||
func TestShowSubcommandHelp_SubcommandUsageText(t *testing.T) {
|
func TestShowSubcommandHelp_SubcommandUsageText(t *testing.T) {
|
||||||
app := &App{
|
app := &App{
|
||||||
Commands: []Command{
|
Commands: []*Command{
|
||||||
{
|
{
|
||||||
Name: "frobbly",
|
Name: "frobbly",
|
||||||
Subcommands: []Command{
|
Subcommands: []*Command{
|
||||||
{
|
{
|
||||||
Name: "bobbly",
|
Name: "bobbly",
|
||||||
UsageText: "this is usage text",
|
UsageText: "this is usage text",
|
||||||
@ -521,7 +520,7 @@ func TestShowSubcommandHelp_SubcommandUsageText(t *testing.T) {
|
|||||||
|
|
||||||
func TestShowAppHelp_HiddenCommand(t *testing.T) {
|
func TestShowAppHelp_HiddenCommand(t *testing.T) {
|
||||||
app := &App{
|
app := &App{
|
||||||
Commands: []Command{
|
Commands: []*Command{
|
||||||
{
|
{
|
||||||
Name: "frobbly",
|
Name: "frobbly",
|
||||||
Action: func(ctx *Context) error {
|
Action: func(ctx *Context) error {
|
||||||
@ -691,7 +690,7 @@ func TestShowAppHelp_HelpPrinterCustom(t *testing.T) {
|
|||||||
|
|
||||||
func TestShowAppHelp_CustomAppTemplate(t *testing.T) {
|
func TestShowAppHelp_CustomAppTemplate(t *testing.T) {
|
||||||
app := &App{
|
app := &App{
|
||||||
Commands: []Command{
|
Commands: []*Command{
|
||||||
{
|
{
|
||||||
Name: "frobbly",
|
Name: "frobbly",
|
||||||
Action: func(ctx *Context) error {
|
Action: func(ctx *Context) error {
|
||||||
|
@ -12,6 +12,10 @@ var (
|
|||||||
wd, _ = os.Getwd()
|
wd, _ = os.Getwd()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
_ = os.Setenv("CLI_TEMPLATE_REPANIC", "1")
|
||||||
|
}
|
||||||
|
|
||||||
func expect(t *testing.T, a interface{}, b interface{}) {
|
func expect(t *testing.T, a interface{}, b interface{}) {
|
||||||
_, fn, line, _ := runtime.Caller(1)
|
_, fn, line, _ := runtime.Caller(1)
|
||||||
fn = strings.Replace(fn, wd+"/", "", -1)
|
fn = strings.Replace(fn, wd+"/", "", -1)
|
||||||
|
@ -1,9 +0,0 @@
|
|||||||
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
|
|
||||||
|
|
||||||
package cli
|
|
||||||
|
|
||||||
import "os"
|
|
||||||
|
|
||||||
func clearenv() {
|
|
||||||
os.Clearenv()
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
package cli
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"syscall"
|
|
||||||
)
|
|
||||||
|
|
||||||
// os.Clearenv() doesn't actually unset variables on Windows
|
|
||||||
// See: https://github.com/golang/go/issues/17902
|
|
||||||
func clearenv() {
|
|
||||||
for _, s := range os.Environ() {
|
|
||||||
for j := 1; j < len(s); j++ {
|
|
||||||
if s[j] == '=' {
|
|
||||||
keyp, _ := syscall.UTF16PtrFromString(s[0:j])
|
|
||||||
syscall.SetEnvironmentVariable(keyp, nil)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -20,7 +20,6 @@ AUTHOR{{with $length := len .Authors}}{{if ne 1 $length}}S{{end}}{{end}}:
|
|||||||
{{end}}{{$author}}{{end}}{{end}}{{if .VisibleCommands}}
|
{{end}}{{$author}}{{end}}{{end}}{{if .VisibleCommands}}
|
||||||
|
|
||||||
COMMANDS:{{range .VisibleCategories}}{{if .Name}}
|
COMMANDS:{{range .VisibleCategories}}{{if .Name}}
|
||||||
|
|
||||||
{{.Name}}:{{range .VisibleCommands}}
|
{{.Name}}:{{range .VisibleCommands}}
|
||||||
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{else}}{{range .VisibleCommands}}
|
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{else}}{{range .VisibleCommands}}
|
||||||
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{end}}{{end}}{{end}}{{if .VisibleFlags}}
|
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{end}}{{end}}{{end}}{{if .VisibleFlags}}
|
||||||
@ -63,7 +62,6 @@ USAGE:
|
|||||||
{{if .UsageText}}{{.UsageText}}{{else}}{{.HelpName}} command{{if .VisibleFlags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}
|
{{if .UsageText}}{{.UsageText}}{{else}}{{.HelpName}} command{{if .VisibleFlags}} [command options]{{end}} {{if .ArgsUsage}}{{.ArgsUsage}}{{else}}[arguments...]{{end}}{{end}}
|
||||||
|
|
||||||
COMMANDS:{{range .VisibleCategories}}{{if .Name}}
|
COMMANDS:{{range .VisibleCategories}}{{if .Name}}
|
||||||
|
|
||||||
{{.Name}}:{{range .VisibleCommands}}
|
{{.Name}}:{{range .VisibleCommands}}
|
||||||
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{else}}{{range .VisibleCommands}}
|
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{else}}{{range .VisibleCommands}}
|
||||||
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{end}}{{end}}{{if .VisibleFlags}}
|
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{end}}{{end}}{{if .VisibleFlags}}
|
||||||
@ -74,8 +72,9 @@ OPTIONS:
|
|||||||
`
|
`
|
||||||
|
|
||||||
var MarkdownDocTemplate = `% {{ .App.Name }}(8) {{ .App.Description }}
|
var MarkdownDocTemplate = `% {{ .App.Name }}(8) {{ .App.Description }}
|
||||||
|
{{ range $Author := .App.Authors}}
|
||||||
% {{ .App.Author }}
|
% {{ $Author.Name }}
|
||||||
|
{{- end}}
|
||||||
|
|
||||||
# NAME
|
# NAME
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user