gitee.com/quant1x/engine@v1.8.4/command/command_flag.go (about) 1 package command 2 3 import ( 4 "gitee.com/quant1x/engine/cache" 5 "gitee.com/quant1x/exchange" 6 "gitee.com/quant1x/gox/tags" 7 "gitee.com/quant1x/pkg/tablewriter" 8 cmder "github.com/spf13/cobra" 9 "strings" 10 "unsafe" 11 ) 12 13 const ( 14 defaultFlagAll = "all" // 全部 15 ) 16 17 var ( 18 flagAll = cmdFlag[bool]{Name: "all", Value: false, Usage: "全部"} 19 flagBaseData = cmdFlag[string]{Name: "base", Value: "", Usage: "基础数据"} 20 flagFeatures = cmdFlag[string]{Name: "features", Value: "", Usage: "特征数据"} 21 flagStartDate = cmdFlag[string]{Name: "start", Value: exchange.LastTradeDate(), Usage: "开始日期"} 22 flagEndDate = cmdFlag[string]{Name: "end", Value: exchange.LastTradeDate(), Usage: "结束日期"} 23 flagDate = cmdFlag[string]{Name: "date", Value: "", Usage: "日期"} 24 ) 25 26 type Command = cmder.Command 27 28 type cmdFlag[T ~int | ~bool | ~string] struct { 29 Name string 30 Usage string 31 Value T 32 } 33 34 func (cf *cmdFlag[T]) init(cmd *cmder.Command) { 35 switch v := any(cf.Value).(type) { 36 case bool: 37 cmd.Flags().BoolVar((*bool)(unsafe.Pointer(&cf.Value)), cf.Name, v, cf.Usage) 38 case int: 39 cmd.Flags().IntVar((*int)(unsafe.Pointer(&cf.Value)), cf.Name, v, cf.Usage) 40 case string: 41 cmd.Flags().StringVar((*string)(unsafe.Pointer(&cf.Value)), cf.Name, v, cf.Usage) 42 } 43 } 44 45 func commandInit[T ~int | ~bool | ~string](cmd *cmder.Command, cf *cmdFlag[T]) { 46 switch v := any(cf.Value).(type) { 47 case bool: 48 cmd.Flags().BoolVar((*bool)(unsafe.Pointer(&cf.Value)), cf.Name, v, cf.Usage) 49 case int: 50 cmd.Flags().IntVar((*int)(unsafe.Pointer(&cf.Value)), cf.Name, v, cf.Usage) 51 case string: 52 cmd.Flags().StringVar((*string)(unsafe.Pointer(&cf.Value)), cf.Name, v, cf.Usage) 53 } 54 } 55 56 func parseFields(text string) (all bool, keywords []string) { 57 text = strings.TrimSpace(text) 58 if len(text) == 0 { 59 return 60 } 61 arr := strings.Split(text, ",") 62 var list []string 63 for _, v := range arr { 64 v := strings.TrimSpace(v) 65 if v == defaultFlagAll { 66 return true, keywords 67 } else if len(v) == 0 { 68 continue 69 } else { 70 list = append(list, v) 71 } 72 } 73 return false, list 74 } 75 76 type optionUsage struct { 77 Key string `name:"关键字"` 78 Name string `name:"概要"` 79 Provider string `name:"数据源"` 80 Usage string `name:"描述"` 81 } 82 83 // 获取插件的Usage信息 84 func getPluginsUsage(plugins []cache.DataAdapter) string { 85 writer := strings.Builder{} 86 table := tablewriter.NewWriter(&writer) 87 table.SetHeader(tags.GetHeadersByTags(optionUsage{})) 88 table.Append(tags.GetValuesByTags(optionUsage{Key: defaultFlagAll, Name: "全部", Provider: ""})) 89 for _, plugin := range plugins { 90 ou := optionUsage{Key: plugin.Key(), Name: plugin.Name(), Provider: plugin.Owner(), Usage: plugin.Usage()} 91 table.Append(tags.GetValuesByTags(ou)) 92 } 93 table.EnableBorder(false) 94 table.Render() 95 return writer.String() 96 }