查询FlagSet的用法

时间:2018-09-12 02:00:31

标签: go flags

我正在尝试构建一个CLI,在其中我希望一组三个参数一起出现。

command --alpha "value-a" --bravo "value-b" --charlie "value-c"

这就是我要尝试的方式:

import (
    "github.com/spf13/pflag"
)

var (
    alpha         string
    bravo         string
    charlie       string
)

abcFlagSet := pflag.NewFlagSet("alpha", pflag.ContinueOnError)
abcFlagSet.StringVar(&bravo, "bravo", "", "bravo-description")
abcFlagSet.StringVar(&charlie, "charlie", "", "charlie-description")
cmd.Flags().AddFlagSet(abcFlagSet)

这可以正确识别bravocharlie标志。但是,我认为它将alpha视为command的子命令,并且不解析分配给它的值,即value-aFlagSet在此用例中是否使用错误?我应该如何解析这种情况,即三个参数可以全部出现或根本不出现?

1 个答案:

答案 0 :(得分:1)

https://github.com/spf13/pflag/blob/298182f68c66c05229eb03ac171abe6e309ee79a/flag.go#L1202-L1213,如此处所述,您正在形成一个名称为SELECT user_id, count(*) as cnt from auction_details a JOIN products p where p.product_id = a.product_id AND p.end_time ='2018-09-12 08:35:30' GROUP by bidprice order by bidprice 的{​​{1}}。显然,您的代码不将其视为标志。定义名称为DELIMITER | DO BEGIN DECLARE cnt AS INT; SELECT user_id, count(*) as cnt from auction_details a JOIN products p where p.product_id = a.product_id AND p.end_time ='2018-09-12 08:35:30' GROUP by bidprice order by bidprice DESC LIMIT 1; IF cnt=1 THEN SET @userid = SELECT user_id from auction_details a JOIN products p where p.product_id = a.product_id AND p.end_time ='2018-09-12 08:35:30' GROUP by bidprice order by bidprice DESC LIMIT 1; update products p join auction_details a on a.product_id = p.product_id join users u on u.user_id = a.user_id set p.winner = @userid WHERE p.end_time ='2018-09-12 08:35:30'; END IF; 的新标志的正确方法是

FlagSet