Skip to content

Commit

Permalink
Merge branch 'main' into issue_1951
Browse files Browse the repository at this point in the history
  • Loading branch information
dearchap authored Aug 31, 2024
2 parents 924201a + 3d76e1b commit f623a80
Show file tree
Hide file tree
Showing 17 changed files with 543 additions and 115 deletions.
2 changes: 1 addition & 1 deletion args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestArgumentsSubcommand(t *testing.T) {
Max: 1,
Destination: &tval,
Config: TimestampConfig{
Layout: time.RFC3339,
Layouts: []string{time.RFC3339},
},
},
&StringArg{
Expand Down
55 changes: 37 additions & 18 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2490,6 +2490,7 @@ func TestSetupInitializesOnlyNilWriters(t *testing.T) {
}

func TestFlagAction(t *testing.T) {
now := time.Now().UTC().Truncate(time.Minute)
testCases := []struct {
name string
args []string
Expand Down Expand Up @@ -2578,8 +2579,8 @@ func TestFlagAction(t *testing.T) {
},
{
name: "flag_timestamp",
args: []string{"app", "--f_timestamp", "2022-05-01 02:26:20"},
exp: "2022-05-01T02:26:20Z ",
args: []string{"app", "--f_timestamp", now.Format(time.DateTime)},
exp: now.UTC().Format(time.RFC3339) + " ",
},
{
name: "flag_timestamp_error",
Expand Down Expand Up @@ -2738,12 +2739,14 @@ func TestFlagAction(t *testing.T) {
&TimestampFlag{
Name: "f_timestamp",
Config: TimestampConfig{
Layout: "2006-01-02 15:04:05",
Timezone: time.UTC,
Layouts: []string{time.DateTime},
},
Action: func(_ context.Context, cmd *Command, v time.Time) error {
if v.IsZero() {
return fmt.Errorf("zero timestamp")
}

_, err := cmd.Root().Writer.Write([]byte(v.Format(time.RFC3339) + " "))
return err
},
Expand Down Expand Up @@ -3763,7 +3766,7 @@ func TestCommandReadArgsFromStdIn(t *testing.T) {
{
name: "empty2",
input: `
`,
args: []string{"foo"},
expectedInt: 0,
Expand All @@ -3781,7 +3784,7 @@ func TestCommandReadArgsFromStdIn(t *testing.T) {
{
name: "intflag-from-input2",
input: `
--if
--if
100`,
args: []string{"foo"},
Expand All @@ -3800,14 +3803,14 @@ func TestCommandReadArgsFromStdIn(t *testing.T) {
--ssf hello
--ssf
"hello
"hello
123
44"
`,
args: []string{"foo"},
expectedInt: 100,
expectedFloat: 100.1,
expectedSlice: []string{"hello", "hello\t\n 123\n44"},
expectedSlice: []string{"hello", "hello\n 123\n44"},
},
{
name: "end-args",
Expand Down Expand Up @@ -3905,6 +3908,11 @@ func TestCommandReadArgsFromStdIn(t *testing.T) {
}
}

func TestZeroValueCommand(t *testing.T) {
var cmd Command
assert.NoError(t, cmd.Run(context.Background(), []string{"foo"}))
}

func TestJSONExportCommand(t *testing.T) {
cmd := buildExtendedTestCommand()
cmd.Arguments = []Argument{
Expand Down Expand Up @@ -3973,7 +3981,8 @@ func TestJSONExportCommand(t *testing.T) {
"config": {
"TrimSpace": false
},
"onlyOnce": false
"onlyOnce": false,
"validateDefaults" : false
},
{
"name": "sub-command-flag",
Expand All @@ -3992,7 +4001,8 @@ func TestJSONExportCommand(t *testing.T) {
"config": {
"Count": null
},
"onlyOnce": false
"onlyOnce": false,
"validateDefaults" : false
}
],
"hideHelp": false,
Expand Down Expand Up @@ -4033,7 +4043,8 @@ func TestJSONExportCommand(t *testing.T) {
"config": {
"TrimSpace": false
},
"onlyOnce": false
"onlyOnce": false,
"validateDefaults" : false
},
{
"name": "another-flag",
Expand All @@ -4052,7 +4063,8 @@ func TestJSONExportCommand(t *testing.T) {
"config": {
"Count": null
},
"onlyOnce": false
"onlyOnce": false,
"validateDefaults" : false
}
],
"hideHelp": false,
Expand Down Expand Up @@ -4210,7 +4222,8 @@ func TestJSONExportCommand(t *testing.T) {
"config": {
"Count": null
},
"onlyOnce": false
"onlyOnce": false,
"validateDefaults" : false
}
],
"hideHelp": false,
Expand Down Expand Up @@ -4251,7 +4264,8 @@ func TestJSONExportCommand(t *testing.T) {
"config": {
"TrimSpace": false
},
"onlyOnce": false
"onlyOnce": false,
"validateDefaults" : false
},
{
"name": "another-flag",
Expand All @@ -4270,7 +4284,8 @@ func TestJSONExportCommand(t *testing.T) {
"config": {
"Count": null
},
"onlyOnce": false
"onlyOnce": false,
"validateDefaults" : false
}
],
"hideHelp": false,
Expand Down Expand Up @@ -4310,7 +4325,8 @@ func TestJSONExportCommand(t *testing.T) {
"config": {
"TrimSpace": false
},
"onlyOnce": false
"onlyOnce": false,
"validateDefaults" : false
},
{
"name": "flag",
Expand All @@ -4330,7 +4346,8 @@ func TestJSONExportCommand(t *testing.T) {
"config": {
"TrimSpace": false
},
"onlyOnce": false
"onlyOnce": false,
"validateDefaults" : false
},
{
"name": "another-flag",
Expand All @@ -4349,7 +4366,8 @@ func TestJSONExportCommand(t *testing.T) {
"config": {
"Count": null
},
"onlyOnce": false
"onlyOnce": false,
"validateDefaults" : false
},
{
"name": "hidden-flag",
Expand All @@ -4366,7 +4384,8 @@ func TestJSONExportCommand(t *testing.T) {
"config": {
"Count": null
},
"onlyOnce": false
"onlyOnce": false,
"validateDefaults" : false
}
],
"hideHelp": false,
Expand Down
8 changes: 6 additions & 2 deletions docs/v2/examples/flags.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,11 @@ See full list of flags at https://pkg.go.dev/github.com/urfave/cli/v2

For bool flags you can specify the flag multiple times to get a count(e.g -v -v -v or -vvv)

> If you want to support the `-vvv` flag, you need to set `App.UseShortOptionHandling`.
<!-- {
"args": ["&#45;&#45;foo", "&#45;&#45;foo"],
"output": "count 2"
"args": ["&#45;&#45;foo", "&#45;&#45;foo", "&#45;fff", "&#45;f"],
"output": "count 6"
} -->
```go
package main
Expand All @@ -123,10 +125,12 @@ func main() {
var count int

app := &cli.App{
UseShortOptionHandling: true,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "foo",
Usage: "foo greeting",
Aliases: []string{"f"},
Count: &count,
},
},
Expand Down
10 changes: 6 additions & 4 deletions docs/v2/examples/full-api-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ func main() {
Action: wopAction,
},
},
SkipFlagParsing: false,
HideHelp: false,
Hidden: false,
HelpName: "doo!",
SkipFlagParsing: false,
HideHelp: false,
HideHelpCommands: false,
Hidden: false,
HelpName: "doo!",
BashComplete: func(cCtx *cli.Context) {
fmt.Fprintf(cCtx.App.Writer, "--better\n")
},
Expand Down Expand Up @@ -156,6 +157,7 @@ func main() {
},
EnableBashCompletion: true,
HideHelp: false,
HideHelpCommands: false,
HideVersion: false,
BashComplete: func(cCtx *cli.Context) {
fmt.Fprintf(cCtx.App.Writer, "lipstick\nkiss\nme\nlipstick\nringo\n")
Expand Down
8 changes: 6 additions & 2 deletions docs/v3/examples/flags.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,11 @@ See full list of flags at https://pkg.go.dev/github.com/urfave/cli/v3

For bool flags you can specify the flag multiple times to get a count(e.g -v -v -v or -vvv)

> If you want to support the `-vvv` flag, you need to set `Command.UseShortOptionHandling`.
<!-- {
"args": ["&#45;&#45;foo", "&#45;&#45;foo"],
"output": "count 2"
"args": ["&#45;&#45;foo", "&#45;&#45;foo", "&#45;fff"],
"output": "count 5"
} -->
```go
package main
Expand All @@ -126,10 +128,12 @@ func main() {
var count int

cmd := &cli.Command{
UseShortOptionHandling: true,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "foo",
Usage: "foo greeting",
Aliases: []string{"f"},
Config: cli.BoolConfig{
Count: &count,
},
Expand Down
15 changes: 13 additions & 2 deletions docs/v3/examples/timestamp-flag.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ import (
func main() {
cmd := &cli.Command{
Flags: []cli.Flag{
&cli.TimestampFlag{Name: "meeting", Config: cli.TimestampConfig{Layout: "2006-01-02T15:04:05"}},
&cli.TimestampFlag{
Name: "meeting",
Config: cli.TimestampConfig{
Layouts: []string{"2006-01-02T15:04:05"},
},
},
},
Action: func(ctx context.Context, cmd *cli.Command) error {
fmt.Printf("%s", cmd.Timestamp("meeting").String())
Expand All @@ -54,7 +59,13 @@ change behavior, a default timezone can be provided with flag definition:
```go
cmd := &cli.Command{
Flags: []cli.Flag{
&cli.TimestampFlag{Name: "meeting", Config: cli.TimestampConfig{Layout: "2006-01-02T15:04:05", Timezone: time.Local}},
&cli.TimestampFlag{
Name: "meeting",
Config: cli.TimestampConfig{
Timezone: time.Local,
AvailableLayouts: []string{"2006-01-02T15:04:05"},
},
},
},
}
```
Expand Down
2 changes: 1 addition & 1 deletion examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ func ExampleCommand_Run_shellComplete_zsh() {

// Simulate a zsh environment and command line arguments
os.Args = []string{"greet", "--generate-shell-completion"}
os.Setenv("SHELL", "/usr/bin/zsh")
os.Setenv("0", "/usr/bin/zsh")

_ = cmd.Run(context.Background(), os.Args)
// Output:
Expand Down
22 changes: 18 additions & 4 deletions flag_bool_with_inverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ func (parent *BoolWithInverseFlag) inverseName() string {
return parent.InversePrefix + parent.BoolFlag.Name
}

func (parent *BoolWithInverseFlag) inversePrefix() string {
if parent.InversePrefix == "" {
return DefaultInverseBoolPrefix
}

return parent.InversePrefix
}

func (parent *BoolWithInverseFlag) inverseAliases() (aliases []string) {
if len(parent.BoolFlag.Aliases) > 0 {
aliases = make([]string, len(parent.BoolFlag.Aliases))
Expand Down Expand Up @@ -170,11 +178,17 @@ func (parent *BoolWithInverseFlag) Names() []string {
// String implements the standard Stringer interface.
//
// Example for BoolFlag{Name: "env"}
// --env (default: false) || --no-env (default: false)
// --[no-]env (default: false)
func (parent *BoolWithInverseFlag) String() string {
if parent.positiveFlag == nil {
return fmt.Sprintf("%s || --%s", parent.BoolFlag.String(), parent.inverseName())
out := FlagStringer(parent)
i := strings.Index(out, "\t")

prefix := "--"

// single character flags are prefixed with `-` instead of `--`
if len(parent.Name) == 1 {
prefix = "-"
}

return fmt.Sprintf("%s || %s", parent.positiveFlag.String(), parent.negativeFlag.String())
return fmt.Sprintf("%s[%s]%s%s", prefix, parent.inversePrefix(), parent.Name, out[i:])
}
Loading

0 comments on commit f623a80

Please sign in to comment.