cosmossdk.io/client/v2@v2.0.0-beta.1/README.md (about) 1 --- 2 sidebar_position: 1 3 --- 4 5 # AutoCLI 6 7 :::note Synopsis 8 This document details how to build CLI and REST interfaces for a module. Examples from various Cosmos SDK modules are included. 9 ::: 10 11 :::note Pre-requisite Readings 12 13 * [CLI](https://docs.cosmos.network/main/core/cli) 14 15 ::: 16 17 The `autocli` (also known as `client/v2`) package is a [Go library](https://pkg.go.dev/cosmossdk.io/client/v2/autocli) for generating CLI (command line interface) interfaces for Cosmos SDK-based applications. It provides a simple way to add CLI commands to your application by generating them automatically based on your gRPC service definitions. Autocli generates CLI commands and flags directly from your protobuf messages, including options, input parameters, and output parameters. This means that you can easily add a CLI interface to your application without having to manually create and manage commands. 18 19 ## Overview 20 21 `autocli` generates CLI commands and flags for each method defined in your gRPC service. By default, it generates commands for each gRPC services. The commands are named based on the name of the service method. 22 23 For example, given the following protobuf definition for a service: 24 25 ```protobuf 26 service MyService { 27 rpc MyMethod(MyRequest) returns (MyResponse) {} 28 } 29 ``` 30 31 For instance, `autocli` would generate a command named `my-method` for the `MyMethod` method. The command will have flags for each field in the `MyRequest` message. 32 33 It is possible to customize the generation of transactions and queries by defining options for each service. 34 35 ## Application Wiring 36 37 Here are the steps to use AutoCLI: 38 39 1. Ensure your app's modules implements the `appmodule.AppModule` interface. 40 2. (optional) Configure how behave `autocli` command generation, by implementing the `func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions` method on the module. 41 3. Use the `autocli.AppOptions` struct to specify the modules you defined. If you are using `depinject` / app v2, it can automatically create an instance of `autocli.AppOptions` based on your app's configuration. 42 4. Use the `EnhanceRootCommand()` method provided by `autocli` to add the CLI commands for the specified modules to your root command. 43 44 :::tip 45 AutoCLI is additive only, meaning _enhancing_ the root command will only add subcommands that are not already registered. This means that you can use AutoCLI alongside other custom commands within your app. 46 ::: 47 48 Here's an example of how to use `autocli` in your app: 49 50 ``` go 51 // Define your app's modules 52 testModules := map[string]appmodule.AppModule{ 53 "testModule": &TestModule{}, 54 } 55 56 // Define the autocli AppOptions 57 autoCliOpts := autocli.AppOptions{ 58 Modules: testModules, 59 } 60 61 // Create the root command 62 rootCmd := &cobra.Command{ 63 Use: "app", 64 } 65 66 if err := appOptions.EnhanceRootCommand(rootCmd); err != nil { 67 return err 68 } 69 70 // Run the root command 71 if err := rootCmd.Execute(); err != nil { 72 return err 73 } 74 ``` 75 76 ### Keyring 77 78 `autocli` uses a keyring for key name resolving and signing transactions. Providing a keyring is optional, but if you want to use the `autocli` generated commands to sign transactions, you must provide a keyring. 79 80 :::tip 81 This provides a better UX as it allows to resolve key names directly from the keyring in all transactions and commands. 82 83 ```sh 84 <appd> q bank balances alice 85 <appd> tx bank send alice bob 1000denom 86 ``` 87 88 ::: 89 90 The keyring to be provided to `client/v2` must match the `client/v2` keyring interface. 91 The keyring should be provided in the `appOptions` struct as follows, and can be gotten from the client context: 92 93 :::tip 94 The Cosmos SDK keyring and Hubl keyring both implement the `client/v2/autocli/keyring` interface, thanks to the following wrapper: 95 96 ```go 97 keyring.NewAutoCLIKeyring(kb) 98 ``` 99 100 ::: 101 102 :::warning 103 When using AutoCLI the keyring will only be created once and before any command flag parsing. 104 ::: 105 106 ```go 107 // Set the keyring in the appOptions 108 appOptions.Keyring = keyring 109 110 err := autoCliOpts.EnhanceRootCommand(rootCmd) 111 ... 112 ``` 113 114 ## Signing 115 116 `autocli` supports signing transactions with the keyring. 117 The [`cosmos.msg.v1.signer` protobuf annotation](https://github.com/cosmos/cosmos-sdk/blob/9dd34510e27376005e7e7ff3628eab9dbc8ad6dc/docs/build/building-modules/05-protobuf-annotations.md#L9) defines the signer field of the message. 118 This field is automatically filled when using the `--from` flag or defining the signer as a positional argument. 119 120 :::warning 121 AutoCLI currently supports only one signer per transaction. 122 ::: 123 124 ## Module Wiring & Customization 125 126 The `AutoCLIOptions()` method on your module allows to specify custom commands, sub-commands or flags for each service, as it was a `cobra.Command` instance, within the `RpcCommandOptions` struct. Defining such options will customize the behavior of the `autocli` command generation, which by default generates a command for each method in your gRPC service. 127 128 ```go 129 *autocliv1.RpcCommandOptions{ 130 RpcMethod: "Params", // The name of the gRPC service 131 Use: "params", // Command usage that is displayed in the help 132 Short: "Query the parameters of the governance process", // Short description of the command 133 Long: "Query the parameters of the governance process. Specify specific param types (voting|tallying|deposit) to filter results.", // Long description of the command 134 PositionalArgs: []*autocliv1.PositionalArgDescriptor{ 135 {ProtoField: "params_type", Optional: true}, // Transform a flag into a positional argument 136 }, 137 } 138 ``` 139 140 ### Specifying Subcommands 141 142 By default, `autocli` generates a command for each method in your gRPC service. However, you can specify subcommands to group related commands together. To specify subcommands, use the `autocliv1.ServiceCommandDescriptor` struct. 143 144 This example shows how to use the `autocliv1.ServiceCommandDescriptor` struct to group related commands together and specify subcommands in your gRPC service by defining an instance of `autocliv1.ModuleOptions` in your `autocli.go`. 145 146 ```go reference 147 https://github.com/cosmos/cosmos-sdk/blob/v0.50.0-beta.0/x/gov/autocli.go#L94-L97 148 ``` 149 150 ### Positional Arguments 151 152 By default `autocli` generates a flag for each field in your protobuf message. However, you can choose to use positional arguments instead of flags for certain fields. 153 154 To add positional arguments to a command, use the `autocliv1.PositionalArgDescriptor` struct, as seen in the example below. Specify the `ProtoField` parameter, which is the name of the protobuf field that should be used as the positional argument. In addition, if the parameter is a variable-length argument, you can specify the `Varargs` parameter as `true`. This can only be applied to the last positional parameter, and the `ProtoField` must be a repeated field. 155 156 Here's an example of how to define a positional argument for the `Account` method of the `auth` service: 157 158 ```go reference 159 https://github.com/cosmos/cosmos-sdk/blob/v0.50.0-beta.0/x/auth/autocli.go#L25-L30 160 ``` 161 162 Then the command can be used as follows, instead of having to specify the `--address` flag: 163 164 ```bash 165 <appd> query auth account cosmos1abcd...xyz 166 ``` 167 168 ### Customising Flag Names 169 170 By default, `autocli` generates flag names based on the names of the fields in your protobuf message. However, you can customise the flag names by providing a `FlagOptions`. This parameter allows you to specify custom names for flags based on the names of the message fields. 171 172 For example, if you have a message with the fields `test` and `test1`, you can use the following naming options to customise the flags: 173 174 ``` go 175 autocliv1.RpcCommandOptions{ 176 FlagOptions: map[string]*autocliv1.FlagOptions{ 177 "test": { Name: "custom_name", }, 178 "test1": { Name: "other_name", }, 179 }, 180 } 181 ``` 182 183 `FlagsOptions` is defined like sub commands in the `AutoCLIOptions()` method on your module. 184 185 ### Combining AutoCLI with Other Commands Within A Module 186 187 AutoCLI can be used alongside other commands within a module. For example, the `gov` module uses AutoCLI to generate commands for the `query` subcommand, but also defines custom commands for the `proposer` subcommands. 188 189 In order to enable this behavior, set in `AutoCLIOptions()` the `EnhanceCustomCommand` field to `true`, for the command type (queries and/or transactions) you want to enhance. 190 191 ```go reference 192 https://github.com/cosmos/cosmos-sdk/blob/fa4d87ef7e6d87aaccc94c337ffd2fe90fcb7a9d/x/gov/autocli.go#L98 193 ``` 194 195 If not set to true, `AutoCLI` will not generate commands for the module if there are already commands registered for the module (when `GetTxCmd()` or `GetTxCmd()` are defined). 196 197 ### Use AutoCLI for non module commands 198 199 It is possible to use `AutoCLI` for non module commands. The trick is still to implement the `appmodule.Module` interface and append it to the `appOptions.ModuleOptions` map. 200 201 For example, here is how the SDK does it for `cometbft` gRPC commands: 202 203 ```go reference 204 https://github.com/cosmos/cosmos-sdk/blob/julien/autocli-comet/client/grpc/cmtservice/autocli.go#L52-L71 205 ``` 206 207 ## Summary 208 209 `autocli` let you generate CLI to your Cosmos SDK-based applications without any cobra boilerplate. It allows you to easily generate CLI commands and flags from your protobuf messages, and provides many options for customising the behavior of your CLI application. 210 211 To further enhance your CLI experience with Cosmos SDK-based blockchains, you can use `hubl`. `hubl` is a tool that allows you to query any Cosmos SDK-based blockchain using the new AutoCLI feature of the Cosmos SDK. With `hubl`, you can easily configure a new chain and query modules with just a few simple commands. 212 213 For more information on `hubl`, including how to configure a new chain and query a module, see the [Hubl documentation](https://docs.cosmos.network/main/tooling/hubl).