From 6777a136e663a766baa8796f71d7e88114b66ccb Mon Sep 17 00:00:00 2001 From: surdeus Date: Fri, 24 Mar 2023 19:47:31 +0300 Subject: [PATCH] Use the new mtool implementation. --- go.mod | 2 +- go.sum | 2 ++ src/cmd/goblin/main.go | 4 +--- src/tool/awk/goawk.go | 7 +++++-- src/tool/ftest/main.go | 17 ++++------------- src/tool/grange/main.go | 21 +++++++-------------- src/tool/in/main.go | 17 ++++------------- src/tool/ln/main.go | 9 ++++----- src/tool/mk/main.go | 11 +++-------- src/tool/paths/main.go | 12 +++++------- src/tool/useprog/main.go | 20 +++++--------------- src/tool/whoami/main.go | 13 +++---------- 12 files changed, 44 insertions(+), 91 deletions(-) diff --git a/go.mod b/go.mod index 8d5edd3..f8eac01 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module github.com/surdeus/goblin go 1.18 -require github.com/surdeus/gomtool v0.0.0-20230324073313-c382bc403f8b // indirect +require github.com/surdeus/gomtool v0.0.0-20230324163514-3199e25c3890 // indirect diff --git a/go.sum b/go.sum index c1da825..bc2c4cd 100644 --- a/go.sum +++ b/go.sum @@ -14,3 +14,5 @@ github.com/surdeus/gomtool v0.0.0-20230324070550-2fb327b6a6a6 h1:aOXHUmhQS/mCo8r github.com/surdeus/gomtool v0.0.0-20230324070550-2fb327b6a6a6/go.mod h1:48d4QXOu0MwH0fbqseBInNdS6WiJ0+EzZU9K5sGu4uo= github.com/surdeus/gomtool v0.0.0-20230324073313-c382bc403f8b h1:9IJVeXxRDTPlp12DQ8M76ydoyDCUx6J4up5UM2DwIXQ= github.com/surdeus/gomtool v0.0.0-20230324073313-c382bc403f8b/go.mod h1:48d4QXOu0MwH0fbqseBInNdS6WiJ0+EzZU9K5sGu4uo= +github.com/surdeus/gomtool v0.0.0-20230324163514-3199e25c3890 h1:mT4VDgCb2AgPfq2k4GfXI8O3I2sIjHJUaJwBvx95AYM= +github.com/surdeus/gomtool v0.0.0-20230324163514-3199e25c3890/go.mod h1:48d4QXOu0MwH0fbqseBInNdS6WiJ0+EzZU9K5sGu4uo= diff --git a/src/cmd/goblin/main.go b/src/cmd/goblin/main.go index ac874d0..258ab22 100644 --- a/src/cmd/goblin/main.go +++ b/src/cmd/goblin/main.go @@ -18,7 +18,6 @@ import ( "github.com/surdeus/goblin/src/tool/mk" "github.com/surdeus/goblin/src/tool/mkdir" "github.com/surdeus/goblin/src/tool/noext" - "github.com/surdeus/goblin/src/tool/path" "github.com/surdeus/goblin/src/tool/paths" "github.com/surdeus/goblin/src/tool/quote" "github.com/surdeus/goblin/src/tool/read" @@ -57,8 +56,7 @@ func main() { "ftest": mtool.Tool{ftest.Run, "filter files by specified features", ""}, "range": mtool.Tool{grange.Run, "too lazy", ""}, "in": mtool.Tool{in.Run, "filter strings from stdin that aren not in arguments", ""}, - "useprog": mtool.Tool{useprog.Run, "print the name of the first existing program in arg list", ""}, - "path": mtool.Tool{path.Run, "print cross platform path based on cmd arguments", ""}, + "which": mtool.Tool{useprog.Run, "print the name or the path of the first existing program in arg list", ""}, "mk": mtool.Tool{mk.Run, "file dependency system, simpler make", ""}, "awk": mtool.Tool{awk.Run, "simple scripting language for working with string templates", ""}, "paths": mtool.Tool{ diff --git a/src/tool/awk/goawk.go b/src/tool/awk/goawk.go index ea55cc0..9c7135c 100644 --- a/src/tool/awk/goawk.go +++ b/src/tool/awk/goawk.go @@ -41,6 +41,8 @@ import ( "github.com/surdeus/goblin/src/tool/awk/interp" "github.com/surdeus/goblin/src/tool/awk/lexer" "github.com/surdeus/goblin/src/tool/awk/parser" + + "github.com/surdeus/gomtool/src/mtool" ) const ( @@ -67,7 +69,7 @@ Additional GoAWK arguments: ` ) -func Run(args []string) { +func Run(flags *mtool.Flags) { // Parse command line arguments manually rather than using the // "flag" package, so we can support flags with no space between // flag and argument, like '-F:' (allowed by POSIX) @@ -83,7 +85,8 @@ func Run(args []string) { outputMode := "" header := false - argv0 := args[0] + argv0 := flags.UtilName() + args := flags.Args() var i int for i = 1; i < len(args); i++ { diff --git a/src/tool/ftest/main.go b/src/tool/ftest/main.go index 18d5fdd..a8f0b3a 100644 --- a/src/tool/ftest/main.go +++ b/src/tool/ftest/main.go @@ -5,7 +5,7 @@ import ( "io" "bufio" "fmt" - "flag" + "github.com/surdeus/gomtool/src/mtool" ) var ( @@ -76,23 +76,14 @@ func checkFile(p string) bool { return true } -func Run(args []string) { - arg0 := args[0] - args = args[1:] - flagSet := flag.NewFlagSet(arg0, flag.ExitOnError) - flagSet.Usage = func() { - fmt.Fprintf(os.Stderr, "usage: %s \n", arg0) - flagSet.PrintDefaults() - os.Exit(1) - } - +func Run(flagSet *mtool.Flags) { flagSet.BoolVar(&flags[fileFlag], "f", false, "is file") flagSet.BoolVar(&flags[dirFlag], "d", false, "is directory") flagSet.BoolVar(&flags[writFlag], "w", false, "is writable") flagSet.BoolVar(&flags[readFlag], "r", false, "is readable") - flagSet.Parse(args) - args = flagSet.Args() + flagSet.Parse() + args := flagSet.Args() if len(args) != 0 { flagSet.Usage() diff --git a/src/tool/grange/main.go b/src/tool/grange/main.go index 41c3cc2..aba21e2 100644 --- a/src/tool/grange/main.go +++ b/src/tool/grange/main.go @@ -1,14 +1,13 @@ package grange /* Concatenate files in "stdout". */ import( - "os" - "flag" "fmt" "strconv" + "github.com/surdeus/gomtool/src/mtool" ) var( - flagSet *flag.FlagSet + flagSet *mtool.Flags args []string blockSize int rangeType string @@ -106,17 +105,11 @@ func ByteRange() { func RuneRange() { } -func Run(arg []string) { - arg0 := arg[0] - args = arg[1:] - flagSet = flag.NewFlagSet(arg0, flag.ExitOnError) - flagSet.StringVar(&rangeType, "t", "int", "range type") - flagSet.Usage = func() { - fmt.Fprintf(os.Stderr, "usage: %s [options] [files]\n", arg0) - flagSet.PrintDefaults() - os.Exit(1) - } - flagSet.Parse(args) +func Run(flags *mtool.Flags) { + flags.StringVar(&rangeType, "t", "int", "range type") + flags.Parse() args = flagSet.Args() + flagSet = flags + rangeTypeMap[rangeType]() } diff --git a/src/tool/in/main.go b/src/tool/in/main.go index 3f2e5e2..f414c3a 100644 --- a/src/tool/in/main.go +++ b/src/tool/in/main.go @@ -5,27 +5,18 @@ import ( "io" "bufio" "fmt" - "flag" + "github.com/surdeus/gomtool/src/mtool" ) -func Run(args []string) { +func Run(flagSet *mtool.Flags) { var ( print bool not bool ) - arg0 := args[0] - args = args[1:] - flagSet := flag.NewFlagSet(arg0, flag.ExitOnError) flagSet.BoolVar(&print, "p", false, "print matching lines") flagSet.BoolVar(¬, "n", false, "find not matching lines") - flagSet.Usage = func() { - fmt.Fprintf(os.Stderr, "usage: %s [str2, ...str3]\n", arg0) - flagSet.PrintDefaults() - os.Exit(1) - } - - flagSet.Parse(args) - args = flagSet.Args() + flagSet.Parse() + args := flagSet.Args() if len(args) == 0 { //flagSet.Usage() diff --git a/src/tool/ln/main.go b/src/tool/ln/main.go index 662b2c4..339e0dd 100644 --- a/src/tool/ln/main.go +++ b/src/tool/ln/main.go @@ -1,18 +1,17 @@ package ln import ( - "flag" "fmt" "os" + "github.com/surdeus/gomtool/src/mtool" ) -func Run(args []string) { +func Run(flagSet *mtool.Flags) { var lflag bool - flagSet := flag.NewFlagSet(args[0], flag.ExitOnError) flagSet.BoolVar(&lflag, "s", false, "make a symbolic link, not a hard one") - flagSet.Parse(args[1:]) - args = flagSet.Args() + flagSet.Parse() + args := flagSet.Args() if len(args) != 2 { flagSet.Usage() diff --git a/src/tool/mk/main.go b/src/tool/mk/main.go index 596b93c..64b1cb7 100644 --- a/src/tool/mk/main.go +++ b/src/tool/mk/main.go @@ -2,7 +2,6 @@ package mk import ( "bufio" - "flag" "fmt" "io/ioutil" "os" @@ -11,6 +10,7 @@ import ( "sync" "github.com/surdeus/goblin/src/pathx" + "github.com/surdeus/gomtool/src/mtool" ) // True if messages should be printed without fancy colors. @@ -317,24 +317,19 @@ func mkPrintRecipe(target string, recipe string, quiet bool) { mkMsgMutex.Unlock() } -func Run(args []string) { +func Run(flags *mtool.Flags) { var mkfilepath string var interactive bool var dryrun bool var shallowrebuild bool var quiet bool - arg0 := args[0] - args = args[1:] - if mkincdir := os.Getenv("MKINCDIR"); mkincdir == "" { homeDir, _ := os.UserHomeDir() homeDir = pathx.FromReal(homeDir).String() os.Setenv("MKINCDIR", homeDir+"/app/goblin/mk/inc") } - flags := flag.NewFlagSet(arg0, flag.ExitOnError) - flags.StringVar(&mkfilepath, "f", "mkfile", "use the given file as mkfile") flags.BoolVar(&dryrun, "n", false, "print commands without actually executing") flags.BoolVar(&shallowrebuild, "r", false, "force building of just targets") @@ -342,7 +337,7 @@ func Run(args []string) { flags.IntVar(&subprocsAllowed, "p", 4, "maximum number of jobs to execute in parallel") flags.BoolVar(&interactive, "i", false, "prompt before executing rules") flags.BoolVar(&quiet, "q", false, "don't print recipes before executing them") - flags.Parse(args) + flags.Parse() mkfile, err := os.Open(pathx.From(mkfilepath).Real()) if err != nil { diff --git a/src/tool/paths/main.go b/src/tool/paths/main.go index 63d8697..9cc6451 100644 --- a/src/tool/paths/main.go +++ b/src/tool/paths/main.go @@ -3,7 +3,6 @@ package paths import ( "bufio" "errors" - "flag" "fmt" "log" "os" @@ -11,6 +10,7 @@ import ( "strings" "github.com/surdeus/goblin/src/pathx" + "github.com/surdeus/gomtool/src/mtool" ) var ( @@ -50,17 +50,15 @@ func handlePath(p string) { fmt.Println(toPrint) } -func Run(args []string) { - arg0 := args[0] - args = args[1:] - flags := flag.NewFlagSet(arg0, flag.ExitOnError) +func Run(flags *mtool.Flags) { flags.StringVar(&part, "p", "all", "part of path you want to print") flags.BoolVar(&r, "r", false, "print real OS dependent paths") flags.BoolVar(&fromReal, "fr", false, "take input paths as real ones") flags.BoolVar(&ec, "ec", false, "escape characters (mostly for '\\' char in Git bash") - flags.Parse(args) - args = flags.Args() + flags.Parse() + args := flags.Args() + lhandler, ok := handlers[part] if !ok { log.Fatal(noPartErr) diff --git a/src/tool/useprog/main.go b/src/tool/useprog/main.go index 97e5e81..0d8469b 100644 --- a/src/tool/useprog/main.go +++ b/src/tool/useprog/main.go @@ -1,26 +1,16 @@ package useprog import ( - "os" "os/exec" "fmt" - "flag" + "github.com/surdeus/gomtool/src/mtool" ) -func Run(args []string) { - arg0 := args[0] - args = args[1:] - flagSet := flag.NewFlagSet(arg0, flag.ExitOnError) - flagSet.Usage = func() { - fmt.Fprintf(os.Stderr, "usage: %s [prog2, ...prog3]\n", arg0) - flagSet.PrintDefaults() - os.Exit(1) - } +func Run(flagSet *mtool.Flags) { + flagSet.Parse() + args := flagSet.Args() - flagSet.Parse(args) - args = flagSet.Args() - - if len(args) == 0 { + if len(args) < 1 { flagSet.Usage() } diff --git a/src/tool/whoami/main.go b/src/tool/whoami/main.go index 0c12ccd..c299788 100644 --- a/src/tool/whoami/main.go +++ b/src/tool/whoami/main.go @@ -3,20 +3,13 @@ package whoami import( "os" "os/user" - "flag" "fmt" "log" + "github.com/surdeus/gomtool/src/mtool" ) -func Run(args []string) { - arg0 := args[0] - args = args[1:] - flagSet := flag.NewFlagSet(arg0, flag.ExitOnError) - flagSet.Usage = func() { - fmt.Fprintf(os.Stderr, "Usage of %s: %s\n", arg0, arg0) - flagSet.PrintDefaults() - } - flagSet.Parse(args) +func Run(flagSet *mtool.Flags) { + flagSet.Parse() if len(flagSet.Args())>0 { flagSet.Usage()