Skip to content

Commit ddb6279

Browse files
authored
fix(zli config): print help for missing args (#4046)
Make `zli config` and config subcommands show usage/help when invoked with missing required args, instead of Cobra’s generic “accepts N arg(s)” errors. Example: - before: `zli config add` -> `Error: accepts 2 arg(s), received 0` - after: `zli config add` prints `zli config add <config-name> <url>` usage/help1~fix(zli config): print help for missing args Signed-off-by: Andrei Aaron <andreifdaaron@gmail.com>
1 parent c7ddbe2 commit ddb6279

3 files changed

Lines changed: 56 additions & 32 deletions

File tree

pkg/cli/client/config_cmd.go

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ func NewConfigCommand() *cobra.Command {
2626
Short: "Configure zot registry parameters for CLI",
2727
Args: cobra.ArbitraryArgs,
2828
RunE: func(cmd *cobra.Command, args []string) error {
29+
if len(args) == 0 && !isListing && !isReset {
30+
_ = cmd.Help()
31+
32+
return nil
33+
}
34+
2935
configPath, err := zliUserConfigPath()
3036
if err != nil {
3137
return err
@@ -62,6 +68,18 @@ Older positional syntax on this command is deprecated and will soon be removed.`
6268
return configCmd
6369
}
6470

71+
func exactArgsOrHelp(expected int) cobra.PositionalArgs {
72+
return func(cmd *cobra.Command, args []string) error {
73+
if len(args) != expected {
74+
_ = cmd.Help()
75+
76+
return zerr.ErrInvalidArgs
77+
}
78+
79+
return nil
80+
}
81+
}
82+
6583
func zliUserConfigPath() (string, error) {
6684
home, err := os.UserHomeDir()
6785
if err != nil {
@@ -106,11 +124,12 @@ func reservedProfileNames(configCmd *cobra.Command) []string {
106124

107125
func NewConfigAddCommand() *cobra.Command {
108126
configAddCmd := &cobra.Command{
109-
Use: "add <config-name> <url>",
110-
Example: " zli config add main https://zot-foo.com:8080",
111-
Short: "Add configuration for a zot registry",
112-
Long: "Add configuration for a zot registry",
113-
Args: cobra.ExactArgs(twoArgs),
127+
Use: "add <config-name> <url>",
128+
Example: " zli config add main https://zot-foo.com:8080",
129+
Short: "Add configuration for a zot registry",
130+
Long: "Add configuration for a zot registry",
131+
SilenceUsage: true,
132+
Args: exactArgsOrHelp(twoArgs),
114133
RunE: func(cmd *cobra.Command, args []string) error {
115134
configPath, err := zliUserConfigPath()
116135
if err != nil {
@@ -143,11 +162,12 @@ func NewConfigAddCommand() *cobra.Command {
143162

144163
func NewConfigRemoveCommand() *cobra.Command {
145164
configRemoveCmd := &cobra.Command{
146-
Use: "remove <config-name>",
147-
Example: " zli config remove main",
148-
Short: "Remove configuration for a zot registry",
149-
Long: "Remove configuration for a zot registry",
150-
Args: cobra.ExactArgs(oneArg),
165+
Use: "remove <config-name>",
166+
Example: " zli config remove main",
167+
Short: "Remove configuration for a zot registry",
168+
Long: "Remove configuration for a zot registry",
169+
SilenceUsage: true,
170+
Args: exactArgsOrHelp(oneArg),
151171
RunE: func(cmd *cobra.Command, args []string) error {
152172
configPath, err := zliUserConfigPath()
153173
if err != nil {
@@ -200,11 +220,12 @@ func NewConfigListCommand() *cobra.Command {
200220

201221
func NewConfigShowCommand() *cobra.Command {
202222
showCmd := &cobra.Command{
203-
Use: "show <name>",
204-
Example: " zli config show main",
205-
Short: "Show all variables for one profile",
206-
Long: "Print every variable set for the named CLI profile.",
207-
Args: cobra.ExactArgs(oneArg),
223+
Use: "show <name>",
224+
Example: " zli config show main",
225+
Short: "Show all variables for one profile",
226+
Long: "Print every variable set for the named CLI profile.",
227+
SilenceUsage: true,
228+
Args: exactArgsOrHelp(oneArg),
208229
RunE: func(cmd *cobra.Command, args []string) error {
209230
configPath, err := zliUserConfigPath()
210231
if err != nil {
@@ -229,11 +250,12 @@ func NewConfigShowCommand() *cobra.Command {
229250

230251
func NewConfigGetCommand() *cobra.Command {
231252
getCmd := &cobra.Command{
232-
Use: "get <name> <key>",
233-
Example: " zli config get main url",
234-
Short: "Print one configuration variable",
235-
Long: "Print the value of a single key for the named profile.",
236-
Args: cobra.ExactArgs(twoArgs),
253+
Use: "get <name> <key>",
254+
Example: " zli config get main url",
255+
Short: "Print one configuration variable",
256+
Long: "Print the value of a single key for the named profile.",
257+
SilenceUsage: true,
258+
Args: exactArgsOrHelp(twoArgs),
237259
RunE: func(cmd *cobra.Command, args []string) error {
238260
configPath, err := zliUserConfigPath()
239261
if err != nil {
@@ -258,11 +280,12 @@ func NewConfigGetCommand() *cobra.Command {
258280

259281
func NewConfigSetCommand() *cobra.Command {
260282
setCmd := &cobra.Command{
261-
Use: "set <name> <key> <value>",
262-
Example: " zli config set main showspinner false",
263-
Short: "Set a configuration variable",
264-
Long: "Set a single key for the named profile and persist ~/.zot.",
265-
Args: cobra.ExactArgs(threeArgs),
283+
Use: "set <name> <key> <value>",
284+
Example: " zli config set main showspinner false",
285+
Short: "Set a configuration variable",
286+
Long: "Set a single key for the named profile and persist ~/.zot.",
287+
SilenceUsage: true,
288+
Args: exactArgsOrHelp(threeArgs),
266289
RunE: func(cmd *cobra.Command, args []string) error {
267290
configPath, err := zliUserConfigPath()
268291
if err != nil {
@@ -280,11 +303,12 @@ func NewConfigSetCommand() *cobra.Command {
280303

281304
func NewConfigResetCommand() *cobra.Command {
282305
resetCmd := &cobra.Command{
283-
Use: "reset <name> <key>",
284-
Example: " zli config reset main showspinner",
285-
Short: "Reset a configuration variable to its default",
286-
Long: "Remove a non-default key from the named profile (URL and profile name cannot be reset).",
287-
Args: cobra.ExactArgs(twoArgs),
306+
Use: "reset <name> <key>",
307+
Example: " zli config reset main showspinner",
308+
Short: "Reset a configuration variable to its default",
309+
Long: "Remove a non-default key from the named profile (URL and profile name cannot be reset).",
310+
SilenceUsage: true,
311+
Args: exactArgsOrHelp(twoArgs),
288312
RunE: func(cmd *cobra.Command, args []string) error {
289313
configPath, err := zliUserConfigPath()
290314
if err != nil {

pkg/cli/client/config_cmd_deprecated_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func TestConfigCmdDeprecatedBasics(t *testing.T) {
6363
err := cmd.Execute()
6464

6565
So(buff.String(), ShouldContainSubstring, "Usage")
66-
So(err, ShouldNotBeNil)
66+
So(err, ShouldBeNil)
6767
})
6868
}
6969

pkg/cli/client/config_cmd_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TestConfigCmdBasics(t *testing.T) {
6262
err := cmd.Execute()
6363

6464
So(buff.String(), ShouldContainSubstring, "Usage")
65-
So(err, ShouldNotBeNil)
65+
So(err, ShouldBeNil)
6666
})
6767
}
6868

0 commit comments

Comments
 (0)