X Tutup
Skip to content

Commit 9f5daec

Browse files
authored
Merge pull request cli#1475 from shihanng/issue-1456__clone-flag-help
Provide better error message for gh repo clone
2 parents a7b450b + 00bdedb commit 9f5daec

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

pkg/cmd/repo/clone/clone.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package clone
22

33
import (
4+
"fmt"
45
"net/http"
56
"strings"
67

@@ -13,6 +14,7 @@ import (
1314
"github.com/cli/cli/pkg/cmdutil"
1415
"github.com/cli/cli/pkg/iostreams"
1516
"github.com/spf13/cobra"
17+
"github.com/spf13/pflag"
1618
)
1719

1820
type CloneOptions struct {
@@ -33,16 +35,18 @@ func NewCmdClone(f *cmdutil.Factory, runF func(*CloneOptions) error) *cobra.Comm
3335
}
3436

3537
cmd := &cobra.Command{
36-
Use: "clone <repository> [<directory>]",
38+
DisableFlagsInUseLine: true,
39+
40+
Use: "clone <repository> [<directory>] [-- <gitflags>...]",
3741
Args: cobra.MinimumNArgs(1),
3842
Short: "Clone a repository locally",
39-
Long: heredoc.Doc(
40-
`Clone a GitHub repository locally.
43+
Long: heredoc.Doc(`
44+
Clone a GitHub repository locally.
4145
4246
If the "OWNER/" portion of the "OWNER/REPO" repository argument is omitted, it
4347
defaults to the name of the authenticating user.
4448
45-
To pass 'git clone' flags, separate them with '--'.
49+
Pass additional 'git clone' flags by listing them after '--'.
4650
`),
4751
RunE: func(cmd *cobra.Command, args []string) error {
4852
opts.Repository = args[0]
@@ -56,6 +60,13 @@ func NewCmdClone(f *cmdutil.Factory, runF func(*CloneOptions) error) *cobra.Comm
5660
},
5761
}
5862

63+
cmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
64+
if err == pflag.ErrHelp {
65+
return err
66+
}
67+
return &cmdutil.FlagError{Err: fmt.Errorf("%w\nSeparate git clone flags with '--'.", err)}
68+
})
69+
5970
return cmd
6071
}
6172

pkg/cmd/repo/clone/clone_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,10 @@ func Test_RepoClone_withoutUsername(t *testing.T) {
179179
assert.Equal(t, 1, cs.Count)
180180
assert.Equal(t, "git clone https://github.com/OWNER/REPO.git", strings.Join(cs.Calls[0].Args, " "))
181181
}
182+
183+
func Test_RepoClone_flagError(t *testing.T) {
184+
_, err := runCloneCommand(nil, "--depth 1 OWNER/REPO")
185+
if err == nil || err.Error() != "unknown flag: --depth\nSeparate git clone flags with '--'." {
186+
t.Errorf("unexpected error %v", err)
187+
}
188+
}

0 commit comments

Comments
 (0)
X Tutup