gitee.com/quant1x/engine@v1.8.4/command/command.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"gitee.com/quant1x/engine/models"
     6  	"gitee.com/quant1x/engine/tracker"
     7  	"gitee.com/quant1x/gox/runtime"
     8  	"gitee.com/quant1x/num"
     9  	cmder "github.com/spf13/cobra"
    10  	goruntime "runtime"
    11  	"strings"
    12  	_ "unsafe" // For go:linkname
    13  )
    14  
    15  //go:linkname cpuModelName internal/cpu.Name
    16  func cpuModelName() string
    17  
    18  const (
    19  	KB = 1024      // Kilo Byte
    20  	MB = 1024 * KB // Mega Byte
    21  	GB = 1024 * MB // Giga Byte
    22  )
    23  
    24  var (
    25  	strategyNumber = uint64(1)              // 策略编号
    26  	businessDebug  = runtime.Debug()        // 业务调试开关
    27  	cpuAvx2        = false                  // AVX2加速状态
    28  	cpuNum         = goruntime.NumCPU() / 2 // cpu数量
    29  )
    30  
    31  var (
    32  	commandDefaultLongFlag = "" // 默认的长标志为空, 主要用在tools
    33  )
    34  
    35  // 输出欢迎语
    36  func printMotd() {
    37  	fmt.Printf("CPU: %s %dCores, AVX2: %t\n", cpuModelName(), goruntime.NumCPU(), num.GetAvx2Enabled())
    38  	fmt.Println()
    39  }
    40  
    41  // 初始化全部子命令
    42  func initSubCommands() {
    43  	initPrint()
    44  	initRepair()
    45  	initUpdate()
    46  	initRules()
    47  	initSafes()
    48  	initBackTesting()
    49  	initTracker()
    50  	initTools()
    51  	initService()
    52  }
    53  
    54  // InitCommands 公开初始化函数
    55  func InitCommands() {
    56  	initSubCommands()
    57  }
    58  
    59  // GlobalFlags engine支持的全部命令
    60  func GlobalFlags() *cmder.Command {
    61  	initSubCommands()
    62  	engineCmd := &cmder.Command{
    63  		Use: Application,
    64  		Run: func(cmd *cmder.Command, args []string) {
    65  			model, err := models.CheckoutStrategy(strategyNumber)
    66  			if err != nil {
    67  				fmt.Println(err)
    68  				return
    69  			}
    70  			// 输出欢迎语
    71  			printMotd()
    72  			barIndex := 1
    73  			tracker.ExecuteStrategy(model, &barIndex)
    74  		},
    75  		PersistentPreRun: func(cmd *cmder.Command, args []string) {
    76  			// 重置全局调试状态
    77  			runtime.SetDebug(businessDebug)
    78  			// AVX2 加速
    79  			num.SetAvx2Enabled(cpuAvx2)
    80  			// 设置CPU最大核数
    81  			runtime.GoMaxProcs(cpuNum)
    82  		},
    83  		PersistentPostRun: func(cmd *cmder.Command, args []string) {
    84  			//
    85  		},
    86  	}
    87  	engineCmd.Flags().Uint64Var(&strategyNumber, "strategy", models.DefaultStrategy, models.UsageStrategyList())
    88  	engineCmd.Flags().IntVar(&models.CountDays, "count", 0, "统计多少天")
    89  	engineCmd.Flags().IntVar(&models.CountTopN, "top", models.AllStockTopN(), "输出前排几名")
    90  	engineCmd.PersistentFlags().BoolVar(&businessDebug, "debug", businessDebug, "打开业务调试开关, 慎重使用!")
    91  	engineCmd.PersistentFlags().BoolVar(&cpuAvx2, "avx2", false, "Avx2 加速开关")
    92  	engineCmd.PersistentFlags().IntVar(&cpuNum, "cpu", cpuNum, "设置CPU最大核数")
    93  	engineCmd.AddCommand(CmdVersion, CmdSafes, CmdBestIP, CmdConfig, CmdTools)
    94  	engineCmd.AddCommand(CmdUpdate, CmdRepair, CmdPrint)
    95  	engineCmd.AddCommand(CmdBackTesting, CmdRules, CmdTracker)
    96  	engineCmd.AddCommand(CmdService)
    97  	return engineCmd
    98  }
    99  
   100  func parseFlagError(err error) (flag, value string) {
   101  	before, _, ok := strings.Cut(err.Error(), "flag:")
   102  	if !ok {
   103  		return
   104  	}
   105  	before = strings.TrimSpace(before)
   106  	//_, err1 := fmt.Sscanf(before, "invalid argument \"%s\" for \"--%s\"", &value, &flag)
   107  	//if err1 != nil {
   108  	//	return
   109  	//}
   110  	arr := strings.Split(before, "\"")
   111  	if len(arr) != 5 {
   112  		return
   113  	}
   114  	value = strings.TrimSpace(arr[1])
   115  	flag = strings.TrimSpace(arr[3])
   116  	arr = strings.Split(flag, "-")
   117  	flag = arr[len(arr)-1]
   118  	return
   119  }