Skip to content

Commit 8e8c60d

Browse files
authored
Merge pull request #1391 from lobshunter/args-error
feat: more friendly cli error message
2 parents 36726f2 + 48ae695 commit 8e8c60d

File tree

17 files changed

+36
-20
lines changed

17 files changed

+36
-20
lines changed

cmd/limactl/copy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func newCopyCommand() *cobra.Command {
2828
Aliases: []string{"cp"},
2929
Short: "Copy files between host and guest",
3030
Long: copyHelp,
31-
Args: cobra.MinimumNArgs(2),
31+
Args: WrapArgsError(cobra.MinimumNArgs(2)),
3232
RunE: copyAction,
3333
}
3434

cmd/limactl/debug.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func newDebugDNSCommand() *cobra.Command {
2424
Use: "dns UDPPORT [TCPPORT]",
2525
Short: "Debug built-in DNS",
2626
Long: "DO NOT USE! THE COMMAND SYNTAX IS SUBJECT TO CHANGE!",
27-
Args: cobra.RangeArgs(1, 2),
27+
Args: WrapArgsError(cobra.RangeArgs(1, 2)),
2828
RunE: debugDNSAction,
2929
}
3030
cmd.Flags().BoolP("ipv6", "6", false, "lookup IPv6 addresses too")

cmd/limactl/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func newDeleteCommand() *cobra.Command {
1616
Use: "delete INSTANCE [INSTANCE, ...]",
1717
Aliases: []string{"remove", "rm"},
1818
Short: "Delete an instance of Lima.",
19-
Args: cobra.MinimumNArgs(1),
19+
Args: WrapArgsError(cobra.MinimumNArgs(1)),
2020
RunE: deleteAction,
2121
ValidArgsFunction: deleteBashComplete,
2222
}

cmd/limactl/disk.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ To create a new disk:
4747
$ limactl disk create DISK --size SIZE
4848
`,
4949
Short: "Create a Lima disk",
50-
Args: cobra.ExactArgs(1),
50+
Args: WrapArgsError(cobra.ExactArgs(1)),
5151
RunE: diskCreateAction,
5252
}
5353
diskCreateCommand.Flags().String("size", "", "configure the disk size")
@@ -100,7 +100,7 @@ $ limactl disk list
100100
`,
101101
Short: "List existing Lima disks",
102102
Aliases: []string{"ls"},
103-
Args: cobra.ArbitraryArgs,
103+
Args: WrapArgsError(cobra.ArbitraryArgs),
104104
RunE: diskListAction,
105105
}
106106
diskListCommand.Flags().Bool("json", false, "JSONify output")
@@ -189,7 +189,7 @@ $ limactl disk delete DISK1 DISK2 ...
189189
`,
190190
Aliases: []string{"remove", "rm"},
191191
Short: "Delete one or more Lima disks",
192-
Args: cobra.MinimumNArgs(1),
192+
Args: WrapArgsError(cobra.MinimumNArgs(1)),
193193
RunE: diskDeleteAction,
194194
}
195195
diskDeleteCommand.Flags().BoolP("force", "f", false, "force delete")
@@ -279,7 +279,7 @@ To unlock multiple disks:
279279
$ limactl disk unlock DISK1 DISK2 ...
280280
`,
281281
Short: "Unlock one or more Lima disks",
282-
Args: cobra.MinimumNArgs(1),
282+
Args: WrapArgsError(cobra.MinimumNArgs(1)),
283283
RunE: diskUnlockAction,
284284
}
285285
return diskUnlockCommand

cmd/limactl/edit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func newEditCommand() *cobra.Command {
2222
var editCommand = &cobra.Command{
2323
Use: "edit INSTANCE",
2424
Short: "Edit an instance of Lima",
25-
Args: cobra.MaximumNArgs(1),
25+
Args: WrapArgsError(cobra.MaximumNArgs(1)),
2626
RunE: editAction,
2727
ValidArgsFunction: editBashComplete,
2828
}

cmd/limactl/factory-reset.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func newFactoryResetCommand() *cobra.Command {
1515
var resetCommand = &cobra.Command{
1616
Use: "factory-reset INSTANCE",
1717
Short: "Factory reset an instance of Lima",
18-
Args: cobra.MaximumNArgs(1),
18+
Args: WrapArgsError(cobra.MaximumNArgs(1)),
1919
RunE: factoryResetAction,
2020
ValidArgsFunction: factoryResetBashComplete,
2121
}

cmd/limactl/hostagent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func newHostagentCommand() *cobra.Command {
2121
var hostagentCommand = &cobra.Command{
2222
Use: "hostagent INSTANCE",
2323
Short: "run hostagent",
24-
Args: cobra.ExactArgs(1),
24+
Args: WrapArgsError(cobra.ExactArgs(1)),
2525
RunE: hostagentAction,
2626
Hidden: true,
2727
}

cmd/limactl/info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func newInfoCommand() *cobra.Command {
1212
infoCommand := &cobra.Command{
1313
Use: "info",
1414
Short: "Show diagnostic information",
15-
Args: cobra.NoArgs,
15+
Args: WrapArgsError(cobra.NoArgs),
1616
RunE: infoAction,
1717
}
1818
return infoCommand

cmd/limactl/list.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func newListCommand() *cobra.Command {
4040
Short: "List instances of Lima.",
4141
Long: "List instances of Lima.\n" + dedent.Dedent(`
4242
The output can be presented in one of several formats, using the --format <format> flag.
43-
43+
4444
--format json - output in json format
4545
--format yaml - output in yaml format
4646
--format table - output in table format
@@ -49,7 +49,7 @@ func newListCommand() *cobra.Command {
4949
The following legacy flags continue to function:
5050
--json - equal to '--format json'
5151
`),
52-
Args: cobra.ArbitraryArgs,
52+
Args: WrapArgsError(cobra.ArbitraryArgs),
5353
RunE: listAction,
5454
ValidArgsFunction: listBashComplete,
5555
}

cmd/limactl/main.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,19 @@ func handleExitCoder(err error) {
119119
return
120120
}
121121
}
122+
123+
// WrapArgsError annotates cobra args error with some context, so the error message is more user-friendly
124+
func WrapArgsError(argFn cobra.PositionalArgs) cobra.PositionalArgs {
125+
return func(cmd *cobra.Command, args []string) error {
126+
err := argFn(cmd, args)
127+
if err == nil {
128+
return nil
129+
}
130+
131+
return fmt.Errorf("%q %s.\nSee '%s --help'.\n\nUsage: %s\n\n%s",
132+
cmd.CommandPath(), err.Error(),
133+
cmd.CommandPath(),
134+
cmd.UseLine(), cmd.Short,
135+
)
136+
}
137+
}

0 commit comments

Comments
 (0)