File tree Expand file tree Collapse file tree 4 files changed +78
-62
lines changed Expand file tree Collapse file tree 4 files changed +78
-62
lines changed Original file line number Diff line number Diff line change @@ -12,44 +12,11 @@ import (
1212 "strings"
1313)
1414
15- // CmdName - base.CmdName
16- const (
17- CmdName = "leetcode"
18- URL = "https://github.com/openset/leetcode/tree/master"
19- )
15+ // URL - base.URL
16+ const URL = "https://github.com/openset/leetcode/tree/master"
2017
21- // Commands - base.Commands
22- var Commands []* Command
23-
24- // Command - base.Command
25- type Command struct {
26- Run func (cmd * Command , args []string )
27- UsageLine string
28- Short string
29- Long string
30- Hidden bool
31- }
32-
33- // Name - base.Name
34- func (c * Command ) Name () string {
35- name := c .UsageLine
36- if i := strings .Index (name , " " ); i > 0 {
37- name = name [0 :i ]
38- }
39- return name
40- }
41-
42- // Usage - base.Usage
43- func (c * Command ) Usage () {
44- fmt .Printf ("usage: %s %s\n \n " , CmdName , c .UsageLine )
45- fmt .Printf ("Run '%s help %s' for details.\n " , CmdName , c .Name ())
46- }
47-
48- // UsageHelp - base.UsageHelp
49- func (c * Command ) UsageHelp () {
50- fmt .Printf ("usage: %s %s\n \n " , CmdName , c .UsageLine )
51- fmt .Println (c .Long )
52- }
18+ // CmdName - base.CmdName
19+ var CmdName = filepath .Base (os .Args [0 ])
5320
5421// Usage - base.Usage
5522func Usage () {
Original file line number Diff line number Diff line change 1+ // Package base provides base support.
2+ package base
3+
4+ import (
5+ "fmt"
6+ "strings"
7+ )
8+
9+ // Commands - base.Commands
10+ var Commands []* Command
11+
12+ // Command - base.Command
13+ type Command struct {
14+ Run func (cmd * Command , args []string )
15+ UsageLine string
16+ Short string
17+ Long string
18+ Hidden bool
19+ }
20+
21+ // Name - base.Command.Name
22+ func (c * Command ) Name () string {
23+ name := c .UsageLine
24+ if i := strings .Index (name , " " ); i > 0 {
25+ name = name [0 :i ]
26+ }
27+ return name
28+ }
29+
30+ // Usage - base.Command.Usage
31+ func (c * Command ) Usage () {
32+ fmt .Printf ("usage: %s %s\n \n " , CmdName , c .UsageLine )
33+ fmt .Printf ("Run '%s help %s' for details.\n " , CmdName , c .Name ())
34+ }
35+
36+ // UsageHelp - base.Command.UsageHelp
37+ func (c * Command ) UsageHelp () {
38+ fmt .Printf ("usage: %s %s\n \n " , CmdName , c .UsageLine )
39+ fmt .Println (c .Long )
40+ }
41+
42+ // Register - base.Register
43+ func Register (cmds ... * Command ) {
44+ Commands = append (Commands , cmds ... )
45+ }
Original file line number Diff line number Diff line change 1+ package base
2+
3+ import (
4+ "flag"
5+ "fmt"
6+ )
7+
8+ func Run () {
9+ flag .Usage = Usage
10+ flag .Parse ()
11+ if flag .NArg () < 1 {
12+ flag .Usage ()
13+ return
14+ }
15+ args := flag .Args ()
16+ cmdName := flag .Arg (0 )
17+ for _ , cmd := range Commands {
18+ if cmd .Name () == cmdName {
19+ cmd .Run (cmd , args [1 :])
20+ return
21+ }
22+ }
23+ fmt .Printf ("%s %s: unknown command\n \n " , CmdName , cmdName )
24+ fmt .Printf ("Run '%s help' for usage.\n " , CmdName )
25+ }
Original file line number Diff line number Diff line change 22package main
33
44import (
5- "flag"
6- "fmt"
7-
85 "github.com/openset/leetcode/internal/base"
96 "github.com/openset/leetcode/internal/build"
107 "github.com/openset/leetcode/internal/clean"
@@ -22,8 +19,8 @@ import (
2219 "github.com/openset/leetcode/internal/version"
2320)
2421
25- func init () {
26- base .Commands = [] * base. Command {
22+ func main () {
23+ base .Register (
2724 readme .CmdReadme ,
2825 page .CmdPage ,
2926 tag .CmdTag ,
@@ -38,24 +35,6 @@ func init() {
3835 version .CmdVersion ,
3936 help .CmdHelp ,
4037 post .CmdPost ,
41- }
42- }
43-
44- func main () {
45- flag .Usage = base .Usage
46- flag .Parse ()
47- if flag .NArg () < 1 {
48- flag .Usage ()
49- return
50- }
51- args := flag .Args ()
52- cmdName := flag .Arg (0 )
53- for _ , cmd := range base .Commands {
54- if cmd .Name () == cmdName {
55- cmd .Run (cmd , args [1 :])
56- return
57- }
58- }
59- fmt .Printf ("%s %s: unknown command\n \n " , base .CmdName , cmdName )
60- fmt .Printf ("Run '%s help' for usage.\n " , base .CmdName )
38+ )
39+ base .Run ()
6140}
You can’t perform that action at this time.
0 commit comments