From 9584398b77eb7baf761bd79a5d9895f90f74181a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 28 Jun 2022 19:39:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=91=BD=E4=BB=A4=E8=A1=8C?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cli.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 cli.go diff --git a/cli.go b/cli.go new file mode 100644 index 0000000..97d4341 --- /dev/null +++ b/cli.go @@ -0,0 +1,44 @@ +// Package command ... +// +// Description : command ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2022-06-28 19:33 +package command + +import ( + "flag" + + "git.zhangdeman.cn/zhangdeman/command/define" + "git.zhangdeman.cn/zhangdeman/util" +) + +// Cli 获取CLI实例 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 19:34 2022/6/28 +func Cli(paramList []*define.CliParam) *cli { + return &cli{paramList: paramList} +} + +type cli struct { + paramList []*define.CliParam +} + +// Parse 解析命令行参数 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 19:34 2022/6/28 +func (c *cli) Parse() error { + for _, item := range c.paramList { + inputValue := flag.String(item.Name, item.DefaultValue, item.Description) + if err := util.ConvertAssign(item.Receiver, *inputValue); nil != err { + return err + } + } + flag.Parse() + return nil +}