Cobra provides simple interfaces to create powerful modern CLI interfaces, such as git and go tools. Cobra is also a program that creates CLI programs.
Cobra's structure-based commands, parameters and flags.
Commands represent operations and parameters and flags are modifiers of these operations.
The best application is like reading sentences. Users will know how to use the native application because they will know how to use it.
For example, in the following example, server is the command and port is the flag:
In the following command, we tell Git to clone the url address.
Using Cobra is simple. First, use go get to install the latest version.
Then quote Cobra in your project.
Generally, Cobra-based applications will follow the following organizational structure, but you can also follow your own interfaces:
In Cobra applications, the main.go file is usually very empty. It mainly does only one thing: initialize Cobra.
Cobra provides its own program to create your program and add the commands you want. This is the easiest way to add Cobra to a program.
You can find relevant information here.
To use Cobra, you need to create an empty main.go file and a rootCmd file. You can choose to add other commands where appropriate.
Cobra doesn't need a special constructor. Just create your command.
Ideally, you should put it in app/cmd/root.go.
You will also define flags and handle the configuration init () function.
For example, cmd/root.go
You need to execute the root command in the main function.
Usually the main.go file is very empty. It mainly does only one thing: initialize Cobra.
Other commands are usually defined in their own files in the cmd/ directory.
If you want to create a version command, you can create a cmd/version.go file and write it to the file:
Flags provide modifiers to control the operation of action commands.
After defining the flag, we need to define a variable to associate the flag.
"Persistent" means that every command under this command will be assigned to this flag. For global flags, the "persistent" flag is bound to the root.
By default, Cobra only parses the flags on the target command, and the parent command ignores any local flags. By opening the command. TraverseChildren Cobra, this flag will be parsed before executing any target command.
You can also bind the logo through viper:
In this example, the permanent tag author is bound by viper. Note that when the user does not provide a value for-author, the author will not be assigned a value.
By default, flags are optional. If you want to report an error on the command line when the flag is not set, you can mark it as needed.
Verify that the location parameter can be passed through the Args field of the command.
The following are the built-in verification methods.
Example of setting up custom authentication
In the following example, we define three commands. Two are at the top level, and one (cmdTimes) is a subcommand of one of the top level commands. In this example, since rootCmd does not provide Run, a single root cannot run and there must be subcommands.
We only defined the tag for one command.
More documentation about the logo can be found in /spf 13/pflag.
A more complete example of a big program can be seen in Hugo.
When your program has subcommands, Cobra will automatically add a help command to your program. When you run "apphelp", the help command will be called. In addition, Help supports other input commands. For example, if you have a command named "create" without any other configuration, it will work when you call "apphelp create" corbra.
The following input is automatically generated by Cobra. Nothing is needed except the definition of commands and flags.
Help, like other commands, has no special logic or behavior. In fact, you can help yourself if you want.
You can provide your own help command or template for the default command. Use the following methods:
The last two also apply to any subcommand.
Cobra will return usage when the user provides an invalid label or command.
From the above help, you may realize that the default help will be embedded in the usage and then used as output.
You can provide your own usage functions or templates for Cobra to use.
For example, you can override Help, Methods, and Templates.
If the Version field is set to the root command, Cobra will provide a top-level' -version' tag. Running the program marked' -version' will follow the template version information. Cmd can modify the template. SetVersionTemplate(s string) method.
It is very easy to rerun the method before or after the command runs. The PersistentPreRun and PreRun methods will be executed before running. PersistentPostRun and PostRun methods will be executed after running. If the subcommands themselves do not define a Persistent*Run method, they will be inherited by the subcommands. These methods will be executed according to the following properties:
In the following example, both commands use the above functions. When the subcommand executes, it will execute the root command's PersistentPreRun, but not the root command's PersistentPostRun:
Output:
Cobra will automatically output suggestions when it encounters an "unknown command" error. This makes Cobra behave like a git command when typing mistakes. For example:
Automatically generate suggestions based on registered subcommands. Implementation using Levenshtein distance. Each registered command will match 2 distances (regardless of case) to provide suggestions.
If you want to disable the suggested or weak string distance in the command, use:
or
You can provide clear noun suggestions for commands through suggestions. This feature allows you not to cut a string when it is not similar, but the meaning is similar to your command, and you don't want to set an alias for the command. For example:
Cobra can generate documents based on subcommands, labels, etc. Use the following format:
Cobra can generate a bash completion file. If more information is added to the command, these supplements are very powerful and flexible. More about Bash completion.