Basics
Lets add some arguments to our greeter app. This allows you to change the behaviour of
the app depending on what argument has been passed. You can lookup arguments by calling
the Args function on cli.Command, e.g.:
package main
import (
"fmt"
"log"
"os"
"context"
"github.com/urfave/cli/v3"
)
func main() {
cmd := &cli.Command{
Action: func(ctx context.Context, cmd *cli.Command) error {
fmt.Printf("Hello %q", cmd.Args().Get(0))
return nil
},
}
if err := cmd.Run(context.Background(), os.Args); err != nil {
log.Fatal(err)
}
}
Running this program with an argument gives the following output
$ greet friend
Hello "Friend"
Any number of arguments can be passed to the greeter app. We can get the number of arguments
and each argument using the Args
package main
import (
"fmt"
"log"
"os"
"context"
"github.com/urfave/cli/v3"
)
func main() {
cmd := &cli.Command{
Action: func(ctx context.Context, cmd *cli.Command) error {
fmt.Printf("Number of args : %d\n", cmd.Args().Len())
var out string
for i := 0; i < cmd.Args().Len(); i++ {
out = out + fmt.Sprintf(" %v", cmd.Args().Get(i))
}
fmt.Printf("Hello%v", out)
return nil
},
}
if err := cmd.Run(context.Background(), os.Args); err != nil {
log.Fatal(err)
}
}
Running this program with an argument gives the following output
$ greet Friend 1 bar 2.0
Number of args : 4
Hello Friend 1 bar 2.0
Note
If the command declares named arguments via the Arguments field, each named argument consumes the positional
arguments it needs from the command line and cmd.Args() only returns the arguments that were not consumed by a
named argument. Use cmd.{Type}Arg(name) to retrieve the value of a named argument. See the
advanced arguments documentation for details.