Ejemplo n.º 1
0
func (v *CmdConfigSet) ParseArgv(ctx *cli.Context) error {
	flags := 0
	args := ctx.Args()

	if len(ctx.Args()) < 1 {
		return fmt.Errorf("Need 1 or more arguments for set")
	}

	v.Path = args[0]

	if ctx.Bool("clear") {
		flags++
		v.DoClear = true
	}
	if ctx.Bool("null") {
		flags++
		v.Value.IsNull = true
	}
	if ctx.Bool("int") {
		if len(args) <= 1 {
			return fmt.Errorf("Missing int value argument")
		}
		flags++
		i, err := strconv.ParseInt(args[1], 10, 64)
		if err != nil {
			return err
		}
		tmp := int(i)
		v.Value.I = &tmp
	}
	if ctx.Bool("bool") {
		if len(args) <= 1 {
			return fmt.Errorf("Missing bool value argument")
		}
		flags++
		b, err := strconv.ParseBool(args[1])
		if err != nil {
			return err
		}
		v.Value.B = &b
	}

	if ctx.Bool("obj") {
		if len(args) <= 1 {
			return fmt.Errorf("Missing obj value argument")
		}
		flags++
		s := args[1]
		v.Value.O = &s
	}

	if ctx.Bool("string") {
		flags++
	}

	if flags > 1 {
		return fmt.Errorf("Can only specify one of -c, -n, -i, -b or -s")
	}

	if ctx.Bool("string") || flags == 0 {
		if len(args) <= 1 {
			return fmt.Errorf("Missing string value argument")
		}
		s := args[1]
		if !ctx.IsSet("string") && v.looksLikeBool(s) {
			return fmt.Errorf("The value %q looks like a boolean value, not a string.  Use the -b flag to set a bool, or -s to confirm this is a string value.", s)
		}
		v.Value.S = &s
	}
	return nil
}