mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-03 22:45:13 +08:00
优化代码规范
This commit is contained in:
@@ -8,47 +8,47 @@ import (
|
||||
|
||||
type valueType int
|
||||
type CommandFunctionCB func(args interface{}) error
|
||||
|
||||
var commandList []*command
|
||||
var programName string
|
||||
const(
|
||||
boolType valueType = 0
|
||||
|
||||
const (
|
||||
boolType valueType = 0
|
||||
stringType valueType = 1
|
||||
intType valueType = 2
|
||||
intType valueType = 2
|
||||
)
|
||||
|
||||
type command struct{
|
||||
valType valueType
|
||||
name string
|
||||
bValue bool
|
||||
type command struct {
|
||||
valType valueType
|
||||
name string
|
||||
bValue bool
|
||||
strValue string
|
||||
intValue int
|
||||
usage string
|
||||
fn CommandFunctionCB
|
||||
usage string
|
||||
fn CommandFunctionCB
|
||||
}
|
||||
|
||||
func (cmd *command) execute() error{
|
||||
func (cmd *command) execute() error {
|
||||
if cmd.valType == boolType {
|
||||
return cmd.fn(cmd.bValue)
|
||||
}else if cmd.valType == stringType {
|
||||
} else if cmd.valType == stringType {
|
||||
return cmd.fn(cmd.strValue)
|
||||
}else if cmd.valType == intType {
|
||||
} else if cmd.valType == intType {
|
||||
return cmd.fn(cmd.intValue)
|
||||
}else{
|
||||
return fmt.Errorf("Unknow command type.")
|
||||
}
|
||||
|
||||
return nil
|
||||
return fmt.Errorf("unknow command type")
|
||||
}
|
||||
|
||||
func Run(args []string) error {
|
||||
flag.Parse()
|
||||
programName = args[0]
|
||||
if flag.NFlag() <= 0 {
|
||||
return fmt.Errorf("Command input parameter error,try `%s -help` for help",args[0])
|
||||
return fmt.Errorf("Command input parameter error,try `%s -help` for help", args[0])
|
||||
}
|
||||
|
||||
var startCmd *command
|
||||
for _,val := range commandList {
|
||||
for _, val := range commandList {
|
||||
if val.name == "start" {
|
||||
startCmd = val
|
||||
continue
|
||||
@@ -63,50 +63,50 @@ func Run(args []string) error {
|
||||
return startCmd.execute()
|
||||
}
|
||||
|
||||
return fmt.Errorf("Command input parameter error,try `%s -help` for help",args[0])
|
||||
return fmt.Errorf("Command input parameter error,try `%s -help` for help", args[0])
|
||||
}
|
||||
|
||||
func RegisterCommandBool(cmdName string, defaultValue bool, usage string,fn CommandFunctionCB){
|
||||
func RegisterCommandBool(cmdName string, defaultValue bool, usage string, fn CommandFunctionCB) {
|
||||
var cmd command
|
||||
cmd.valType = boolType
|
||||
cmd.name = cmdName
|
||||
cmd.fn = fn
|
||||
cmd.usage = usage
|
||||
flag.BoolVar(&cmd.bValue, cmdName, defaultValue, usage)
|
||||
commandList = append(commandList,&cmd)
|
||||
commandList = append(commandList, &cmd)
|
||||
}
|
||||
|
||||
func RegisterCommandInt(cmdName string, defaultValue int, usage string,fn CommandFunctionCB){
|
||||
func RegisterCommandInt(cmdName string, defaultValue int, usage string, fn CommandFunctionCB) {
|
||||
var cmd command
|
||||
cmd.valType = intType
|
||||
cmd.name = cmdName
|
||||
cmd.fn = fn
|
||||
cmd.usage = usage
|
||||
flag.IntVar(&cmd.intValue, cmdName, defaultValue, usage)
|
||||
commandList = append(commandList,&cmd)
|
||||
commandList = append(commandList, &cmd)
|
||||
}
|
||||
|
||||
func RegisterCommandString(cmdName string, defaultValue string, usage string,fn CommandFunctionCB){
|
||||
func RegisterCommandString(cmdName string, defaultValue string, usage string, fn CommandFunctionCB) {
|
||||
var cmd command
|
||||
cmd.valType = stringType
|
||||
cmd.name = cmdName
|
||||
cmd.fn = fn
|
||||
cmd.usage = usage
|
||||
flag.StringVar(&cmd.strValue, cmdName, defaultValue, usage)
|
||||
commandList = append(commandList,&cmd)
|
||||
commandList = append(commandList, &cmd)
|
||||
}
|
||||
|
||||
func PrintDefaults(){
|
||||
func PrintDefaults() {
|
||||
fmt.Fprintf(os.Stderr, "Options:\n")
|
||||
|
||||
for _,val := range commandList {
|
||||
fmt.Fprintf(os.Stderr, " -%-10s%10s\n",val.name,val.usage)
|
||||
for _, val := range commandList {
|
||||
fmt.Fprintf(os.Stderr, " -%-10s%10s\n", val.name, val.usage)
|
||||
}
|
||||
}
|
||||
|
||||
func GetParamStringVal(paramName string) string{
|
||||
for _,cmd := range commandList {
|
||||
if cmd.name == paramName{
|
||||
func GetParamStringVal(paramName string) string {
|
||||
for _, cmd := range commandList {
|
||||
if cmd.name == paramName {
|
||||
return cmd.strValue
|
||||
}
|
||||
}
|
||||
@@ -114,11 +114,11 @@ func GetParamStringVal(paramName string) string{
|
||||
}
|
||||
|
||||
func GetParamBoolVal(paramName string) bool {
|
||||
for _,cmd := range commandList {
|
||||
if cmd.name == paramName{
|
||||
for _, cmd := range commandList {
|
||||
if cmd.name == paramName {
|
||||
return cmd.bValue
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user