/
cli.go
76 lines (70 loc) · 1.89 KB
/
cli.go
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"fmt"
"os"
"strconv"
)
// 用来接收命令行参数并且控制区块链操作的文件
type CLI struct {
bc *BlockChain
}
const Usage = `
printChain "Print blockchain"
getBalance --address ADDRESS "Get balance of a specified address"
send FROM TO AMOUNT MINER DATA "From FROM transfer AMOUNT to TO, mined by MINER with message DATA"
newWallet "Add a new wallet"
listAddresses "list all wallet addresses"
`
// 接受参数的动作放到一个函数中
func (cli *CLI) Run() {
args := os.Args
if len(args) < 2 {
// fmt.Printf("Invalid command")
// fmt.Printf(Usage)
}
// 2. 分析命令
cmd := args[0]
if len(args) > 1 {
cmd = args[1]
}
switch cmd {
case "printChain":
// 打印区块
fmt.Printf("print block\n")
cli.PrintBlockChain()
case "getBalance":
fmt.Printf("get balance\n")
if len(args) == 4 && args[2] == "--address" {
address := args[3]
cli.GetBalance(address)
}
case "send":
fmt.Printf("start sending...\n")
if len(args) != 7 {
fmt.Printf("参数个数错误\n")
fmt.Printf(Usage)
return
}
// send FROM TO AMOUNT MINER DATA "由FROM转AMOUNT给TO,由MINER挖矿,同时写入DATA"
from := args[2]
to := args[3]
// func ParseFloat(s string, bitSize int) (float64, error) {
amount, _ := strconv.ParseFloat(args[4], 64)
miner := args[5]
data := args[6]
fmt.Printf("transfer information: %s %s %f %s %s\n", from, to, amount, miner, data)
cli.Send(from, to, amount, miner, data)
case "newWallet":
fmt.Printf("creating new wallet.......\n")
cli.NewWallet()
case "listAddresses":
fmt.Printf("list all wallet address\n")
cli.listAddresses()
default:
// fmt.Printf("Invalid command")
// fmt.Printf(Usage)
}
// 分析命令
// 1. 添加区块 2. 打印区块
// 执行相应动作
}