2020-01-19 06:06:00 +00:00
|
|
|
package arguments
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"flag"
|
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
var FlagName = "command"
|
|
|
|
|
|
|
|
type FlagSet struct {
|
|
|
|
*flag.FlagSet
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFlagSet() *FlagSet {
|
|
|
|
fs := flag.NewFlagSet(FlagName, flag.ContinueOnError)
|
|
|
|
fs.SetOutput(ioutil.Discard)
|
|
|
|
|
|
|
|
return &FlagSet{fs}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FlagSet) Usage() string {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
fs.FlagSet.SetOutput(&buf)
|
|
|
|
fs.FlagSet.Usage()
|
|
|
|
fs.FlagSet.SetOutput(ioutil.Discard)
|
|
|
|
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
type Flag struct {
|
2020-01-24 03:17:03 +00:00
|
|
|
command string
|
2020-01-19 06:06:00 +00:00
|
|
|
arguments []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Flag) ParseContent(arguments []string) error {
|
|
|
|
// trim the command out
|
2020-01-24 03:17:03 +00:00
|
|
|
f.command, f.arguments = arguments[0], arguments[1:]
|
2020-01-19 06:06:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Flag) Usage() string {
|
2020-01-25 21:01:05 +00:00
|
|
|
return "[flags] arguments..."
|
2020-01-19 06:06:00 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 03:17:03 +00:00
|
|
|
func (f *Flag) Command() string {
|
|
|
|
return f.command
|
|
|
|
}
|
|
|
|
|
2020-01-19 06:06:00 +00:00
|
|
|
func (f *Flag) Args() []string {
|
|
|
|
return f.arguments
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Flag) Arg(n int) string {
|
|
|
|
if n < 0 || n >= len(f.arguments) {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return f.arguments[n]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Flag) String() string {
|
|
|
|
return strings.Join(f.arguments, " ")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Flag) With(fs *flag.FlagSet) error {
|
|
|
|
return fs.Parse(f.arguments)
|
|
|
|
}
|