Example #1
0
func TestApp_RunAsSubcommandParseFlags(t *testing.T) {
	var context *cli.Context

	a := cli.NewApp()
	a.Commands = []cli.Command{
		{
			Name: "foo",
			Action: func(c *cli.Context) {
				context = c
			},
			Flags: []cli.Flag{
				cli.StringFlag{
					Name:  "lang",
					Value: "english",
					Usage: "language for the greeting",
				},
			},
			Before: func(_ *cli.Context) error { return nil },
		},
	}
	a.Run([]string{"", "foo", "--lang", "spanish", "abcd"})

	expect(t, context.Args().Get(0), "abcd")
	expect(t, context.String("lang"), "spanish")
}
Example #2
0
func buildCmd(c *cli.Context) {
	exit := func(err error) {
		if err == nil {
			os.Exit(0)
		} else {
			log.Crit("command failed", "err", err)
			os.Exit(1)
		}
	}

	opts := &Options{
		Version:    c.String("version"),
		SrcPath:    c.String("src"),
		TargetPath: c.String("target"),
	}

	platforms := c.String("platforms")
	if platforms == "" {
		opts.Platforms = defaultPlatforms
	} else {
		opts.Platforms = make([]Platform, 0)
		for _, pString := range strings.Split(platforms, " ") {
			parts := strings.Split(pString, "_")
			if len(parts) != 2 {
				exit(fmt.Errorf("Invalid platform string: %v", pString))
			}
			opts.Platforms = append(opts.Platforms, Platform{parts[0], parts[1]})
		}
	}

	exit(Build(opts))
}