package main import ( "flag" "fmt" "os" flags "code.cloudfoundry.org/cli/cf/flags" ) func main() { var context flags.FlagContext context.New() flag.StringVar(&context.Token, "token", "", "API token to authenticate with") flag.StringVar(&context.Organization, "org", "", "Name of target organization") flag.IntVar(&context.Instances, "instances", 1, "Number of instances to deploy") flag.Parse() if context.Token == "" { fmt.Println("API token not provided. Run with the --token flag.") os.Exit(1) } // rest of your program logic here using context values }
package main import ( "flag" "fmt" flags "code.cloudfoundry.org/cli/cf/flags" ) func main() { var context flags.FlagContext context.New() flag.StringVar(&context.Token, "token", "", "") flag.StringVar(&context.Organization, "org", "", "") flag.Parse() if !context.IsSet("token") { fmt.Println("API token not provided. Run with the --token flag.") } if context.IsSet("org") { fmt.Printf("Target organization set to %s\n", context.Organization) } }In this example, we create an instance of the FlagContext struct and parse command line flags with the `flag` package. We use the `IsSet` method of the FlagContext struct to determine whether certain flags were passed in or not, and produce output based on the results.