hzDocs

Command: DynCommand

loading dynamic commands at runtime

Lists dynamic commands at runtime

cmdr allows scanning and collecting subcmds at runtime.

Sample in concise

The example app concise demostrates dyn-cmd in codes of cmd jump.

At same time, jump still adds its normal subcmd to programatically.

./examples/tiny1/main.go
package main
 
import (
	"context"
	"os"
 
	"github.com/hedzr/cmdr/v2"
	"github.com/hedzr/cmdr/v2/examples/cmd"
	"github.com/hedzr/cmdr/v2/pkg/logz"
)
 
const (
	appName = "concise"
	desc    = `concise version of tiny app.`
	version = cmdr.Version
	author  = `The Example Authors`
)
 
func main() {
	app := cmdr.Create(appName, version, author, desc).
		WithAdders(cmd.Commands...).
		Build()
 
	ctx := context.Background()
	if err := app.Run(ctx); err != nil {
		logz.ErrorContext(ctx, "Application Error:", "err", err) // stacktrace if in debug mode/build
		os.Exit(app.SuggestRetCode())
	} else if rc := app.SuggestRetCode(); rc != 0 {
		os.Exit(rc)
	}
}

外部脚本文件

cmdr 在 ci/ 中附加了一些短小的 Shell 脚本文件以便对 concise app 进行演示支持。

cpu
disk
memory

The shell scripts, cpu, memory and disk in the directory ./ci/usr.local.lib/concise/ext/ will be added as subcmd of jump.

The special directory ./ci/usr.local.lib/concise/ext/ is for development. For the product mode, cmdr will locate the system folder at /usr/local/lib/concise/ext/.

Run

The result of the example app is,

$ go run ./examples/tiny/concise jump
concise v2.1.1 ~ Copyright © 2025 by The Example Authors ~ All Rights Reserved.
 
Usage:
 
  $ concise jump [Options...][files...]
 
Description:
 
  jump command
 
Examples:
 
  jump example
 
Commands:
  to                                          to command [Since: v0.1.1]
  cpu                                         ci/pkg/usr.local.lib/concise/ext/cpu
  disk                                        ci/pkg/usr.local.lib/concise/ext/disk
  memory                                      ci/pkg/usr.local.lib/concise/ext/memory
 
Global Flags:
  [Misc]
    -h, --help,--info,--usage                 Show this help screen (-?) [Env: HELP] (Default: false)
 
Type '-h'/'-?' or '--help' to get command help screen.
More: '-D'/'--debug', '-V'/'--version', '-#'/'--build-info', '--no-color'...
$ go run ./examples/tiny/concise jump cpu
100.5%
$

The dyncmds have been listed in help screen, and they will be launched properly.

Backstages

The backstage of these dyncmds is in litecmd.go. At cmdr preparing time, it loads the dyncmds from certain a target folder, make them invokeable (by InvokeShell(...)). For the detail, see also function onEvalJumpSubCommands.

Basically, loading dyncmds needs specifying the callback to OnEvaluateSubCommands(cb),

OnEvaluateSubCommands(dyncmd.OnEvalJumpSubCommands).

cmdr will request the callback handler at these time:

  • preprocess() at bootstrap time
  • printing help screen
  • parse() parsing the commandline
  • exec() invoking the parsed result
  • ...

An expcetion, the callback handler will be ignored in ~~tree listing.

Another case is OnEvaluateSubCommandsOnce(), which will be called back just once.

DynFlags ?

cmdr supports calculating dynflags indeed.

Please looking for app.Flg().OnEvaluateFlags() / OnEvaluateFlagsOnce().

额外的话题

How is this guide?

Edit on GitHub

Last updated on

On this page