Skip to content

Commit af1d8a6

Browse files
committed
Print friendly error when "gh repo clone" is missing required argument
1 parent 2afc462 commit af1d8a6

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

pkg/cmd/repo/clone/clone.go

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

33
import (
4+
"errors"
45
"fmt"
56
"net/http"
67
"strings"
@@ -37,8 +38,13 @@ func NewCmdClone(f *cmdutil.Factory, runF func(*CloneOptions) error) *cobra.Comm
3738
cmd := &cobra.Command{
3839
DisableFlagsInUseLine: true,
3940

40-
Use: "clone <repository> [<directory>] [-- <gitflags>...]",
41-
Args: cobra.MinimumNArgs(1),
41+
Use: "clone <repository> [<directory>] [-- <gitflags>...]",
42+
Args: func(cmd *cobra.Command, args []string) error {
43+
if len(args) == 0 {
44+
return &cmdutil.FlagError{Err: errors.New("cannot clone: no repository name given")}
45+
}
46+
return nil
47+
},
4248
Short: "Clone a repository locally",
4349
Long: heredoc.Doc(`
4450
Clone a GitHub repository locally.

pkg/cmd/repo/clone/clone_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,10 @@ func Test_RepoClone_flagError(t *testing.T) {
210210
t.Errorf("unexpected error %v", err)
211211
}
212212
}
213+
214+
func Test_RepoClone_noArgError(t *testing.T) {
215+
_, err := runCloneCommand(nil, "")
216+
if err == nil || err.Error() != "cannot clone: no repository name given" {
217+
t.Errorf("unexpected error %v", err)
218+
}
219+
}

0 commit comments

Comments
 (0)