-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
68 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,20 +15,19 @@ demonstration purposes. Use of one's imagination is encouraged. | |
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"time" | ||
"context" | ||
|
||
"github.com/urfave/cli/v3" | ||
) | ||
|
||
func init() { | ||
cli.AppHelpTemplate += "\nCUSTOMIZED: you bet ur muffins\n" | ||
cli.RootCommandHelpTemplate += "\nCUSTOMIZED: you bet ur muffins\n" | ||
cli.CommandHelpTemplate += "\nYMMV\n" | ||
cli.SubcommandHelpTemplate += "\nor something\n" | ||
|
||
|
@@ -38,11 +37,11 @@ func init() { | |
cli.HelpPrinter = func(w io.Writer, templ string, data interface{}) { | ||
fmt.Fprintf(w, "best of luck to you\n") | ||
} | ||
cli.VersionPrinter = func(ctx context.Context, cmd *cli.Command)) { | ||
fmt.Fprintf(cCtx.App.Writer, "version=%s\n", cCtx.App.Version) | ||
cli.VersionPrinter = func(cmd *cli.Command) { | ||
fmt.Fprintf(cmd.Root().Writer, "version=%s\n", cmd.Root().Version) | ||
} | ||
cli.OsExiter = func(cCtx int) { | ||
fmt.Fprintf(cli.ErrWriter, "refusing to exit %d\n", cCtx) | ||
cli.OsExiter = func(cmd int) { | ||
fmt.Fprintf(cli.ErrWriter, "refusing to exit %d\n", cmd) | ||
} | ||
cli.ErrWriter = ioutil.Discard | ||
cli.FlagStringer = func(fl cli.Flag) string { | ||
|
@@ -76,17 +75,15 @@ func (g *genericType) String() string { | |
|
||
func main() { | ||
cmd := &cli.Command{ | ||
Name: "kənˈtrīv", | ||
Version: "v19.99.0", | ||
Compiled: time.Now(), | ||
Authors: []*cli.Author{ | ||
Name: "kənˈtrīv", | ||
Version: "v19.99.0", | ||
/*Authors: []any{ | ||
&cli.Author{ | ||
Name: "Example Human", | ||
Email: "[email protected]", | ||
}, | ||
}, | ||
},*/ | ||
Copyright: "(c) 1999 Serious Enterprise", | ||
HelpName: "contrive", | ||
Usage: "demonstrate available API", | ||
UsageText: "contrive - demonstrating the available API", | ||
ArgsUsage: "[args and such]", | ||
|
@@ -102,7 +99,7 @@ func main() { | |
Flags: []cli.Flag{ | ||
&cli.BoolFlag{Name: "forever", Aliases: []string{"forevvarr"}}, | ||
}, | ||
Subcommands: []*cli.Command{ | ||
Commands: []*cli.Command{ | ||
&cli.Command{ | ||
Name: "wop", | ||
Action: wopAction, | ||
|
@@ -111,31 +108,30 @@ func main() { | |
SkipFlagParsing: false, | ||
HideHelp: false, | ||
Hidden: false, | ||
HelpName: "doo!", | ||
BashComplete: func(ctx context.Context, cmd *cli.Command)) { | ||
fmt.Fprintf(cCtx.App.Writer, "--better\n") | ||
ShellComplete: func(ctx context.Context, cmd *cli.Command) { | ||
fmt.Fprintf(cmd.Root().Writer, "--better\n") | ||
}, | ||
Before: func(ctx context.Context, cmd *cli.Command)) error { | ||
fmt.Fprintf(cCtx.App.Writer, "brace for impact\n") | ||
Before: func(ctx context.Context, cmd *cli.Command) error { | ||
fmt.Fprintf(cmd.Root().Writer, "brace for impact\n") | ||
return nil | ||
}, | ||
After: func(ctx context.Context, cmd *cli.Command)) error { | ||
fmt.Fprintf(cCtx.App.Writer, "did we lose anyone?\n") | ||
After: func(ctx context.Context, cmd *cli.Command) error { | ||
fmt.Fprintf(cmd.Root().Writer, "did we lose anyone?\n") | ||
return nil | ||
}, | ||
Action: func(ctx context.Context, cmd *cli.Command)) error { | ||
cCtx.Command.FullName() | ||
cCtx.Command.HasName("wop") | ||
cCtx.Command.Names() | ||
cCtx.Command.VisibleFlags() | ||
fmt.Fprintf(cCtx.App.Writer, "dodododododoodododddooooododododooo\n") | ||
if cCtx.Bool("forever") { | ||
cCtx.Command.Run(cCtx) | ||
Action: func(ctx context.Context, cmd *cli.Command) error { | ||
cmd.FullName() | ||
cmd.HasName("wop") | ||
cmd.Names() | ||
cmd.VisibleFlags() | ||
fmt.Fprintf(cmd.Root().Writer, "dodododododoodododddooooododododooo\n") | ||
if cmd.Bool("forever") { | ||
cmd.Run(ctx, nil) | ||
} | ||
return nil | ||
}, | ||
OnUsageError: func(cCtx *cli.Context, err error, isSubcommand bool) error { | ||
fmt.Fprintf(cCtx.App.Writer, "for shame\n") | ||
OnUsageError: func(ctx context.Context, cmd *cli.Command, err error, isSubcommand bool) error { | ||
fmt.Fprintf(cmd.Root().Writer, "for shame\n") | ||
return err | ||
}, | ||
}, | ||
|
@@ -144,100 +140,67 @@ func main() { | |
&cli.BoolFlag{Name: "fancy"}, | ||
&cli.BoolFlag{Value: true, Name: "fancier"}, | ||
&cli.DurationFlag{Name: "howlong", Aliases: []string{"H"}, Value: time.Second * 3}, | ||
&cli.Float64Flag{Name: "howmuch"}, | ||
&cli.GenericFlag{Name: "wat", Value: &genericType{}}, | ||
&cli.Int64Flag{Name: "longdistance"}, | ||
&cli.Int64SliceFlag{Name: "intervals"}, | ||
&cli.IntFlag{Name: "distance"}, | ||
&cli.IntSliceFlag{Name: "times"}, | ||
&cli.FloatFlag{Name: "howmuch"}, | ||
&cli.IntFlag{Name: "longdistance"}, | ||
&cli.IntSliceFlag{Name: "intervals"}, | ||
&cli.StringFlag{Name: "dance-move", Aliases: []string{"d"}}, | ||
&cli.StringSliceFlag{Name: "names", Aliases: []string{"N"}}, | ||
&cli.UintFlag{Name: "age"}, | ||
&cli.Uint64Flag{Name: "bigage"}, | ||
}, | ||
EnableBashCompletion: true, | ||
HideHelp: false, | ||
HideVersion: false, | ||
BashComplete: func(ctx context.Context, cmd *cli.Command)) { | ||
fmt.Fprintf(cCtx.App.Writer, "lipstick\nkiss\nme\nlipstick\nringo\n") | ||
EnableShellCompletion: true, | ||
HideHelp: false, | ||
HideVersion: false, | ||
ShellComplete: func(ctx context.Context, cmd *cli.Command) { | ||
fmt.Fprintf(cmd.Root().Writer, "lipstick\nkiss\nme\nlipstick\nringo\n") | ||
}, | ||
Before: func(ctx context.Context, cmd *cli.Command)) error { | ||
fmt.Fprintf(cCtx.App.Writer, "HEEEERE GOES\n") | ||
Before: func(ctx context.Context, cmd *cli.Command) error { | ||
fmt.Fprintf(cmd.Root().Writer, "HEEEERE GOES\n") | ||
return nil | ||
}, | ||
After: func(ctx context.Context, cmd *cli.Command)) error { | ||
fmt.Fprintf(cCtx.App.Writer, "Phew!\n") | ||
After: func(ctx context.Context, cmd *cli.Command) error { | ||
fmt.Fprintf(cmd.Root().Writer, "Phew!\n") | ||
return nil | ||
}, | ||
CommandNotFound: func(cCtx *cli.Context, command string) { | ||
fmt.Fprintf(cCtx.App.Writer, "Thar be no %q here.\n", command) | ||
CommandNotFound: func(ctx context.Context, cmd *cli.Command, command string) { | ||
fmt.Fprintf(cmd.Root().Writer, "Thar be no %q here.\n", command) | ||
}, | ||
OnUsageError: func(cCtx *cli.Context, err error, isSubcommand bool) error { | ||
OnUsageError: func(ctx context.Context, cmd *cli.Command, err error, isSubcommand bool) error { | ||
if isSubcommand { | ||
return err | ||
} | ||
|
||
fmt.Fprintf(cCtx.App.Writer, "WRONG: %#v\n", err) | ||
fmt.Fprintf(cmd.Root().Writer, "WRONG: %#v\n", err) | ||
return nil | ||
}, | ||
Action: func(ctx context.Context, cmd *cli.Command)) error { | ||
cli.DefaultAppComplete(cCtx) | ||
Action: func(ctx context.Context, cmd *cli.Command) error { | ||
cli.DefaultAppComplete(ctx, cmd) | ||
cli.HandleExitCoder(errors.New("not an exit coder, though")) | ||
cli.ShowAppHelp(cCtx) | ||
cli.ShowCommandCompletions(cCtx, "nope") | ||
cli.ShowCommandHelp(cCtx, "also-nope") | ||
cli.ShowCompletions(cCtx) | ||
cli.ShowSubcommandHelp(cCtx) | ||
cli.ShowVersion(cCtx) | ||
|
||
fmt.Printf("%#v\n", cCtx.App.Command("doo")) | ||
if cCtx.Bool("infinite") { | ||
cCtx.App.Run([]string{"app", "doo", "wop"}) | ||
cli.ShowAppHelp(cmd) | ||
cli.ShowCommandHelp(ctx, cmd, "also-nope") | ||
cli.ShowSubcommandHelp(cmd) | ||
cli.ShowVersion(cmd) | ||
|
||
fmt.Printf("%#v\n", cmd.Root().Command("doo")) | ||
if cmd.Bool("infinite") { | ||
cmd.Root().Run(ctx, []string{"app", "doo", "wop"}) | ||
} | ||
|
||
if cCtx.Bool("forevar") { | ||
cCtx.App.RunAsSubcommand(cCtx) | ||
if cmd.Bool("forevar") { | ||
cmd.Root().Run(ctx, nil) | ||
} | ||
cCtx.App.Setup() | ||
fmt.Printf("%#v\n", cCtx.App.VisibleCategories()) | ||
fmt.Printf("%#v\n", cCtx.App.VisibleCommands()) | ||
fmt.Printf("%#v\n", cCtx.App.VisibleFlags()) | ||
fmt.Printf("%#v\n", cmd.Root().VisibleCategories()) | ||
fmt.Printf("%#v\n", cmd.Root().VisibleCommands()) | ||
fmt.Printf("%#v\n", cmd.Root().VisibleFlags()) | ||
|
||
fmt.Printf("%#v\n", cCtx.Args().First()) | ||
if cCtx.Args().Len() > 0 { | ||
fmt.Printf("%#v\n", cCtx.Args().Get(1)) | ||
fmt.Printf("%#v\n", cmd.Args().First()) | ||
if cmd.Args().Len() > 0 { | ||
fmt.Printf("%#v\n", cmd.Args().Get(1)) | ||
} | ||
fmt.Printf("%#v\n", cCtx.Args().Present()) | ||
fmt.Printf("%#v\n", cCtx.Args().Tail()) | ||
|
||
set := flag.NewFlagSet("contrive", 0) | ||
nc := cli.NewContext(cCtx.App, set, cCtx) | ||
|
||
fmt.Printf("%#v\n", nc.Args()) | ||
fmt.Printf("%#v\n", nc.Bool("nope")) | ||
fmt.Printf("%#v\n", !nc.Bool("nerp")) | ||
fmt.Printf("%#v\n", nc.Duration("howlong")) | ||
fmt.Printf("%#v\n", nc.Float64("hay")) | ||
fmt.Printf("%#v\n", nc.Generic("bloop")) | ||
fmt.Printf("%#v\n", nc.Int64("bonk")) | ||
fmt.Printf("%#v\n", nc.Int64Slice("burnks")) | ||
fmt.Printf("%#v\n", nc.Int("bips")) | ||
fmt.Printf("%#v\n", nc.IntSlice("blups")) | ||
fmt.Printf("%#v\n", nc.String("snurt")) | ||
fmt.Printf("%#v\n", nc.StringSlice("snurkles")) | ||
fmt.Printf("%#v\n", nc.Uint("flub")) | ||
fmt.Printf("%#v\n", nc.Uint64("florb")) | ||
|
||
fmt.Printf("%#v\n", nc.FlagNames()) | ||
fmt.Printf("%#v\n", nc.IsSet("wat")) | ||
fmt.Printf("%#v\n", nc.Set("wat", "nope")) | ||
fmt.Printf("%#v\n", nc.NArg()) | ||
fmt.Printf("%#v\n", nc.NumFlags()) | ||
fmt.Printf("%#v\n", nc.Lineage()[1]) | ||
nc.Set("wat", "also-nope") | ||
fmt.Printf("%#v\n", cmd.Args().Present()) | ||
fmt.Printf("%#v\n", cmd.Args().Tail()) | ||
|
||
ec := cli.Exit("ohwell", 86) | ||
fmt.Fprintf(cCtx.App.Writer, "%d", ec.ExitCode()) | ||
fmt.Fprintf(cmd.Root().Writer, "%d", ec.ExitCode()) | ||
fmt.Printf("made it!\n") | ||
return ec | ||
}, | ||
|
@@ -249,15 +212,15 @@ func main() { | |
} | ||
|
||
if os.Getenv("HEXY") != "" { | ||
app.Writer = &hexWriter{} | ||
app.ErrWriter = &hexWriter{} | ||
cmd.Writer = &hexWriter{} | ||
cmd.ErrWriter = &hexWriter{} | ||
} | ||
|
||
cmd.Run(context.Background(), os.Args) | ||
} | ||
|
||
func wopAction(cCtx *cli.Context) error { | ||
fmt.Fprintf(cCtx.App.Writer, ":wave: over here, eh\n") | ||
func wopAction(ctx context.Context, cmd *cli.Command) error { | ||
fmt.Fprintf(cmd.Root().Writer, ":wave: over here, eh\n") | ||
return nil | ||
} | ||
``` |