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.
Building command hierarchy from struct (and value and Tag) is a new feature since cmdr.v2 v2.1.36.
And more, from v2.1.27, FromStruct
can be applied on subcommand building.
Again, since v2.1.38, passing the pointer of the struct variable into FromStruct
will make binding-to-variable(s) feature available.
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 screengroup
: Group field for help screenrequired
: Avaliable forFlag
, theFlag.Required
fieldenv
,envvars
: Avaliable forFlag
, theFlag.EnvVars
field. comma-separated, just likeenv:"USER,USERPROFILE"
head-like
,headLike
: Avaliable forFlag
, theFlag.HeadLike
field。For example,head-like:"true"
。cmdr
: some abilities like:cmdr:"-"
: the field should be ignoredcmdr:"positional"
: when the field has type[]string
, it'll receive the positional args from parsing.
- the others will be ignored
The notable thing is, since v2.1.28, binding-to-var/field is supported. See the section Bind to Variable or Field later.
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.
For Subcmd
Basic Usage
We reached a new mailstone: now you can import children command system into anywhere from a given struct value and tags.
This way can simplify coding work sometimes.
In most cases, we just need a more shorter and rapider approach to code the initial jobs. So here is a sample code to reflect-read struct type and tags from root{}
and build subcommands for a subcmd multi
. After Add()
, the root{}
will be recycled by Go GC.
Running it and get these outputs:
Binding to Variable or Field
You could rightfully build command-line args parser with others library, like flag
, or some 3rd-party libraries. All of them are used to bind a flag onto a variable (or its pointer), as task := flag.String("task", "", "The task you want to add to your to-do list")
have been doing. And you would get the parsed result by *task
.
More of the others libs did some further expansions on it.
The same thing had been done at cmdr.v2 v2.1.38 release.
As a control of the above sample code, you could just supply a pointer of struct value to FromStruct
to get bindings of all of struct fields. It's observed that you must persist the value somewhere so that you can access the result in it later.
The below sample shows how we did these. It can also be integrated to any blueprint
like's app.
The result is,
Reviewing the result outputs, Fa2:yes,man! Fa3:[jesus]
proved the write-back of --fa2
is successful. And also positional-args are record into Fa3
. At another side, F2:ok
means the write-back of -f2=ok
is ok.
Conslusion
In this article, the above abilities of binding to struct field, can be used to speed up your coding.
The future
We could iterate this feature in recent versions to integrated with blueprint
app.
:end:
How is this guide?
Last updated on