Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: urfave/cli
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.20.2
Choose a base ref
...
head repository: urfave/cli
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v2.20.3
Choose a head ref
  • 10 commits
  • 5 files changed
  • 2 contributors

Commits on Oct 16, 2022

  1. Verified

    This commit was signed with the committer’s verified signature.
    kzaher Krunoslav Zaher
    Copy the full SHA
    e302525 View commit details

Commits on Oct 18, 2022

  1. Copy the full SHA
    da8868f View commit details
  2. Run goimports

    dearchap committed Oct 18, 2022
    Copy the full SHA
    f460fd1 View commit details

Commits on Oct 19, 2022

  1. Copy the full SHA
    8467e25 View commit details
  2. Update docs/v2/examples/flags.md

    Co-authored-by: Anatoli Babenia <anatoli@rainforce.org>
    dearchap and abitrolly authored Oct 19, 2022
    Copy the full SHA
    a5b62b8 View commit details
  3. Merge pull request #1539 from urfave/issue_1276

    Docs:(issue_1276) Make destination/value/default text clear
    dearchap authored Oct 19, 2022
    Copy the full SHA
    cf49ddb View commit details
  4. Revert flag_test.go

    dearchap committed Oct 19, 2022
    Copy the full SHA
    374bbfb View commit details
  5. Copy the full SHA
    96bff3c View commit details

Commits on Oct 22, 2022

  1. Merge pull request #1537 from dearchap/issue_1263

    Fix:(issue_1263) FlagNames should return names set via env as well
    dearchap authored Oct 22, 2022
    Copy the full SHA
    d1ac284 View commit details
  2. Copy the full SHA
    8ea10b8 View commit details
Showing with 46 additions and 16 deletions.
  1. +22 −2 context.go
  2. +2 −1 docs/v2/examples/flags.md
  3. +16 −11 flag.go
  4. +4 −0 flag_test.go
  5. +2 −2 sliceflag_test.go
24 changes: 22 additions & 2 deletions context.go
Original file line number Diff line number Diff line change
@@ -82,15 +82,35 @@ func (cCtx *Context) IsSet(name string) bool {
func (cCtx *Context) LocalFlagNames() []string {
var names []string
cCtx.flagSet.Visit(makeFlagNameVisitor(&names))
return names
// Check the flags which have been set via env or file
if cCtx.Command != nil && cCtx.Command.Flags != nil {
for _, f := range cCtx.Command.Flags {
if f.IsSet() {
names = append(names, f.Names()...)
}
}
}

// Sort out the duplicates since flag could be set via multiple
// paths
m := map[string]struct{}{}
var unames []string
for _, name := range names {
if _, ok := m[name]; !ok {
m[name] = struct{}{}
unames = append(unames, name)
}
}

return unames
}

// FlagNames returns a slice of flag names used by the this context and all of
// its parent contexts.
func (cCtx *Context) FlagNames() []string {
var names []string
for _, pCtx := range cCtx.Lineage() {
pCtx.flagSet.Visit(makeFlagNameVisitor(&names))
names = append(names, pCtx.LocalFlagNames()...)
}
return names
}
3 changes: 2 additions & 1 deletion docs/v2/examples/flags.md
Original file line number Diff line number Diff line change
@@ -51,7 +51,8 @@ func main() {
```

You can also set a destination variable for a flag, to which the content will be
scanned.
scanned. Note that if the `Value` is set for the flag, it will be shown as default,
and destination will be set to this value before parsing flag on the command line.

<!-- {
"output": "Hello someone"
27 changes: 16 additions & 11 deletions flag.go
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import (
"flag"
"fmt"
"io/ioutil"
"os"
"regexp"
"runtime"
"strings"
@@ -268,19 +269,23 @@ func prefixedNames(names []string, placeholder string) string {
return prefixed
}

func withEnvHint(envVars []string, str string) string {
envText := ""
func envFormat(envVars []string, prefix, sep, suffix string) string {
if len(envVars) > 0 {
prefix := "$"
suffix := ""
sep := ", $"
if runtime.GOOS == "windows" {
prefix = "%"
suffix = "%"
sep = "%, %"
}
return fmt.Sprintf(" [%s%s%s]", prefix, strings.Join(envVars, sep), suffix)
}
return ""
}

envText = fmt.Sprintf(" [%s%s%s]", prefix, strings.Join(envVars, sep), suffix)
func defaultEnvFormat(envVars []string) string {
return envFormat(envVars, "$", ", $", "")
}

func withEnvHint(envVars []string, str string) string {
envText := ""
if runtime.GOOS != "windows" || os.Getenv("PSHOME") != "" {
envText = defaultEnvFormat(envVars)
} else {
envText = envFormat(envVars, "%", "%, %", "%")
}
return str + envText
}
4 changes: 4 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
@@ -239,6 +239,10 @@ func TestFlagsFromEnv(t *testing.T) {
t.Errorf("Flag %s not set", f.Names()[0])
}

// check that flag names are returned when set via env as well
if !reflect.DeepEqual(ctx.FlagNames(), test.flag.Names()) {
t.Errorf("Not enough flag names %+v", ctx.FlagNames())
}
return nil
},
}
4 changes: 2 additions & 2 deletions sliceflag_test.go
Original file line number Diff line number Diff line change
@@ -156,8 +156,8 @@ func ExampleMultiStringFlag() {
//---
//Setting all flags via environment...
//
//Flag names: []
//Local flag names: []
//Flag names: ["flag-one" "1" "two" "2" "flag-three" "3" "flag-four" "4"]
//Local flag names: ["flag-one" "1" "two" "2" "flag-three" "3" "flag-four" "4"]
//Context values:
//"flag-one"=["v 9" "v 10"]
//"two"=["v 11" "v 12"]