Skip to content

Commit

Permalink
Include a time.Duration flag in the subcommands example (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
FollowTheProcess authored Jan 16, 2025
1 parent f52dfee commit 7e90afb
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 4 deletions.
Binary file modified docs/img/namedargs.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/img/quickstart.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/img/subcommands.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/src/subcommands.tape
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Type "clear" Sleep 500ms Enter

Sleep 1s

Type "./subcommands do 'something' --fast --count 3 -vvv" Sleep 500ms Enter
Type "./subcommands do 'something' --fast --count 3 --duration 5m27s -vvv" Sleep 500ms Enter

Sleep 4s

Expand Down
16 changes: 13 additions & 3 deletions examples/subcommands/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"strings"
"time"

"github.com/FollowTheProcess/cli"
)
Expand Down Expand Up @@ -55,6 +56,7 @@ type doOptions struct {
count int
fast bool
verbosity cli.FlagCount
duration time.Duration
}

func buildDoCommand() (*cli.Command, error) {
Expand All @@ -64,10 +66,12 @@ func buildDoCommand() (*cli.Command, error) {
cli.Short("Do a thing"),
cli.Example("Do something", "demo do something --fast"),
cli.Example("Do it 3 times", "demo do something --count 3"),
cli.Allow(cli.MaxArgs(1)), // Only allowed to do one thing
cli.Example("Do it for a specific duration", "demo do something --duration 1m30s"),
cli.Allow(cli.ExactArgs(1)), // Only allowed to do one thing
cli.Flag(&options.count, "count", 'c', 1, "Number of times to do the thing"),
cli.Flag(&options.fast, "fast", 'f', false, "Do the thing quickly"),
cli.Flag(&options.verbosity, "verbosity", 'v', 0, "Increase the verbosity level"),
cli.Flag(&options.duration, "duration", 'd', 1*time.Second, "Do the thing for a specific duration"),
cli.Run(runDo(&options)),
)
if err != nil {
Expand Down Expand Up @@ -97,9 +101,15 @@ func runSay(options *sayOptions) func(cmd *cli.Command, args []string) error {
func runDo(options *doOptions) func(cmd *cli.Command, args []string) error {
return func(cmd *cli.Command, args []string) error {
if options.fast {
fmt.Fprintf(cmd.Stdout(), "Doing %s %d times, but fast!\n", args[0], options.count)
fmt.Fprintf(
cmd.Stdout(),
"Doing %s %d times, but faster! (will still take %v)\n",
args[0],
options.count,
options.duration,
)
} else {
fmt.Fprintf(cmd.Stdout(), "Doing %s %d times\n", args[0], options.count)
fmt.Fprintf(cmd.Stdout(), "Doing %s %d times for %v\n", args[0], options.count, options.duration)
}

fmt.Fprintf(cmd.Stdout(), "Verbosity level was %d\n", options.verbosity)
Expand Down

0 comments on commit 7e90afb

Please sign in to comment.