diff --git a/pkg/cmd/repo/clone/clone.go b/pkg/cmd/repo/clone/clone.go index ef82d5081..9faa0da59 100644 --- a/pkg/cmd/repo/clone/clone.go +++ b/pkg/cmd/repo/clone/clone.go @@ -1,6 +1,7 @@ package clone import ( + "fmt" "net/http" "strings" @@ -13,6 +14,7 @@ import ( "github.com/cli/cli/pkg/cmdutil" "github.com/cli/cli/pkg/iostreams" "github.com/spf13/cobra" + "github.com/spf13/pflag" ) type CloneOptions struct { @@ -33,16 +35,18 @@ func NewCmdClone(f *cmdutil.Factory, runF func(*CloneOptions) error) *cobra.Comm } cmd := &cobra.Command{ - Use: "clone []", + DisableFlagsInUseLine: true, + + Use: "clone [] [-- ...]", Args: cobra.MinimumNArgs(1), Short: "Clone a repository locally", - Long: heredoc.Doc( - `Clone a GitHub repository locally. + Long: heredoc.Doc(` + Clone a GitHub repository locally. If the "OWNER/" portion of the "OWNER/REPO" repository argument is omitted, it defaults to the name of the authenticating user. - To pass 'git clone' flags, separate them with '--'. + Pass additional 'git clone' flags by listing them after '--'. `), RunE: func(cmd *cobra.Command, args []string) error { opts.Repository = args[0] @@ -56,6 +60,13 @@ func NewCmdClone(f *cmdutil.Factory, runF func(*CloneOptions) error) *cobra.Comm }, } + cmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error { + if err == pflag.ErrHelp { + return err + } + return &cmdutil.FlagError{Err: fmt.Errorf("%w\nSeparate git clone flags with '--'.", err)} + }) + return cmd } diff --git a/pkg/cmd/repo/clone/clone_test.go b/pkg/cmd/repo/clone/clone_test.go index c732c8bfe..54aa2f1cb 100644 --- a/pkg/cmd/repo/clone/clone_test.go +++ b/pkg/cmd/repo/clone/clone_test.go @@ -179,3 +179,10 @@ func Test_RepoClone_withoutUsername(t *testing.T) { assert.Equal(t, 1, cs.Count) assert.Equal(t, "git clone https://github.com/OWNER/REPO.git", strings.Join(cs.Calls[0].Args, " ")) } + +func Test_RepoClone_flagError(t *testing.T) { + _, err := runCloneCommand(nil, "--depth 1 OWNER/REPO") + if err == nil || err.Error() != "unknown flag: --depth\nSeparate git clone flags with '--'." { + t.Errorf("unexpected error %v", err) + } +}