-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #791 from openset/develop
A: v1.6.1
- Loading branch information
Showing
6 changed files
with
83 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Package base provides base support. | ||
package base | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// Commands - base.Commands | ||
var Commands []*Command | ||
|
||
// Command - base.Command | ||
type Command struct { | ||
Run func(cmd *Command, args []string) | ||
UsageLine string | ||
Short string | ||
Long string | ||
Hidden bool | ||
} | ||
|
||
// Name - base.Command.Name | ||
func (c *Command) Name() string { | ||
name := c.UsageLine | ||
if i := strings.Index(name, " "); i > 0 { | ||
name = name[0:i] | ||
} | ||
return name | ||
} | ||
|
||
// Usage - base.Command.Usage | ||
func (c *Command) Usage() { | ||
fmt.Printf("usage: %s %s\n\n", CmdName, c.UsageLine) | ||
fmt.Printf("Run '%s help %s' for details.\n", CmdName, c.Name()) | ||
} | ||
|
||
// UsageHelp - base.Command.UsageHelp | ||
func (c *Command) UsageHelp() { | ||
fmt.Printf("usage: %s %s\n\n", CmdName, c.UsageLine) | ||
fmt.Println(c.Long) | ||
} | ||
|
||
// Register - base.Register | ||
func Register(cmds ...*Command) { | ||
Commands = append(Commands, cmds...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package base | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
func Run() { | ||
flag.Usage = Usage | ||
flag.Parse() | ||
if flag.NArg() < 1 { | ||
flag.Usage() | ||
return | ||
} | ||
args := flag.Args() | ||
cmdName := flag.Arg(0) | ||
for _, cmd := range Commands { | ||
if cmd.Name() == cmdName { | ||
cmd.Run(cmd, args[1:]) | ||
return | ||
} | ||
} | ||
fmt.Printf("%s %s: unknown command\n\n", CmdName, cmdName) | ||
fmt.Printf("Run '%s help' for usage.\n", CmdName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters