当前位置:网站首页>Share a go auxiliary command line
Share a go auxiliary command line
2022-07-22 09:58:00 【It workers】
Open an additional process , Loop to get input , Then parse the input and register through cmdmap Find the corresponding callback To call .
Commands and parameters are separated by spaces , Such as :
The complete code is as follows :
package ctl
import (
"bufio"
"errors"
"os"
"strings"
)
type cmdinfo struct {
cmd string
desc string
fn func(str string)
}
var cbmap map[string]*cmdinfo
func init() {
go run()
cbmap = make(map[string]*cmdinfo)
Register("test", "This is a test", func(arg string) {
println("Test callback of test command.")
})
Register("help", "Show all commands", func(arg string) {help()})
}
func Register(key string, desc string, fn func(string)) error {
if _, ok := cbmap[key];ok{
return errors.New("Repeat register cb of ctl:"+ key)
}
cbmap[key] = &cmdinfo{key, desc, fn}
return nil
}
func help(){
for key, cmd := range(cbmap) {
println("*\t", key, "\t\t ", cmd.desc)
}
}
func run() {
for {
input := bufio.NewScanner(os.Stdin)
input.Scan()
in := input.Text()
// Commands and parameters are separated by spaces
strs := strings.Split(in, " ")
cmd := strs[0]
arg := ""
if len(strs)==2{
arg = strs[1]
}
if cmd, ok := cbmap[cmd];ok{
cmd.fn(arg)
} else {
println("Invalid command, input ‘help’ to get more information.")
}
}
}
Pay attention to process safety !!
Pay attention to process safety !!
Pay attention to process safety !!
边栏推荐
- 黑马瑞吉外卖之后台登录与退出功能开发
- Review of Biochemistry III. sugars
- Section 24 of Chapter 2: document operation: Writing
- Robot slam navigation core technology and practice Season 1: Chapter 1_ Necessary knowledge for ROS entry
- 智能科学创新讲堂 | 自动化所何晖光: 基于视觉信息编解码的深度学习类脑机制研究
- 【OpenCV 例程300篇】234. 特征提取之主成分分析(PCA)
- 第三章 第一节:函数的概念
- MxCAD5.2 20190704更新
- 在东方财富网上开户安全吗,开户有门槛的吗?
- How win10 sends icons to the desktop
猜你喜欢
随机推荐
List集合
第二章 第二十五节:文件操作:with和复制
关于for...in和for...of理解和使用
分享一个GO 辅助命令行
自定义滚动条
关于最近面试总结
高校学生返校名单信息生成- 腾讯云场景连接器全国高校创新赛
Section 20 of Chapter 2: operators one
设置浮动无法浮动到上一个div的上面
互联网寒冬,3个月如何从功能测试进阶自动化测试?【附学习指南】
setAttribute、getAttribute、removeAttribute
Read the paper with me - multi model text recognition network
Wechat applet image image is 100% wide and highly adaptive
Comment win10 envoie une icône au Bureau
Win10如何把圖標發送到桌面
Wechat applet navigation bar sliding transparency change
Section 17 of Chapter 2: Dictionary knowledge supplement
Section 26 of Chapter II: document operation: document modification
call、apply、bind以及bind的兼容问题
关于IE6浏览器出现的问题的解决