1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
|
package cli_test
import (
"context"
"fmt"
"net/mail"
"os"
"time"
// Alias the package import to make the examples runnable on pkg.go.dev.
//
// See issue #1811.
cli "github.com/urfave/cli/v3"
)
func ExampleCommand_Run() {
// Declare a command
cmd := &cli.Command{
Name: "greet",
Flags: []cli.Flag{
&cli.StringFlag{Name: "name", Value: "pat", Usage: "a name to say"},
},
Action: func(_ context.Context, cmd *cli.Command) error {
fmt.Printf("Hello %[1]v\n", cmd.String("name"))
return nil
},
Authors: []any{
&mail.Address{Name: "Oliver Allen", Address: "oliver@toyshop.example.com"},
"gruffalo@soup-world.example.org",
},
Version: "v0.13.12",
}
// Simulate the command line arguments
os.Args = []string{"greet", "--name", "Jeremy"}
if err := cmd.Run(context.Background(), os.Args); err != nil {
// do something with unhandled errors
fmt.Fprintf(os.Stderr, "Unhandled error: %[1]v\n", err)
os.Exit(86)
}
// Output:
// Hello Jeremy
}
func ExampleCommand_Run_subcommand() {
cmd := &cli.Command{
Name: "say",
Commands: []*cli.Command{
{
Name: "hello",
Aliases: []string{"hi"},
Usage: "use it to see a description",
Description: "This is how we describe hello the function",
Commands: []*cli.Command{
{
Name: "english",
Aliases: []string{"en"},
Usage: "sends a greeting in english",
Description: "greets someone in english",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Value: "Bob",
Usage: "Name of the person to greet",
},
},
Action: func(_ context.Context, cmd *cli.Command) error {
fmt.Println("Hello,", cmd.String("name"))
return nil
},
},
},
},
},
}
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
// Simulate the command line arguments
os.Args = []string{"say", "hi", "english", "--name", "Jeremy"}
_ = cmd.Run(ctx, os.Args)
// Output:
// Hello, Jeremy
}
func ExampleCommand_Run_appHelp() {
cmd := &cli.Command{
Name: "greet",
Version: "0.1.0",
Description: "This is how we describe greet the app",
Authors: []any{
&mail.Address{Name: "Harrison", Address: "harrison@lolwut.example.com"},
"Oliver Allen <oliver@toyshop.example.com>",
},
Flags: []cli.Flag{
&cli.StringFlag{Name: "name", Value: "bob", Usage: "a name to say"},
},
Arguments: cli.AnyArguments,
Commands: []*cli.Command{
{
Name: "describeit",
Aliases: []string{"d"},
Usage: "use it to see a description",
Description: "This is how we describe describeit the function",
ArgsUsage: "[arguments...]",
Action: func(context.Context, *cli.Command) error {
fmt.Printf("i like to describe things")
return nil
},
},
},
}
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
// Simulate the command line arguments
os.Args = []string{"greet", "help"}
_ = cmd.Run(ctx, os.Args)
// Output:
// NAME:
// greet - A new cli application
//
// USAGE:
// greet [global options] [command [command options]] [arguments...]
//
// VERSION:
// 0.1.0
//
// DESCRIPTION:
// This is how we describe greet the app
//
// AUTHORS:
// "Harrison" <harrison@lolwut.example.com>
// Oliver Allen <oliver@toyshop.example.com>
//
// COMMANDS:
// describeit, d use it to see a description
// help, h Shows a list of commands or help for one command
//
// GLOBAL OPTIONS:
// --name string a name to say (default: "bob")
// --help, -h show help
// --version, -v print the version
}
func ExampleCommand_Run_commandHelp() {
cmd := &cli.Command{
Name: "greet",
Flags: []cli.Flag{
&cli.StringFlag{Name: "name", Value: "pat", Usage: "a name to say"},
},
Action: func(_ context.Context, cmd *cli.Command) error {
fmt.Fprintf(cmd.Root().Writer, "hello to %[1]q\n", cmd.String("name"))
return nil
},
Commands: []*cli.Command{
{
Name: "describeit",
Aliases: []string{"d"},
Usage: "use it to see a description",
Description: "This is how we describe describeit the function",
ArgsUsage: "[arguments...]",
Action: func(context.Context, *cli.Command) error {
fmt.Println("i like to describe things")
return nil
},
},
},
}
// Simulate the command line arguments
os.Args = []string{"greet", "h", "describeit"}
_ = cmd.Run(context.Background(), os.Args)
// Output:
// NAME:
// greet describeit - use it to see a description
//
// USAGE:
// greet describeit [arguments...]
//
// DESCRIPTION:
// This is how we describe describeit the function
//
// OPTIONS:
// --help, -h show help
}
func ExampleCommand_Run_noAction() {
cmd := &cli.Command{Name: "greet"}
// Simulate the command line arguments
os.Args = []string{"greet"}
_ = cmd.Run(context.Background(), os.Args)
// Output:
// NAME:
// greet - A new cli application
//
// USAGE:
// greet [global options]
//
// GLOBAL OPTIONS:
// --help, -h show help
}
func ExampleCommand_Run_subcommandNoAction() {
cmd := &cli.Command{
Name: "greet",
Commands: []*cli.Command{
{
Name: "describeit",
Aliases: []string{"d"},
Usage: "use it to see a description",
ArgsUsage: "[arguments...]",
Description: "This is how we describe describeit the function",
},
},
}
// Simulate the command line arguments
os.Args = []string{"greet", "describeit"}
_ = cmd.Run(context.Background(), os.Args)
// Output:
// NAME:
// greet describeit - use it to see a description
//
// USAGE:
// greet describeit [options] [arguments...]
//
// DESCRIPTION:
// This is how we describe describeit the function
//
// OPTIONS:
// --help, -h show help
}
func ExampleCommand_Run_shellComplete_bash_withShortFlag() {
cmd := &cli.Command{
Name: "greet",
EnableShellCompletion: true,
Flags: []cli.Flag{
&cli.Int64Flag{
Name: "other",
Aliases: []string{"o"},
},
&cli.StringFlag{
Name: "xyz",
Aliases: []string{"x"},
},
},
}
// Simulate a bash environment and command line arguments
os.Setenv("SHELL", "bash")
os.Args = []string{"greet", "-", "--generate-shell-completion"}
_ = cmd.Run(context.Background(), os.Args)
// Output:
// --other
// --xyz
// --help
}
func ExampleCommand_Run_shellComplete_bash_withLongFlag() {
cmd := &cli.Command{
Name: "greet",
EnableShellCompletion: true,
Flags: []cli.Flag{
&cli.Int64Flag{
Name: "other",
Aliases: []string{"o"},
},
&cli.StringFlag{
Name: "xyz",
Aliases: []string{"x"},
},
&cli.StringFlag{
Name: "some-flag,s",
},
&cli.StringFlag{
Name: "similar-flag",
},
},
}
// Simulate a bash environment and command line arguments
os.Setenv("SHELL", "bash")
os.Args = []string{"greet", "--s", "--generate-shell-completion"}
_ = cmd.Run(context.Background(), os.Args)
// Output:
// --some-flag
// --similar-flag
}
func ExampleCommand_Run_shellComplete_bash_withMultipleLongFlag() {
cmd := &cli.Command{
Name: "greet",
EnableShellCompletion: true,
Flags: []cli.Flag{
&cli.Int64Flag{
Name: "int-flag",
Aliases: []string{"i"},
},
&cli.StringFlag{
Name: "string",
Aliases: []string{"s"},
},
&cli.StringFlag{
Name: "string-flag-2",
},
&cli.StringFlag{
Name: "similar-flag",
},
&cli.StringFlag{
Name: "some-flag",
},
},
}
// Simulate a bash environment and command line arguments
os.Setenv("SHELL", "bash")
os.Args = []string{"greet", "--st", "--generate-shell-completion"}
_ = cmd.Run(context.Background(), os.Args)
// Output:
// --string
// --string-flag-2
}
func ExampleCommand_Run_shellComplete_bash() {
cmd := &cli.Command{
Name: "greet",
EnableShellCompletion: true,
Commands: []*cli.Command{
{
Name: "describeit",
Aliases: []string{"d"},
Usage: "use it to see a description",
Description: "This is how we describe describeit the function",
Action: func(context.Context, *cli.Command) error {
fmt.Printf("i like to describe things")
return nil
},
}, {
Name: "next",
Usage: "next example",
Description: "more stuff to see when generating shell completion",
Action: func(context.Context, *cli.Command) error {
fmt.Printf("the next example")
return nil
},
},
},
}
// Simulate a bash environment and command line arguments
os.Setenv("SHELL", "bash")
os.Args = []string{"greet", "--generate-shell-completion"}
_ = cmd.Run(context.Background(), os.Args)
// Output:
// describeit
// next
// help
}
func ExampleCommand_Run_shellComplete_zsh() {
cmd := &cli.Command{
Name: "greet",
EnableShellCompletion: true,
Commands: []*cli.Command{
{
Name: "describeit",
Aliases: []string{"d"},
Usage: "use it to see a description",
Description: "This is how we describe describeit the function",
Action: func(context.Context, *cli.Command) error {
fmt.Printf("i like to describe things")
return nil
},
}, {
Name: "next",
Usage: "next example",
Description: "more stuff to see when generating bash completion",
Action: func(context.Context, *cli.Command) error {
fmt.Printf("the next example")
return nil
},
},
},
}
// Simulate a zsh environment and command line arguments
os.Args = []string{"greet", "--generate-shell-completion"}
os.Setenv("SHELL", "/usr/bin/zsh")
_ = cmd.Run(context.Background(), os.Args)
// Output:
// describeit:use it to see a description
// next:next example
// help:Shows a list of commands or help for one command
}
func ExampleCommand_Run_sliceValues() {
cmd := &cli.Command{
Name: "multi_values",
Flags: []cli.Flag{
&cli.StringSliceFlag{Name: "stringSlice"},
&cli.FloatSliceFlag{Name: "float64Slice"},
&cli.Int64SliceFlag{Name: "intSlice"},
},
HideHelp: true,
Action: func(ctx context.Context, cmd *cli.Command) error {
for i, v := range cmd.FlagNames() {
fmt.Printf("%d-%s %#v\n", i, v, cmd.Value(v))
}
err := ctx.Err()
fmt.Println("error:", err)
return err
},
}
// Simulate command line arguments
os.Args = []string{
"multi_values",
"--stringSlice", "parsed1,parsed2", "--stringSlice", "parsed3,parsed4",
"--float64Slice", "13.3,14.4", "--float64Slice", "15.5,16.6",
"--intSlice", "13,14", "--intSlice", "15,16",
}
_ = cmd.Run(context.Background(), os.Args)
// Output:
// 0-stringSlice []string{"parsed1", "parsed2", "parsed3", "parsed4"}
// 1-float64Slice []float64{13.3, 14.4, 15.5, 16.6}
// 2-intSlice []int64{13, 14, 15, 16}
// error: <nil>
}
func ExampleCommand_Run_mapValues() {
cmd := &cli.Command{
Name: "multi_values",
Flags: []cli.Flag{
&cli.StringMapFlag{Name: "stringMap"},
},
HideHelp: true,
Action: func(ctx context.Context, cmd *cli.Command) error {
for i, v := range cmd.FlagNames() {
fmt.Printf("%d-%s %#v\n", i, v, cmd.StringMap(v))
}
fmt.Printf("notfound %#v\n", cmd.StringMap("notfound"))
err := ctx.Err()
fmt.Println("error:", err)
return err
},
}
// Simulate command line arguments
os.Args = []string{
"multi_values",
"--stringMap", "parsed1=parsed two", "--stringMap", "parsed3=",
}
_ = cmd.Run(context.Background(), os.Args)
// Output:
// 0-stringMap map[string]string{"parsed1":"parsed two", "parsed3":""}
// notfound map[string]string(nil)
// error: <nil>
}
func ExampleBoolWithInverseFlag() {
flagWithInverse := &cli.BoolWithInverseFlag{
Name: "env",
}
cmd := &cli.Command{
Flags: []cli.Flag{
flagWithInverse,
},
Action: func(_ context.Context, cmd *cli.Command) error {
if flagWithInverse.IsSet() {
if cmd.Bool("env") {
fmt.Println("env is set")
} else {
fmt.Println("no-env is set")
}
}
return nil
},
}
_ = cmd.Run(context.Background(), []string{"prog", "--no-env"})
fmt.Println("flags:", len(flagWithInverse.Names()))
// Output:
// no-env is set
// flags: 2
}
func ExampleCommand_Suggest() {
cmd := &cli.Command{
Name: "greet",
ErrWriter: os.Stdout,
Suggest: true,
HideHelp: false,
HideHelpCommand: true,
CustomRootCommandHelpTemplate: "(this space intentionally left blank)\n",
Flags: []cli.Flag{
&cli.StringFlag{Name: "name", Value: "squirrel", Usage: "a name to say"},
},
Action: func(_ context.Context, cmd *cli.Command) error {
fmt.Printf("Hello %v\n", cmd.String("name"))
return nil
},
}
if cmd.Run(context.Background(), []string{"greet", "--nema", "chipmunk"}) == nil {
fmt.Println("Expected error")
}
// Output:
// Incorrect Usage: flag provided but not defined: -nema
//
// Did you mean "--name"?
//
// (this space intentionally left blank)
}
func ExampleCommand_Suggest_command() {
cmd := &cli.Command{
ErrWriter: os.Stdout,
Name: "greet",
Flags: []cli.Flag{
&cli.StringFlag{Name: "name", Value: "squirrel", Usage: "a name to say"},
},
Action: func(_ context.Context, cmd *cli.Command) error {
fmt.Printf("Hello %v\n", cmd.String("name"))
return nil
},
Commands: []*cli.Command{
{
Name: "neighbors",
HideHelp: true,
HideHelpCommand: true,
Suggest: true,
CustomHelpTemplate: "(this space intentionally left blank)\n",
Flags: []cli.Flag{
&cli.BoolFlag{Name: "smiling"},
},
Action: func(_ context.Context, cmd *cli.Command) error {
if cmd.Bool("smiling") {
fmt.Println("😀")
}
fmt.Println("Hello, neighbors")
return nil
},
},
},
}
if cmd.Run(context.Background(), []string{"greet", "neighbors", "--sliming"}) == nil {
fmt.Println("Expected error")
}
// Output:
// Incorrect Usage: flag provided but not defined: -sliming
//
// Did you mean "--smiling"?
}
|