Skip to content

Commit

Permalink
feat: improve command readability (#12)
Browse files Browse the repository at this point in the history
* add helper function to change output command syntax

* rename contractAddress for address in wasm usage

* add helper function to keys networks and wasm

* update readme output

* add getVersion util in order to use package json version

* bump 0.1.6 version
  • Loading branch information
j0nl1 authored May 13, 2022
1 parent 3620baf commit c6c9281
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 26 deletions.
24 changes: 10 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ Commands:
keys [command] manage your keys
networks [command] manage networks
wasm [command] wasm transaction subcommands
help [command] display help for command
```

> **Wasm Command**
Expand All @@ -39,14 +38,13 @@ Usage: cwcli wasm [command]
Wasm transaction subcommands

Options:
-h, --help display help for command
-h, --help display help for command

Commands:
upload [options] <wasm file> Upload a wasm binary
query [options] <contractAddress> <msg> Querying commands for contracts
execute [options] <contractAddress> <msg> execute a smart contract method
instantiate [options] <codeId> <msg> instantiate an uploaded contract
help [command] display help for command
upload <wasm file> Upload a wasm binary
query <address> <msg> Querying commands for contracts
execute <address> <msg> execute a smart contract method
instantiate <codeId> <msg> instantiate an uploaded contract
```

> **Keys Command**
Expand All @@ -57,14 +55,13 @@ Usage: cwcli keys [command]
Manage your keys

Options:
-h, --help display help for command
-h, --help display help for command

Commands:
add <name> Add an encrypted private key (either newly generated or recovered), encrypt it, and save to keyring
delete <name> Delete keys from the cli
show [options] <name> Return address depending on network selection
list Return a list of all public keys stored by this cli
help [command] display help for command display help for command
add <name> Add an encrypted private key (either newly generated or recovered), encrypt it, and save to keyring
delete <name> Delete keys from the cli
show <name> Return address depending on network selection
list Return a list of all public keys stored by this cli
```

> **Network Command**
Expand All @@ -81,7 +78,6 @@ Commands:
add <filePath> Load a custom network
delete <name> Remove a custom network
list show a list of networks
help [command] display help for command
```


2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cosmwasm-cli",
"version": "0.1.5",
"version": "0.1.6",
"author": "Javier Aceña <j0nl1>",
"description": "Cosmwasm Command Line Interface",
"scripts": {
Expand Down
4 changes: 3 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { Command } from "commander";
import KeysCommand from "./commands/keys";
import NetworkCommand from "./commands/networks";
import WasmCommand from "./commands/wasm";
import getVersion from "./utils/getVersion";

export default new Command("cwcli")
.version("0.1.5")
.version(getVersion())
.description("Cosmwasm Command Line Interface")
.usage("[command]")
.addCommand(KeysCommand)
.addCommand(NetworkCommand)
.addCommand(WasmCommand)
.addHelpCommand(false)
.parse(process.argv);
7 changes: 6 additions & 1 deletion src/commands/keys/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ import AddKeyCommand from "./addKey";
import ShowKeyCommand from "./showKey";
import DeleteKeyCommand from "./deleteKey";
import ListKeysCommand from "./listKeys";
import subcommandTerm from "../../utils/subcommandTerm";

export default new Command("keys")
const KeysCommand = new Command("keys")
.description("Manage your keys")
.usage("[command]")
.addCommand(AddKeyCommand)
.addCommand(DeleteKeyCommand)
.addCommand(ShowKeyCommand)
.addCommand(ListKeysCommand);

KeysCommand.addHelpCommand(false).configureHelp({ subcommandTerm });

export default KeysCommand;
7 changes: 6 additions & 1 deletion src/commands/networks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import { Command } from "commander";
import AddCustomNetwork from "./addCustomNetwork";
import ListNetworkCommand from "./listNetworks";
import deleteCustomNetwork from "./deleteCustomNetwork";
import subcommandTerm from "../../utils/subcommandTerm";

export default new Command("networks")
const NetworksCommand = new Command("networks")
.description("Manage networks")
.usage("[command]")
.addCommand(AddCustomNetwork)
.addCommand(deleteCustomNetwork)
.addCommand(ListNetworkCommand);

NetworksCommand.addHelpCommand(false).configureHelp({ subcommandTerm });

export default NetworksCommand;
4 changes: 2 additions & 2 deletions src/commands/wasm/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import NetworkOption from "../options/network";

export default new Command("execute")
.description("execute a smart contract method")
.usage("<contractAddress> <msg> --address <name> --network <name> [options]")
.argument("<contractAddress>")
.usage("<address> <msg> --address <name> --network <name> [options]")
.argument("<address>")
.argument("<msg>")
.addOption(AddressOption)
.addOption(NetworkOption)
Expand Down
12 changes: 9 additions & 3 deletions src/commands/wasm/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { Command } from "commander";
import { Command, Help } from "commander";
import UploadCommand from "./upload";
import InstantiateCommand from "./instantiate";
import QueryCommand from "./query";
import ExecuteCommand from "./execute";
import subcommandTerm from "../../utils/subcommandTerm";

export default new Command("wasm")
const t = new Help();

const WasmCommand = new Command("wasm")
.description("Wasm transaction subcommands")
.usage("[command]")
.argument("[command]")
.addCommand(UploadCommand)
.addCommand(QueryCommand)
.addCommand(ExecuteCommand)
.addCommand(InstantiateCommand);

WasmCommand.addHelpCommand(false).configureHelp({ subcommandTerm });

export default WasmCommand;
6 changes: 3 additions & 3 deletions src/commands/wasm/query.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Command } from "commander";
import { Command, Help } from "commander";
import { queryContract } from "../../services/wasm.service";
import NetworkOption from "../options/network";

export default new Command("query")
.description("Querying commands for contracts")
.usage("<contractAddress> <msg> [options]")
.argument("<contractAddress>")
.usage("<address> <msg> [options]")
.argument("<address>")
.argument("<msg>")
.addOption(NetworkOption)
.action(queryContract);
4 changes: 4 additions & 0 deletions src/utils/getVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import fs from "fs";
import path from "path";

export default () => JSON.parse(fs.readFileSync(path.join(__dirname, "..", "..", "package.json"), "utf8")).version;
12 changes: 12 additions & 0 deletions src/utils/subcommandTerm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Argument, Command } from "commander";

export default (cmd: Command & { _name: string; _aliases: string[]; _args: Argument[] }) => {
const args = cmd._args
.map((arg: Argument) => {
const nameOutput = arg.name() + (arg.variadic === true ? "..." : "");

return arg.required ? "<" + nameOutput + ">" : "[" + nameOutput + "]";
})
.join(" ");
return cmd._name + (cmd._aliases[0] ? "|" + cmd._aliases[0] : "") + (args ? " " + args : "");
};

0 comments on commit c6c9281

Please sign in to comment.