Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7784
  • 博文数量: 2
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 30
  • 用 户 组: 普通用户
  • 注册时间: 2017-11-21 19:11
文章分类
文章存档

2017年(2)

我的朋友
最近访客

分类: LINUX

2017-11-25 16:42:33

command包是主要是用于分析传入的参数, 并且根据传入的参数,执行特定的命令。 
Command 结构中最重要的run 成员和 UsageLine, 这个一个函数类型,每个参数对应一个特定的函数进行处理。  

点击(此处)折叠或打开

  1. package command

  2. import (
  3.     "flag"
  4.     "fmt"
  5.     "os"
  6.     "strings"
  7. )

  8. /************************************
  9.   command 目录下定义了全局变量制指针,分别是 cmdBenchmark
  10.   Backup 等文件 每个文件都是一个 Command 的初始化,以及run函数
  11. **********************************/
  12. var Commands = []*Command{
  13.     cmdBenchmark,
  14.     cmdBackup,
  15.     cmdCompact,
  16.     cmdCopy,
  17.     cmdFix,
  18.     cmdServer,
  19.     cmdMaster,
  20.     cmdFiler,
  21.     cmdUpload,
  22.     cmdDownload,
  23.     cmdShell,
  24.     cmdVersion,
  25.     cmdVolume,
  26.     cmdExport,
  27.     cmdMount,
  28. }

  29. /**********************************************
  30.  一个Command 类型, 一个run函数,
  31.   几个字符串是用法,
  32. **********************************************/
  33. type Command struct {
  34.     // Run runs the command.
  35.     // The args are the arguments after the command name.
  36.     Run func(cmd *Command, args []string) bool

  37.     // UsageLine is the one-line usage message.
  38.     // The first word in the line is taken to be the command name.
  39.     UsageLine string

  40.     // Short is the short description shown in the 'go help' output.
  41.     Short string

  42.     // Long is the long message shown in the 'go help <this-command>' output.
  43.     Long string

  44.     // Flag is a set of flags specific to this command.
  45.     Flag flag.FlagSet

  46.     IsDebug *bool
  47. }

  48. // Name returns the command's name: the first word in the usage line.
  49. /**********************************************
  50.  返回的是 UsageLine 空格前的内容 UsageLine: "backup -dir=. -volumeId=234 -server=localhost:9333",

  51. **********************************************/
  52. func (c *Command) Name() string {
  53.     name := c.UsageLine
  54.     i := strings.Index(name, " ")
  55.     if i >= 0 {
  56.         name = name[:i]
  57.     }
  58.     return name
  59. }

  60. /*******************************************
  61.    打印一下这些信息
  62. *******************************************/
  63. func (c *Command) Usage() {
  64.     fmt.Fprintf(os.Stderr, "Example: weed %s\n", c.UsageLine)
  65.     fmt.Fprintf(os.Stderr, "Default Usage:\n")
  66.     c.Flag.PrintDefaults() // 没细看
  67.     fmt.Fprintf(os.Stderr, "Description:\n")
  68.     fmt.Fprintf(os.Stderr, " %s\n", strings.TrimSpace(c.Long)) //用 strings.TrimSpace(s) 来剔除字符串开头和结尾的空白符号;
  69.     os.Exit(2)
  70. }

  71. // Runnable reports whether the command can be run; otherwise
  72. // it is a documentation pseudo-command such as importpath.
  73. func (c *Command) Runnable() bool {
  74.     return c.Run != nil
  75. }

阅读(1106) | 评论(0) | 转发(0) |
0

上一篇:goLang 环境搭建

下一篇:没有了

给主人留下些什么吧!~~