diff --git a/commands/api/api.go b/commands/api/api.go index e41a9437f33d33847a4869e605ca24dcefc28e85..0a22f98aaa06e17f0b4e011edc4e0509cfb83a29 100644 --- a/commands/api/api.go +++ b/commands/api/api.go @@ -154,8 +154,16 @@ func NewCmdApi(f *cmdutils.Factory, runF func(*ApiOptions) error) *cobra.Command GITLAB_HOST, GITLAB_URI, GITLAB_URL: specify a GitLab host to make request to. `), }, - Args: cobra.ExactArgs(1), + Args: cobra.MaximumNArgs(1), RunE: func(c *cobra.Command, args []string) error { + if len(args) == 0 { + err := c.Help() + if err != nil { + return err + } + return fmt.Errorf("error: must provide at least 1 argument") + } + opts.RequestPath = args[0] opts.RequestMethodPassed = c.Flags().Changed("method") opts.Config, _ = f.Config() diff --git a/commands/api/api_test.go b/commands/api/api_test.go index 85bad997b788e371e9bc7c71bbfaccdac252a90d..e8009acb683a8daa1de1eb85224a061650441fee 100644 --- a/commands/api/api_test.go +++ b/commands/api/api_test.go @@ -189,9 +189,8 @@ func Test_NewCmdApi(t *testing.T) { wantsErr: false, }, { - name: "no arguments", - cli: "", - wantsErr: true, + name: "no arguments", + cli: "", }, { name: "with hostname", diff --git a/commands/cmdutils/cmdutils.go b/commands/cmdutils/cmdutils.go index 8e9fb87daacbd844804c8ae807634f4a29a57e04..3fa355534ba5147c768f05f154b9aa697b471f45 100644 --- a/commands/cmdutils/cmdutils.go +++ b/commands/cmdutils/cmdutils.go @@ -12,6 +12,7 @@ import ( "gitlab.com/gitlab-org/cli/pkg/iostreams" + "github.com/spf13/cobra" "github.com/xanzy/go-gitlab" "gitlab.com/gitlab-org/cli/api" "gitlab.com/gitlab-org/cli/internal/glrepo" @@ -62,6 +63,15 @@ func LoadGitLabTemplate(tmplType, tmplName string) (string, error) { return strings.TrimSpace(string(tmpl)), nil } +func ReturnHelpWhenNoArgs(cmd *cobra.Command, args []string) { + if len(args) == 0 { + err := cmd.Help() + if err != nil { + return + } + } +} + // TODO: properly handle errors in this function. // // For now, it returns nil and empty slice if there's an error