Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ec2eb24

Browse files
author
Sandy
authored
Merge pull request #10 from openset/develop
Add: leetcode
2 parents 6992eef + f7649db commit ec2eb24

File tree

5 files changed

+84
-11
lines changed

5 files changed

+84
-11
lines changed

‎internal/base/base.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package base
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
)
8+
9+
const CmdName = "leetcode"
10+
11+
var Commands []*Command
12+
13+
type Command struct {
14+
Run func(cmd *Command, args []string)
15+
UsageLine string
16+
Short string
17+
Long string
18+
}
19+
20+
func (c *Command) Name() string {
21+
name := c.UsageLine
22+
if i := strings.Index(name, " "); i > 0 {
23+
name = name[0 : i+1]
24+
}
25+
return name
26+
}
27+
28+
func (c *Command) Usage() {
29+
fmt.Printf("usage: %s %s\n", CmdName, c.UsageLine)
30+
fmt.Printf("Run '%s help %s' for details.\n", CmdName, c.Name())
31+
os.Exit(0)
32+
}
33+
34+
func Usage() {
35+
fmt.Printf("%s is a tool for managing leetcode source code.\n\n", CmdName)
36+
fmt.Println("Usage:")
37+
fmt.Printf("\t%s <command> [arguments]\n", CmdName)
38+
fmt.Println("The commands are:")
39+
for _, cmd := range Commands {
40+
fmt.Printf("\t%s \t%s\n", cmd.Name(), cmd.Short)
41+
}
42+
fmt.Println()
43+
fmt.Printf(`Use "%s help <command>" for more information about a command.`, CmdName)
44+
fmt.Println()
45+
}

‎internal/help/help.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package help

‎internal/login/login.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package login

‎internal/version/version.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package version
2+
3+
import (
4+
"fmt"
5+
"runtime"
6+
7+
"github.com/openset/leetcode/internal/base"
8+
)
9+
10+
const version = "0.0.1"
11+
12+
var CmdVersion = &base.Command{
13+
Run: runVersion,
14+
UsageLine: "version",
15+
Short: fmt.Sprintf("print %s version", base.CmdName),
16+
Long: fmt.Sprintf("Version prints the %s version.", base.CmdName),
17+
}
18+
19+
func runVersion(cmd *base.Command, args []string) {
20+
if len(args) != 0 {
21+
cmd.Usage()
22+
}
23+
24+
fmt.Printf("%s version %s %s/%s\n", base.CmdName, version, runtime.GOOS, runtime.GOARCH)
25+
}

‎main.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@ import (
44
"flag"
55
"fmt"
66
"log"
7-
"os"
7+
8+
"github.com/openset/leetcode/internal/base"
9+
"github.com/openset/leetcode/internal/version"
810
)
911

12+
func init() {
13+
base.Commands = []*base.Command{
14+
version.CmdVersion,
15+
}
16+
}
17+
1018
func main() {
1119
log.SetFlags(0)
12-
log.SetPrefix("leetcode: ")
20+
log.SetPrefix(fmt.Sprintf("%s: ", base.CmdName))
1321

14-
flag.Usage = usage
22+
flag.Usage = base.Usage
1523
flag.Parse()
16-
17-
args := flag.Args()
18-
if len(args) < 1 {
24+
if flag.NArg() < 1 {
1925
flag.Usage()
2026
}
2127
}
22-
23-
func usage() {
24-
fmt.Fprintln(os.Stderr, "usage: ")
25-
flag.PrintDefaults()
26-
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /