### cmd包分析
#### cmd下面总共有13个子包,除了util包之外,每个子包都有一个主函数,每个主函数的init方法中都定义了该主函数支持的命令,如
##### geth包下面的:
```
func init() {
// Initialize the CLI app and start Geth
app.Action = geth
app.HideVersion = true // we have a command to print the version
app.Copyright = "Copyright 2013-2017 The go-ethereum Authors"
app.Commands = []cli.Command{
// See chaincmd.go:
initCommand,
importCommand,
exportCommand,
copydbCommand,
removedbCommand,
dumpCommand,
// See monitorcmd.go:
monitorCommand,
// See accountcmd.go:
accountCommand,
walletCommand,
// See consolecmd.go:
consoleCommand,
attachCommand,
javascriptCommand,
// See misccmd.go:
makecacheCommand,
makedagCommand,
versionCommand,
bugCommand,
licenseCommand,
// See config.go
dumpConfigCommand,
}
sort.Sort(cli.CommandsByName(app.Commands))
}
```
###### 再单独分析initCommand:
```
initCommand = cli.Command{
Action: utils.MigrateFlags(initGenesis),
Name: "init",
Usage: "Bootstrap and initialize a new genesis block",
ArgsUsage: "<genesisPath>",
Flags: []cli.Flag{
utils.DataDirFlag,
utils.LightModeFlag,
},
Category: "BLOCKCHAIN COMMANDS",
Description: `
The init command initializes a new genesis block and definition for the network.
This is a destructive action and changes the network in which you will be
participating.
```
###### 其中Name是对应命令的指令,action是调用该指令去完成的动作,usage表示用途,arguUsage显示该命令后面跟的参数个数以及每个参数的意义,
###### 该init方法其实就是去初始化创世块,flags代表的是这个子命令额外可以执行的命令,如改init命令可以携带两个参数,点进去utils.DataDirFlag可以看到:
```
// General settings
DataDirFlag = DirectoryFlag{
Name: "datadir",
Usage: "Data directory for the databases and keystore",
Value: DirectoryString{node.DefaultDataDir()},
}
```
*
__可以用 --datadir [dir]来指定数据库的路径,如果没有指定由于该参数有value所以会启用默认的路径,也是home目录下面的.ethereum.__
*
__/cmd/wnode/main.go 通过连接其他节点启动__
*
__/cmd/geth /cmd/swarm 都是定义了很多命令__
### eth下cmd的rlpdump子包,该包的主要作用从给定文件中转储RLP数据以可读的形式.如果文件名被省略,数据将从stdin中读取
/rlpdump
##### 解码rlp的数据
###### rlpdump的command的help
```
Usage: /tmp/___cmd_rlpdump_test [-noascii] [-hex <data>] [filename]
-hex string
dump given hex data
-noascii
don't print ASCII strings readably
-single
print only the first element, discard the rest
Dumps RLP data from the given file in readable form.
If the filename is omitted, data is read from stdin.
```
###### example1:
```
demo command: --hex f872f870845609a1ba64c0b8660480136e573eb81ac4a664f8f76e4887ba927f791a053ec5ff580b1037a8633320ca70f8ec0cdea59167acaa1debc07bc0a0b3a5b41bdf0cb4346c18ddbbd2cf222f54fed795dde94417d2e57f85a580d87238efc75394ca4a92cfe6eb9debcc3583c26fee8580
success_result_demo:
[
[
5609a1ba,
"d",
[],
0480136e573eb81ac4a664f8f76e4887ba927f791a053ec5ff580b1037a8633320ca70f8ec0cdea59167acaa1debc07bc0a0b3a5b41bdf0cb4346c18ddbbd2cf222f54fed795dde94417d2e57f85a580d87238efc75394ca4a92cfe6eb9debcc3583c26fee85,
"",
],
]
```
###### example2:
```
demo command: --noascii --hex CE0183FFFFFFC4C304050583616263
success_result_demo:
[
01,
ffffff,
[
[
04,
05,
05,
],
],
616263,
]
```
### cmd包下的p2psim子包的分析, p2psim is a command line client for the HTTP API
##### 首先我们启动对应的main函数,对应的启动参数是
`--help`
,来查看该包下所有命令的使用,结果如下:
```
NAME:
___go_build_main_go__1_ - devp2p simulation command-line client
USAGE:
___go_build_main_go__1_ [global options] command [command options] [arguments...]
VERSION:
0.0.0
COMMANDS:
show show network information
events stream network events
snapshot create a network snapshot to stdout
load load a network snapshot from stdin
node manage simulation nodes
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--api value simulation API U