83 lines
1.9 KiB
Go
83 lines
1.9 KiB
Go
// Copyright 2017 The Xorm Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
// A Command is an implementation of a go command
|
|
// like go build or go fix.
|
|
type Command struct {
|
|
// Run runs the command.
|
|
// The args are the arguments after the command name.
|
|
Run func(cmd *Command, args []string)
|
|
|
|
// UsageLine is the one-line usage message.
|
|
// The first word in the line is taken to be the command name.
|
|
UsageLine string
|
|
|
|
// Short is the short description shown in the 'go help' output.
|
|
Short string
|
|
|
|
// Long is the long message shown in the 'go help <this-command>' output.
|
|
Long string
|
|
|
|
// Flag is a set of flags specific to this command.
|
|
Flags map[string]bool
|
|
}
|
|
|
|
// Name returns the command's name: the first word in the usage line.
|
|
func (c *Command) Name() string {
|
|
name := c.UsageLine
|
|
i := strings.Index(name, " ")
|
|
if i >= 0 {
|
|
name = name[:i]
|
|
}
|
|
return name
|
|
}
|
|
|
|
func (c *Command) Usage() {
|
|
fmt.Fprintf(os.Stderr, "usage: %s\n\n", c.UsageLine)
|
|
fmt.Fprintf(os.Stderr, "%s\n", strings.TrimSpace(c.Long))
|
|
os.Exit(2)
|
|
}
|
|
|
|
// Runnable reports whether the command can be run; otherwise
|
|
// it is a documentation pseudo-command such as importpath.
|
|
func (c *Command) Runnable() bool {
|
|
return c.Run != nil
|
|
}
|
|
|
|
// checkFlags checks if the flag exists with correct format.
|
|
func checkFlags(flags map[string]bool, args []string, print func(string)) int {
|
|
num := 0 // Number of valid flags, use to cut out.
|
|
for i, f := range args {
|
|
// Check flag prefix '-'.
|
|
if !strings.HasPrefix(f, "-") {
|
|
// Not a flag, finish check process.
|
|
break
|
|
}
|
|
|
|
// Check if it a valid flag.
|
|
if v, ok := flags[f]; ok {
|
|
flags[f] = !v
|
|
if !v {
|
|
print(f)
|
|
} else {
|
|
fmt.Println("DISABLE: " + f)
|
|
}
|
|
} else {
|
|
fmt.Printf("[ERRO] Unknown flag: %s.\n", f)
|
|
return -1
|
|
}
|
|
num = i + 1
|
|
}
|
|
|
|
return num
|
|
}
|