|
| 1 | +// Package main is a fang example. |
| 2 | +package main |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + "errors" |
| 7 | + "os" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/charmbracelet/fang" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +func main() { |
| 15 | + var foo string |
| 16 | + var bar int |
| 17 | + var baz float64 |
| 18 | + var d time.Duration |
| 19 | + var eerr bool |
| 20 | + |
| 21 | + cmd := &cobra.Command{ |
| 22 | + Use: "example [args]", |
| 23 | + Short: "An example program!", |
| 24 | + Long: `A little example program! |
| 25 | +
|
| 26 | +It doesn’t really do anything, but that’s the point.™`, |
| 27 | + Example: ` |
| 28 | +# Run it: |
| 29 | +example |
| 30 | +
|
| 31 | +# Run it with some arguments: |
| 32 | +FOO=bar ZAZ="quoted value" example --name=Carlos -a -s Becker -a |
| 33 | +
|
| 34 | +# Run a subcommand with an argument: |
| 35 | +example sub --async --foo=xyz --async arguments |
| 36 | +
|
| 37 | +# Run with a quoted string: |
| 38 | +example sub "quoted string" |
| 39 | +
|
| 40 | +# Mix and match: |
| 41 | +example sub "multi-word quoted string" --name "another quoted string" -a |
| 42 | +
|
| 43 | +# Multi-line: |
| 44 | +ENV_A=0 ENV_B=0 ENV_C=0 \ |
| 45 | + CERT_FILE=/path/to/chain.pem KEY_FILE=/path/to/key.pem \ |
| 46 | + example sub "quoted argument" |
| 47 | +
|
| 48 | +# Run a subcommand's subcommand with an argument: |
| 49 | +example sub another args --flag |
| 50 | + `, |
| 51 | + |
| 52 | + RunE: func(c *cobra.Command, _ []string) error { |
| 53 | + if eerr { |
| 54 | + return errors.New("we have an error") |
| 55 | + } |
| 56 | + c.Println("You ran the root command. Now try --help.") |
| 57 | + return nil |
| 58 | + }, |
| 59 | + } |
| 60 | + cmd.PersistentFlags().StringVarP(&foo, "surname", "s", "doe", "Your surname") |
| 61 | + cmd.Flags().StringVar(&foo, "name", "jane", "Your name") |
| 62 | + cmd.Flags().DurationVar(&d, "duration", 0, "Time since your last commit") |
| 63 | + cmd.Flags().IntVar(&bar, "age", 0, "Your age") |
| 64 | + cmd.Flags().Float64Var(&baz, "idk", 0.0, "I don't know") |
| 65 | + cmd.Flags().BoolP("async", "a", false, "Run async") |
| 66 | + cmd.Flags().BoolVarP(&eerr, "error", "e", false, "Makes the program exit with error") |
| 67 | + |
| 68 | + _ = cmd.Flags().MarkHidden("age") |
| 69 | + _ = cmd.Flags().MarkHidden("duration") |
| 70 | + _ = cmd.Flags().MarkHidden("idk") |
| 71 | + _ = cmd.Flags().MarkHidden("error") |
| 72 | + |
| 73 | + cmd.AddGroup(&cobra.Group{ |
| 74 | + ID: "group1", |
| 75 | + Title: "My Group", |
| 76 | + }) |
| 77 | + sub := &cobra.Command{ |
| 78 | + Use: "sub [command] [flags] [args]", |
| 79 | + Short: "An example subcommand", |
| 80 | + GroupID: "group1", |
| 81 | + Example: `example sub some arguments --and-flags |
| 82 | +example sub another --thing`, |
| 83 | + Run: func(c *cobra.Command, _ []string) { |
| 84 | + c.Println("Ran the sub command!") |
| 85 | + }, |
| 86 | + } |
| 87 | + cmd.AddCommand(sub) |
| 88 | + sub.AddCommand(&cobra.Command{ |
| 89 | + Use: "another", |
| 90 | + Short: "another sub command", |
| 91 | + Example: `example sub another --foo=bar`, |
| 92 | + RunE: func(c *cobra.Command, _ []string) error { |
| 93 | + cmd.Println("Working...") |
| 94 | + select { |
| 95 | + case <-time.After(time.Second * 5): |
| 96 | + cmd.Println("Done!") |
| 97 | + case <-c.Context().Done(): |
| 98 | + return c.Context().Err() |
| 99 | + } |
| 100 | + return nil |
| 101 | + }, |
| 102 | + }) |
| 103 | + |
| 104 | + cmd.AddCommand(&cobra.Command{ |
| 105 | + Use: "throw", |
| 106 | + Short: "Throws an error", |
| 107 | + GroupID: "group1", |
| 108 | + RunE: func(*cobra.Command, []string) error { |
| 109 | + return errors.New("a super long error string that is meant to test the error handling in fang. It should be long enough to wrap around and test the error styling and formatting capabilities of fang. This is a test to see how well fang handles long error messages and whether it can display them properly without breaking the layout or causing any issues") |
| 110 | + }, |
| 111 | + }) |
| 112 | + |
| 113 | + // This is where the magic happens. |
| 114 | + if err := fang.Execute( |
| 115 | + context.Background(), |
| 116 | + cmd, |
| 117 | + fang.WithNotifySignal(os.Interrupt, os.Kill), |
| 118 | + ); err != nil { |
| 119 | + os.Exit(1) |
| 120 | + } |
| 121 | +} |
0 commit comments