From struct-value and Tag
BuildFrom Example
You can build command system by kinds of forms:
- traditional stream calls (
app.Cmd("verbose", "v").Action(onVerbose)
) - concise modes by
[Create]
and cmd/xxcmd.go - use
[Create.BuildFrom]
to build cmdsys from a struct value via[App.FromStruct]
, see example #example_Create_buildFromStructValue
Getting started from New or Create function.
Builing command hierarchy is a new feature since cmdr.v2 v2.1.36.
This feature is quite like kong
, but a little bit rough. Nonetheless, its abilities are still fully by supplying With()
and Action()
methods to a stuct.
Commonly these keys are useful:
title
,name
: Long title fieldshorts
,short
: comma-separated Short Titles. First of them will be used forFlag.Short
field, else forExtraShorts
aliases
,alias
: Long Alias titlesdesc
,help
: Desc field for displaying in help screenrequired
:Only forFlag
, theFlag.Required
field- the others will be ignored
Run the app like this,
The help screen of rm full
subcommand is:
Using With()
method
Defining each structs is useful to controlling them.
The following structs can be passing into cmdr by Create().BuildFrom(R{})
.
Here the With()
methods allow you customize a command or flag with traditional way.
Using Action()
method
In above example, we could add Action()
method to E
and F
to give the invoking OnAction
callback.
So, there it is.
Specifying default value
To specify default value to a flag, you could setup them in struct-value.
The future
We could iterate this feature in recent versions to integrated with blueprint
app.
:end:
How is this guide?
Last updated on