Skip to content

Commit f78afe5

Browse files
committed
Experiment with adding autocomplete
1 parent f730dfe commit f78afe5

30 files changed

+443
-87
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Ignore Oasis CLI binary.
22
oasis
3+
# Ignore generated completions (created by goreleaser).
4+
completions/
35
# Ignore Python cache directories.
46
__pycache__/
57
# Ignore temporary files.

.goreleaser.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ project_name: Oasis CLI
55
before:
66
hooks:
77
- go mod tidy
8+
- mkdir -p completions
9+
- sh -c "go run . completion bash > completions/oasis.bash"
10+
- sh -c "go run . completion zsh > completions/oasis.zsh"
11+
- sh -c "go run . completion fish > completions/oasis.fish"
812

913
universal_binaries:
1014
- id: oasis-darwin-universal
@@ -82,6 +86,8 @@ archives:
8286
format_overrides:
8387
- goos: windows
8488
formats: [ 'zip' ]
89+
files:
90+
- completions/*
8591

8692
checksum:
8793
name_template: SHA256SUMS-{{.Version}}.txt

cmd/account/allow.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ import (
1414
)
1515

1616
var allowCmd = &cobra.Command{
17-
Use: "allow <beneficiary> <amount>",
18-
Short: "Configure beneficiary allowance",
19-
Args: cobra.ExactArgs(2),
17+
Use: "allow <beneficiary> <amount>",
18+
Short: "Configure beneficiary allowance",
19+
Args: cobra.ExactArgs(2),
20+
ValidArgsFunction: common.AddressesAt(0), // <beneficiary> at position 1.
2021
Run: func(_ *cobra.Command, args []string) {
2122
cfg := cliConfig.Global()
2223
npa := common.GetNPASelection(cfg)

cmd/account/delegate.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ import (
1818
)
1919

2020
var delegateCmd = &cobra.Command{
21-
Use: "delegate <amount> <to>",
22-
Short: "Delegate given amount of tokens to an entity",
23-
Args: cobra.ExactArgs(2),
21+
Use: "delegate <amount> <to>",
22+
Short: "Delegate given amount of tokens to an entity",
23+
Args: cobra.ExactArgs(2),
24+
ValidArgsFunction: common.AddressesAt(1), // <to> at position 2.
2425
Run: func(_ *cobra.Command, args []string) {
2526
cfg := cliConfig.Global()
2627
npa := common.GetNPASelection(cfg)

cmd/account/deposit.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ import (
2020
)
2121

2222
var depositCmd = &cobra.Command{
23-
Use: "deposit <amount> [to]",
24-
Short: "Deposit tokens into ParaTime",
25-
Args: cobra.RangeArgs(1, 2),
23+
Use: "deposit <amount> [to]",
24+
Short: "Deposit tokens into ParaTime",
25+
Args: cobra.RangeArgs(1, 2),
26+
ValidArgsFunction: common.AddressesAt(1), // [to] at position 2.
2627
Run: func(_ *cobra.Command, args []string) {
2728
cfg := cliConfig.Global()
2829
npa := common.GetNPASelection(cfg)

cmd/account/show/show.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ var (
2626
showDelegations bool
2727

2828
Cmd = &cobra.Command{
29-
Use: "show [address]",
30-
Short: "Show balance and other information",
31-
Aliases: []string{"s", "balance", "b"},
32-
Args: cobra.MaximumNArgs(1),
29+
Use: "show [address]",
30+
Short: "Show balance and other information",
31+
Aliases: []string{"s", "balance", "b"},
32+
Args: cobra.MaximumNArgs(1),
33+
ValidArgsFunction: common.CompleteAccountAndAddressBookNames,
3334
Run: func(_ *cobra.Command, args []string) {
3435
cfg := cliConfig.Global()
3536
npa := common.GetNPASelection(cfg)

cmd/account/transfer.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ import (
1818
)
1919

2020
var transferCmd = &cobra.Command{
21-
Use: "transfer <amount> [<denom>] <to>",
22-
Short: "Transfer given amount of tokens",
23-
Aliases: []string{"t"},
24-
Args: cobra.RangeArgs(2, 3),
21+
Use: "transfer <amount> [<denom>] <to>",
22+
Short: "Transfer given amount of tokens",
23+
Aliases: []string{"t"},
24+
Args: cobra.RangeArgs(2, 3),
25+
ValidArgsFunction: common.AddressesAt(1, 2), // <to> can be at position 2 or 3.
2526
Run: func(_ *cobra.Command, args []string) {
2627
cfg := cliConfig.Global()
2728
npa := common.GetNPASelection(cfg)

cmd/account/undelegate.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ import (
1818
)
1919

2020
var undelegateCmd = &cobra.Command{
21-
Use: "undelegate <shares> <from>",
22-
Short: "Undelegate given amount of shares from an entity",
23-
Args: cobra.ExactArgs(2),
21+
Use: "undelegate <shares> <from>",
22+
Short: "Undelegate given amount of shares from an entity",
23+
Args: cobra.ExactArgs(2),
24+
ValidArgsFunction: common.AddressesAt(1), // <from> at position 2.
2425
Run: func(_ *cobra.Command, args []string) {
2526
cfg := cliConfig.Global()
2627
npa := common.GetNPASelection(cfg)

cmd/account/withdraw.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ import (
1919
)
2020

2121
var withdrawCmd = &cobra.Command{
22-
Use: "withdraw <amount> [to]",
23-
Short: "Withdraw tokens from ParaTime",
24-
Args: cobra.RangeArgs(1, 2),
22+
Use: "withdraw <amount> [to]",
23+
Short: "Withdraw tokens from ParaTime",
24+
Args: cobra.RangeArgs(1, 2),
25+
ValidArgsFunction: common.AddressesAt(1), // [to] at position 2.
2526
Run: func(_ *cobra.Command, args []string) {
2627
cfg := cliConfig.Global()
2728
npa := common.GetNPASelection(cfg)

cmd/addressbook.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/spf13/cobra"
88

9+
"github.com/oasisprotocol/cli/cmd/common"
910
"github.com/oasisprotocol/cli/config"
1011
"github.com/oasisprotocol/cli/table"
1112
)
@@ -70,9 +71,10 @@ var (
7071
}
7172

7273
abShowCmd = &cobra.Command{
73-
Use: "show <name>",
74-
Short: "Show address information",
75-
Args: cobra.ExactArgs(1),
74+
Use: "show <name>",
75+
Short: "Show address information",
76+
Args: cobra.ExactArgs(1),
77+
ValidArgsFunction: common.CompleteAddressBookNames,
7678
Run: func(_ *cobra.Command, args []string) {
7779
name := args[0]
7880
abEntry, ok := config.Global().AddressBook.All[name]
@@ -89,10 +91,11 @@ var (
8991
}
9092

9193
abRmCmd = &cobra.Command{
92-
Use: "remove <name>",
93-
Aliases: []string{"rm"},
94-
Short: "Remove an address from address book",
95-
Args: cobra.ExactArgs(1),
94+
Use: "remove <name>",
95+
Aliases: []string{"rm"},
96+
Short: "Remove an address from address book",
97+
Args: cobra.ExactArgs(1),
98+
ValidArgsFunction: common.CompleteAddressBookNames,
9699
Run: func(_ *cobra.Command, args []string) {
97100
cfg := config.Global()
98101
name := args[0]
@@ -106,10 +109,11 @@ var (
106109
}
107110

108111
abRenameCmd = &cobra.Command{
109-
Use: "rename <old> <new>",
110-
Aliases: []string{"mv"},
111-
Short: "Rename address",
112-
Args: cobra.ExactArgs(2),
112+
Use: "rename <old> <new>",
113+
Aliases: []string{"mv"},
114+
Short: "Rename address",
115+
Args: cobra.ExactArgs(2),
116+
ValidArgsFunction: common.CompleteAddressBookNames,
113117
Run: func(_ *cobra.Command, args []string) {
114118
cfg := config.Global()
115119
oldName, newName := args[0], args[1]

0 commit comments

Comments
 (0)