-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathget.go
62 lines (51 loc) · 1.32 KB
/
get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package command
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/gladmo/leetcode/leet"
)
var getCmd = &cobra.Command{
Use: "get question_id|leetcode_url",
Short: "get leetcode question from leetcode-cn.com",
Example: `
leetcode get 795
leetcode get leetcode-cn.com/problems/k-th-symbol-in-grammar
leetcode get https://leetcode-cn.com/problems/k-th-symbol-in-grammar
leetcode get https://leetcode-cn.com/problems/k-th-symbol-in-grammar/solution/
`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
cmd.Println("参数异常")
cmd.Help()
os.Exit(1)
return
}
override, err := cmd.PersistentFlags().GetBool("override")
if err != nil {
fmt.Println(err.Error())
}
withDetail, err := cmd.PersistentFlags().GetBool("with-detail")
if err != nil {
fmt.Println(err.Error())
}
param := leet.Parse(strings.TrimSpace(args[0]))
if param == "" {
cmd.Println(fmt.Sprintf("未找到关于 「%s」 的相关题目", args[0]))
cmd.Help()
os.Exit(1)
return
}
res, err := leet.Fetch(param)
if err != nil {
panic(err)
}
res.Download(override)
leet.InfoPrint(leet.GetQuestionInfo(param), withDetail)
},
}
func init() {
getCmd.PersistentFlags().Bool("override", false, "override")
getCmd.PersistentFlags().Bool("with-detail", false, "with-detail")
}