hzDocs

(Root)

cmdr 顶级例程

math

c=±a2+b2c = \pm\sqrt{a^2 + b^2}

highlight line

<div>Hello World</div>
<div>Hello World</div>
<div>Goodbye</div>
<div>Hello World</div>

highlight words

const config = {
  reactStrictMode: true,
};
 
diff
 
```ts
console.log('hewwo'); // [!code --]
console.log('hello'); // [!code ++]

tabs

Actions

响应函数(Action)

Action 函数将会提供一个响应函数。

当一个命令被命中时,它的 Action 响应函数将会获得控制权。

值得注意的是,一旦定义了 Action 函数,那么应用程序运行时就不会自动显示帮助屏了,除非你使用 wget-cover --help (或者 -h, -? 等)等方式(这也是 POSIX 兼容的方案)。

对于标志来说,Action 也可以被用于响应,但更多的情况是通过标志的 OnSet 来完成自定义的标志值的校验。

 cmdr.NewBool().
  Titles("background", "b", "bg").
  Description("go to background after startup").
  Group(cStartup).
  OnSet(func(keyPath string, value interface{}) {
   if value == true {
    panic("unexpected value: true")
   }
  }).
  AttachTo(root)

See Also: here

关于 PreActionPostAction

对于 RootCommand 来说,除了能够定义应用程序级别的信息,例如 appName,version,banner 等等之外,需要注意的就是它的 PreActionPostAction 函数了。在 RootCommand 的 Action 函数被调用之前和之后,这两个函数会分别获得执行控制权。

它们的特殊之处在于,对于子命令的 Action 函数被执行的情况,除了子命令的 PreAction 和 PostAction 会有机会被执行之外,RootCommand 的 PreAction 和 PostAction 也会被执行。所以它们隐含着全局的前置和后置调用的含义——全局的 Pre/PostAction。

AddGlobalPreAction & AddGlobalPostAction

进一步地,你可以反复多次指定所谓的全局 Pre/PostAction。例如 examples/actions 中的片段

func buildRootCmd() (rootCmd *cmdr.RootCommand) {
 root := cmdr.Root(appName, cmdr_examples.Version).
  AddGlobalPreAction(func(cmd *cmdr.Command, args []string) (err error) {
   fmt.Println("# global pre-action 1")
   return
  }).
  AddGlobalPreAction(func(cmd *cmdr.Command, args []string) (err error) {
   fmt.Println("# global pre-action 2")
   return
  }).
  AddGlobalPostAction(func(cmd *cmdr.Command, args []string) {
   fmt.Println("# global post-action 1")
  }).
  AddGlobalPostAction(func(cmd *cmdr.Command, args []string) {
   fmt.Println("# global post-action 2")
  }).
  Copyright(copyright, "hedzr").
  Description(desc, longDesc).
  Examples(examples)
 rootCmd = root.RootCommand()
 
 cmdr.NewBool(false).
  Titles("enable-ueh", "ueh").
  Description("Enables the unhandled exception handler?").
  AttachTo(root)
 
 soundex(root)
 panicTest(root)
 
 return
}

OnSet

在上文中已经提到过 OnSet 的用法:

  OnSet(func(keyPath string, value interface{}) {
   if value == true {
    panic("unexpected value: true")
   }
  }).

除了可以被用于完成校验任务之外,OnSet 也可以被用于设置有关系的全局变量,翻转某些特定业务逻辑等。

🔚

How is this guide?

Edit on GitHub

Last updated on

On this page