@@ -50,6 +50,7 @@ go get go.followtheprocess.codes/cli@latest
5050package main
5151
5252import (
53+ " context"
5354 " fmt"
5455 " os"
5556
@@ -77,7 +78,7 @@ func run() error {
7778 cli.Example (" Do a thing" , " quickstart something" ),
7879 cli.Example (" Count the things" , " quickstart something --count 3" ),
7980 cli.Flag (&count, " count" , ' c' , 0 , " Count the things" ),
80- cli.Run (func (cmd *cli.Command ) error {
81+ cli.Run (func (ctx context. Context , cmd *cli.Command ) error {
8182 fmt.Fprintf (cmd.Stdout (), " Hello from quickstart!, my args were: %v , count was %d \n " , cmd.Args (), count)
8283 return nil
8384 }),
@@ -86,7 +87,7 @@ func run() error {
8687 return err
8788 }
8889
89- return cmd.Execute ()
90+ return cmd.Execute (context. Background () )
9091}
9192```
9293
@@ -107,9 +108,9 @@ To create CLI commands, you simply call `cli.New`:
107108cmd , err := cli.New (
108109 " name" , // The name of your command
109110 cli.Short (" A new command" ) // Shown in the help
110- cli.Run (func (cmd *cli.Command , args [] string ) error {
111+ cli.Run (func (ctx context. Context , cmd *cli.Command ) error {
111112 // This function is what your command does
112- fmt.Printf (" name called with args: %v \n " , args )
113+ fmt.Printf (" name called with args: %v \n " , cmd. Args () )
113114 return nil
114115 })
115116)
@@ -124,7 +125,7 @@ To add a subcommand underneath the command you've just created, it's again `cli.
124125
125126``` go
126127// Best to abstract it into a function
127- func buildSubcommand () (*cli .Command , error ) {
128+ func buildSubcommand (ctx context . Context ) (*cli .Command , error ) {
128129 return cli.New (
129130 " sub" , // Name of the sub command e.g. 'clone' for 'git clone'
130131 cli.Short (" A sub command" ),
@@ -136,11 +137,13 @@ func buildSubcommand() (*cli.Command, error) {
136137And add it to your parent command:
137138
138139``` go
140+ ctx := context.Background ()
141+
139142// From the example above
140143cmd , err := cli.New (
141144 " name" , // The name of your command
142145 // ...
143- cli.SubCommands (buildSubcommand),
146+ cli.SubCommands (buildSubcommand (ctx) ),
144147)
145148```
146149
@@ -151,6 +154,8 @@ This pattern can be repeated recursively to create complex command structures.
151154Flags in ` cli ` are generic, that is, there is * one* way to add a flag to your command, and that's with the ` cli.Flag ` option to ` cli.New `
152155
153156``` go
157+ // These will get set at command line parse time
158+ // based on your flags
154159type options struct {
155160 name string
156161 force bool
@@ -167,7 +172,7 @@ func buildCmd() (*cli.Command, error) {
167172 cli.Flag (&options.force , " force" , cli.NoShortHand , false , " Force delete without confirmation" ),
168173 cli.Flag (&options.size , " size" , ' s' , 0 , " Size of something" ),
169174 cli.Flag (&options.items , " items" , ' i' , nil , " Items to include" ),
170- cli. Run ( runCmd (&options)), // Pass the parsed flag values to your command run function
175+ // ...
171176 )
172177}
173178```
@@ -220,7 +225,7 @@ There are two approaches to positional arguments in `cli`, you can either just g
220225cli.New (
221226 " my-command" ,
222227 // ...
223- cli.Run (func (cmd *cli.Command ) error {
228+ cli.Run (func (ctx context. Context , cmd *cli.Command ) error {
224229 fmt.Fprintf (cmd.Stdout (), " Hello! My arguments were: %v \n " , cmd.Args ())
225230 return nil
226231 })
0 commit comments