feat: added custom args to be able to call the tools in more flexablie way.
This commit is contained in:
parent
cc0131c812
commit
561b23ffc8
2 changed files with 20 additions and 21 deletions
|
@ -9,21 +9,21 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
type Flags struct {
|
type Flags struct {
|
||||||
*flag.FlagSet
|
*flag.FlagSet
|
||||||
tool *Tool
|
tool *Tool
|
||||||
args []string
|
args []string
|
||||||
parsedArgs []string
|
parsedArgs []string
|
||||||
envNameMap map[string] []string
|
customArgs []any
|
||||||
envMap map[string]string
|
envNameMap map[string][]string
|
||||||
|
envMap map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
type ToolMap map[string] *Tool
|
type ToolMap map[string]*Tool
|
||||||
|
|
||||||
func (flags *Flags) wasPassed(name string) bool {
|
func (flags *Flags) wasPassed(name string) bool {
|
||||||
found := false
|
found := false
|
||||||
flags.Visit(func(f *flag.Flag){
|
flags.Visit(func(f *flag.Flag) {
|
||||||
if f.Name == name {
|
if f.Name == name {
|
||||||
found = true
|
found = true
|
||||||
}
|
}
|
||||||
|
@ -150,4 +150,3 @@ func (flags *Flags) Args() []string {
|
||||||
func (flags *Flags) Tool() *Tool {
|
func (flags *Flags) Tool() *Tool {
|
||||||
return flags.tool
|
return flags.tool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
package mtool
|
package mtool
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"text/tabwriter"
|
|
||||||
"strings"
|
|
||||||
"flag"
|
"flag"
|
||||||
"sort"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
"text/tabwriter"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Tool struct {
|
type Tool struct {
|
||||||
|
@ -107,7 +107,7 @@ func (t *Tool) PrintSubs(out io.Writer) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Tool) Run(args []string) {
|
func (t *Tool) Run(args []string, customArgs ...any) {
|
||||||
var(
|
var(
|
||||||
usageTool *Tool
|
usageTool *Tool
|
||||||
)
|
)
|
||||||
|
@ -120,6 +120,7 @@ func (t *Tool) Run(args []string) {
|
||||||
FlagSet : flagSet,
|
FlagSet : flagSet,
|
||||||
envMap: make(map[string]string),
|
envMap: make(map[string]string),
|
||||||
envNameMap: make(map[string] []string),
|
envNameMap: make(map[string] []string),
|
||||||
|
customArgs: customArgs,
|
||||||
}
|
}
|
||||||
out := flags.Output()
|
out := flags.Output()
|
||||||
flags.Usage = func() {
|
flags.Usage = func() {
|
||||||
|
@ -150,7 +151,7 @@ func (t *Tool) Run(args []string) {
|
||||||
usageTool.FullName(), FormatInCode(t.desc, true),
|
usageTool.FullName(), FormatInCode(t.desc, true),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Usage
|
// Usage
|
||||||
fmt.Fprintf(
|
fmt.Fprintf(
|
||||||
|
@ -160,7 +161,7 @@ func (t *Tool) Run(args []string) {
|
||||||
if hasOptions {
|
if hasOptions {
|
||||||
fmt.Fprintf(out, " [options]")
|
fmt.Fprintf(out, " [options]")
|
||||||
}
|
}
|
||||||
|
|
||||||
if usageTool.usage != "" {
|
if usageTool.usage != "" {
|
||||||
fmt.Fprintf(
|
fmt.Fprintf(
|
||||||
out,
|
out,
|
||||||
|
@ -178,16 +179,16 @@ func (t *Tool) Run(args []string) {
|
||||||
FormatInCode(usageTool.ldesc, true),
|
FormatInCode(usageTool.ldesc, true),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Options
|
// Options
|
||||||
if hasOptions {
|
if hasOptions {
|
||||||
fmt.Fprintln(out, "\nOptions:")
|
fmt.Fprintln(out, "\nOptions:")
|
||||||
fmt.Fprintln(out, " -- option terminator")
|
fmt.Fprintln(out, " -- option terminator")
|
||||||
flags.PrintDefaults()
|
flags.PrintDefaults()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
flags.args = args
|
flags.args = args
|
||||||
|
|
||||||
// If the tool has its own handler run it.
|
// If the tool has its own handler run it.
|
||||||
|
@ -234,12 +235,12 @@ func (t *Tool) Run(args []string) {
|
||||||
fmt.Fprint(out, "\nCommands:\n")
|
fmt.Fprint(out, "\nCommands:\n")
|
||||||
t.PrintSubs(out)
|
t.PrintSubs(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
toolName := args[0]
|
toolName := args[0]
|
||||||
args = args[1:]
|
args = args[1:]
|
||||||
|
|
||||||
if _, ok := t.subs[toolName] ; !ok {
|
if _, ok := t.subs[toolName] ; !ok {
|
||||||
fmt.Fprintf(
|
fmt.Fprintf(
|
||||||
out,
|
out,
|
||||||
|
@ -253,7 +254,7 @@ func (t *Tool) Run(args []string) {
|
||||||
|
|
||||||
sub := t.subs[toolName]
|
sub := t.subs[toolName]
|
||||||
usageTool = sub
|
usageTool = sub
|
||||||
sub.Run(args)
|
sub.Run(args, customArgs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the built-in
|
// Returns the built-in
|
||||||
|
@ -278,4 +279,3 @@ func FormatInCode(
|
||||||
}
|
}
|
||||||
return desc
|
return desc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue