-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.go
More file actions
62 lines (56 loc) · 1.16 KB
/
cli.go
File metadata and controls
62 lines (56 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* @Author: ASlowPerson
* @Date: 19-6-29 上午1:38
*/
package main
import (
"fmt"
"os"
)
/*
定义命令行,处理用户输入命令,完成具体函数的调用
*/
type CLI struct {
}
/*
定义使用说明,帮助用户正确使用
*/
const Usage = `
Usage:
./blockchain create "创建区块链"
./blockchain addBlock <需要写入的的数据> "添加区块"
./blockchain print "打印区块链"
`
/*
定义方法,负责解析输入命令
*/
func (cli *CLI) Run() {
// 获取命令行参数
cmds := os.Args
// 判断参数个数,用户至少要输入两个参数
if len(cmds) < 2 {
fmt.Println("输入参数无效,请检查!")
fmt.Println(Usage)
return
}
switch cmds[1] {
case "create":
fmt.Println("创建区块方法被调用!")
cli.createBlockChain()
case "addBlock":
// 判断参数个数是否正确
if len(cmds) != 3 {
fmt.Println("输入参数无效,请检查!")
return
}
fmt.Println("添加区块方法被调用!")
data := cmds[2]
cli.addBlock(data)
case "print":
fmt.Println("打印区块链方法被调用!")
cli.printBlock()
default:
fmt.Println("输入参数无效,请检查!")
fmt.Println(Usage)
}
}