feat: added custom args to be able to call the tools in more flexablie way.

This commit is contained in:
Andrey Parhomenko 2024-09-23 01:14:45 +05:00
parent cc0131c812
commit 3274f31b64
2 changed files with 24 additions and 21 deletions

View file

@ -9,18 +9,22 @@ 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
customArgs []any
envNameMap map[string][]string envNameMap map[string][]string
envMap map[string]string envMap map[string]string
} }
type ToolMap map[string]*Tool type ToolMap map[string]*Tool
func (flags *Flags) CustomArgs() []any {
return flags.customArgs
}
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) {
@ -150,4 +154,3 @@ func (flags *Flags) Args() []string {
func (flags *Flags) Tool() *Tool { func (flags *Flags) Tool() *Tool {
return flags.tool return flags.tool
} }

View file

@ -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() {
@ -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
} }