This commit is contained in:
Andrey Parhomenko 2023-03-24 16:54:51 +03:00
parent 29b71aa353
commit a2ad07dc5e
22 changed files with 122 additions and 195 deletions

2
go.mod
View file

@ -2,4 +2,4 @@ module github.com/surdeus/goblin
go 1.18 go 1.18
require github.com/surdeus/gomtool v0.0.0-20230216120523-b00a31dd9887 // indirect require github.com/surdeus/gomtool v0.0.0-20230324073313-c382bc403f8b // indirect

6
go.sum
View file

@ -8,3 +8,9 @@ github.com/surdeus/gomtool v0.0.0-20221123071619-d67de6873631 h1:dSnG4J2jusYRVnR
github.com/surdeus/gomtool v0.0.0-20221123071619-d67de6873631/go.mod h1:48d4QXOu0MwH0fbqseBInNdS6WiJ0+EzZU9K5sGu4uo= github.com/surdeus/gomtool v0.0.0-20221123071619-d67de6873631/go.mod h1:48d4QXOu0MwH0fbqseBInNdS6WiJ0+EzZU9K5sGu4uo=
github.com/surdeus/gomtool v0.0.0-20230216120523-b00a31dd9887 h1:lKe6TayY2qB50RqPYrHBlHPeJPevNViM+UQn8/2jBxc= github.com/surdeus/gomtool v0.0.0-20230216120523-b00a31dd9887 h1:lKe6TayY2qB50RqPYrHBlHPeJPevNViM+UQn8/2jBxc=
github.com/surdeus/gomtool v0.0.0-20230216120523-b00a31dd9887/go.mod h1:48d4QXOu0MwH0fbqseBInNdS6WiJ0+EzZU9K5sGu4uo= github.com/surdeus/gomtool v0.0.0-20230216120523-b00a31dd9887/go.mod h1:48d4QXOu0MwH0fbqseBInNdS6WiJ0+EzZU9K5sGu4uo=
github.com/surdeus/gomtool v0.0.0-20230324065513-d8d05cb8077d h1:COfpUMABa+RrsD05bOnXDx9FX+oaJ+dnKiLpqOODxl8=
github.com/surdeus/gomtool v0.0.0-20230324065513-d8d05cb8077d/go.mod h1:48d4QXOu0MwH0fbqseBInNdS6WiJ0+EzZU9K5sGu4uo=
github.com/surdeus/gomtool v0.0.0-20230324070550-2fb327b6a6a6 h1:aOXHUmhQS/mCo8r72dERsxAnYrlk/hP61BAyZ79MkDs=
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=

View file

@ -30,48 +30,51 @@ import (
"github.com/surdeus/goblin/src/tool/wc" "github.com/surdeus/goblin/src/tool/wc"
"github.com/surdeus/goblin/src/tool/whoami" "github.com/surdeus/goblin/src/tool/whoami"
"github.com/surdeus/goblin/src/tool/yes" "github.com/surdeus/goblin/src/tool/yes"
mtool "github.com/surdeus/gomtool/src/multitool" "github.com/surdeus/gomtool/src/mtool"
) )
func main() { func main() {
tools := mtool.Tools{ tools := mtool.Tools{
"basename": mtool.Tool{basename.Run, "get base name of file path"}, "basename": mtool.Tool{basename.Run, "get base name of file path", ""},
"cat": mtool.Tool{cat.Run, "print file data to the standard output"}, "cat": mtool.Tool{cat.Run, "print file data to the standard output", ""},
"mkdir": mtool.Tool{mkdir.Run, "make new directory"}, "mkdir": mtool.Tool{mkdir.Run, "make new directory", ""},
"echo": mtool.Tool{echo.Run, "print strings to the standard output"}, "echo": mtool.Tool{echo.Run, "print strings to the standard output", ""},
"true": mtool.Tool{gtrue.Run, "exit with true status"}, "true": mtool.Tool{gtrue.Run, "exit with true status", ""},
"false": mtool.Tool{gfalse.Run, "exit with false status"}, "false": mtool.Tool{gfalse.Run, "exit with false status", ""},
"sort": mtool.Tool{sort.Run, "sort strings inputed from standard input"}, "sort": mtool.Tool{sort.Run, "sort strings inputed from standard input", ""},
"tac": mtool.Tool{tac.Run, "print strings from standard input in reversed order"}, "tac": mtool.Tool{tac.Run, "print strings from standard input in reversed order", ""},
"ls": mtool.Tool{ls.Run, "list directory content"}, "ls": mtool.Tool{ls.Run, "list directory content", ""},
"yes": mtool.Tool{yes.Run, "print string infinite/exact amount times"}, "yes": mtool.Tool{yes.Run, "print string infinite/exact amount times", ""},
"date": mtool.Tool{date.Run, "print current date"}, "date": mtool.Tool{date.Run, "print current date", ""},
"uniq": mtool.Tool{uniq.Run, "filter repeated strings"}, "uniq": mtool.Tool{uniq.Run, "filter repeated strings", ""},
"quote": mtool.Tool{quote.Run, "quote words containing space characters"}, "quote": mtool.Tool{quote.Run, "quote words containing space characters", ""},
"urlprs": mtool.Tool{urlprs.Run, "parse URLs"}, "urlprs": mtool.Tool{urlprs.Run, "parse URLs", ""},
"noext": mtool.Tool{noext.Run, "print file path without extension"}, "noext": mtool.Tool{noext.Run, "print file path without extension", ""},
"mergelbl": mtool.Tool{mergelbl.Run, "merge line by line"}, "mergelbl": mtool.Tool{mergelbl.Run, "merge line by line", ""},
"ec": mtool.Tool{ec.Run, "render escape sequences"}, "ec": mtool.Tool{ec.Run, "render escape sequences", ""},
"read": mtool.Tool{read.Run, "read lines and exit"}, "read": mtool.Tool{read.Run, "read lines and exit", ""},
"wc": mtool.Tool{wc.Run, "count words, bytes, runes etc"}, "wc": mtool.Tool{wc.Run, "count words, bytes, runes etc", ""},
"ftest": mtool.Tool{ftest.Run, "filter files by specified features"}, "ftest": mtool.Tool{ftest.Run, "filter files by specified features", ""},
"range": mtool.Tool{grange.Run, "too lazy"}, "range": mtool.Tool{grange.Run, "too lazy", ""},
"in": mtool.Tool{in.Run, "filter strings from stdin that aren not in arguments"}, "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"}, "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"}, "path": mtool.Tool{path.Run, "print cross platform path based on cmd arguments", ""},
"mk": mtool.Tool{mk.Run, "file dependency system, simpler make"}, "mk": mtool.Tool{mk.Run, "file dependency system, simpler make", ""},
"awk": mtool.Tool{awk.Run, "simple scripting language for working with string templates"}, "awk": mtool.Tool{awk.Run, "simple scripting language for working with string templates", ""},
"paths": mtool.Tool{ "paths": mtool.Tool{
paths.Run, paths.Run,
"convert UNIX slash separated paths into the OS compatible ones", "convert UNIX slash separated paths into the OS compatible ones",
"",
}, },
"whoami": mtool.Tool{ "whoami": mtool.Tool{
whoami.Run, whoami.Run,
"print current user name", "print current user name",
"",
}, },
"ln": mtool.Tool{ "ln": mtool.Tool{
ln.Run, ln.Run,
"link files", "link files",
"",
}, },
} }

View file

@ -3,9 +3,8 @@ package basename
import( import(
"fmt" "fmt"
"flag"
"os"
"path" "path"
"github.com/surdeus/gomtool/src/mtool"
) )
var( var(
@ -18,16 +17,9 @@ func Base(p string) string {
return path.Base(p) return path.Base(p)
} }
func Run(argv []string) { func Run(flags *mtool.Flags) {
arg0 = argv[0] flags.Parse()
args = argv[1:] args := flags.Args()
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
flagSet.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s [files]\n", arg0, arg0)
flagSet.PrintDefaults()
}
flagSet.Parse(args)
args = flagSet.Args()
lasti := len(args) - 1 lasti := len(args) - 1
for i, v := range args { for i, v := range args {

View file

@ -3,8 +3,8 @@ package cat
import( import(
"os" "os"
"io" "io"
"flag"
"fmt" "fmt"
"github.com/surdeus/gomtool/src/mtool"
) )
var( var(
@ -37,22 +37,15 @@ func fcat(f *os.File) error {
return nil return nil
} }
func Run(args []string) { func Run(flags *mtool.Flags) {
arg0 := args[0] flags.IntVar(&blockSize, "bs", 512, "block size")
args = args[1:] flags.Parse()
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError) args := flags.Args()
flagSet.IntVar(&blockSize, "bs", 512, "block size")
flagSet.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s [options] [files]\n", arg0)
flagSet.PrintDefaults()
}
flagSet.Parse(args)
args = flagSet.Args()
if len(args)>0 { if len(args)>0 {
for _, p := range args { for _, p := range args {
e := Cat(p) e := Cat(p)
if e != nil { if e != nil {
fmt.Fprintf(os.Stderr, "%s: %s.\n", arg0, e) fmt.Fprintf(os.Stderr, "%s.\n", e)
} }
} }
} else { } else {

View file

@ -1,25 +1,17 @@
package date package date
import( import(
"os"
"flag"
"fmt" "fmt"
"time" "time"
"github.com/surdeus/gomtool/src/mtool"
) )
func Run(args []string) { func Run(flagSet *mtool.Flags) {
arg0 := args[0] flagSet.Parse()
args = args[1:] args := flagSet.Args()
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)
if len(flagSet.Args())>0 { if len(args) > 0 {
flagSet.Usage() flagSet.Usage()
os.Exit(1)
} }
date := time.Now() date := time.Now()

View file

@ -6,6 +6,7 @@ import(
"os" "os"
"bufio" "bufio"
"log" "log"
"github.com/surdeus/gomtool/src/mtool"
) )
const( const(
@ -31,7 +32,7 @@ handleEscChar(rd *bufio.Reader) error {
} }
func func
Run(args []string) { Run(flags *mtool.Flags) {
rd := bufio.NewReader(os.Stdin) rd := bufio.NewReader(os.Stdin)
for { for {
r, _, e := rd.ReadRune() r, _, e := rd.ReadRune()

View file

@ -3,7 +3,7 @@ package echo
import ( import (
"fmt" "fmt"
"flag" "github.com/surdeus/gomtool/src/mtool"
) )
var( var(
@ -11,13 +11,15 @@ var(
eol = "\n" eol = "\n"
) )
func Run(args []string) { func Run(flagSet *mtool.Flags) {
var nflag bool var nflag bool
flagSet := flag.NewFlagSet(args[0], flag.ExitOnError)
flagSet.BoolVar(&nflag, "n", false, "Do not print new line character.") flagSet.BoolVar(&nflag, "n", false, "Do not print new line character.")
flagSet.StringVar(&del, "d", " ", "Delimiter of arguments") flagSet.StringVar(&del, "d", " ", "Delimiter of arguments")
flagSet.Parse(args[1:])
args = flagSet.Args() flagSet.Parse()
args := flagSet.Args()
l := len(args) - 1 l := len(args) - 1
for i, s := range args { for i, s := range args {
fmt.Print(s) fmt.Print(s)

View file

@ -1,4 +1,10 @@
package gfalse package gfalse
func Run(args []string) { import(
"os"
"github.com/surdeus/gomtool/src/mtool"
)
func Run(flags *mtool.Flags) {
os.Exit(1)
} }

View file

@ -1,5 +1,5 @@
package gtrue package gtrue
func Run(args []string) { import "github.com/surdeus/gomtool/src/mtool"
}
func Run(flags *mtool.Flags) {}

View file

@ -2,17 +2,15 @@ package ls
import( import(
"os" "os"
"fmt" "fmt"
"flag"
"strings" "strings"
"regexp" "regexp"
"path" "path"
"github.com/surdeus/gomtool/src/mtool"
) )
var( var(
listHidden bool listHidden bool
args []string args []string
arg0 string
flagSet *flag.FlagSet
) )
var slashRegexp = regexp.MustCompile("/+") var slashRegexp = regexp.MustCompile("/+")
@ -126,30 +124,25 @@ ls(p string, fold int) error {
return nil return nil
} }
func Run(argv []string) { func Run(flagSet *mtool.Flags) {
arg0 = argv[0]
args = argv[1:]
flagSet = flag.NewFlagSet(arg0, flag.ExitOnError)
var foldLvl int var foldLvl int
flagSet.IntVar(&foldLvl, "r", 1, "List recursively with choosing deepness, can't be negative or zero.") flagSet.IntVar(&foldLvl, "r", 1, "List recursively with choosing deepness, can't be negative or zero.")
flagSet.BoolVar(&listHidden, "a", false, "List hidden files.") flagSet.BoolVar(&listHidden, "a", false, "List hidden files.")
flagSet.Parse(args) flagSet.Parse()
args = flagSet.Args() args = flagSet.Args()
if foldLvl<0 { if foldLvl<0 {
flagSet.Usage() flagSet.Usage()
os.Exit(1)
} }
if foldLvl==0 && len(args)==0 { if foldLvl==0 && len(args)==0 {
flagSet.Usage() flagSet.Usage()
os.Exit(1)
} }
if len(args) == 0 { if len(args) == 0 {
foldLvl -= 1 foldLvl -= 1
if l, e := ReadDir(".") ; e != nil { if l, e := ReadDir(".") ; e != nil {
fmt.Fprintf(os.Stderr, "%s: %s.\n", arg0, e) fmt.Fprintf(os.Stderr, "%s.\n", e)
} else { } else {
for _, f := range l { for _, f := range l {
if !shouldList(f.Name()) { if !shouldList(f.Name()) {
@ -162,8 +155,7 @@ func Run(argv []string) {
e := ls(f.Name(), foldLvl) e := ls(f.Name(), foldLvl)
if e!=nil { if e!=nil {
fmt.Fprintf(os.Stderr, fmt.Fprintf(os.Stderr,
"%s: %s\n", "%s\n", e)
arg0, e)
} }
} }
} }
@ -172,7 +164,7 @@ func Run(argv []string) {
for _, p := range args { for _, p := range args {
e := ls(p, foldLvl) e := ls(p, foldLvl)
if e != nil { if e != nil {
fmt.Fprintf(os.Stderr, "%s: %s\n", arg0, e) fmt.Fprintf(os.Stderr, "%s\n", e)
} }
} }

View file

@ -4,11 +4,11 @@ import(
"os" "os"
"fmt" "fmt"
"bufio" "bufio"
"flag"
"log" "log"
"github.com/surdeus/gomtool/src/mtool"
) )
func Run(args []string) { func Run(flagSet *mtool.Flags) {
var( var(
e error e error
buf string buf string
@ -16,15 +16,8 @@ func Run(args []string) {
rsep := '\n' rsep := '\n'
wsep := "\n" wsep := "\n"
//del := "" //del := ""
arg0 := args[0] flagSet.Parse()
args = args[1:] args := flagSet.Args()
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
flagSet.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s <file1> <file2> .. [fileN]\n", arg0)
flagSet.PrintDefaults()
}
flagSet.Parse(args)
args = flagSet.Args()
files := make([]*os.File, len(args)) files := make([]*os.File, len(args))
for i, v := range args { for i, v := range args {

View file

@ -1,29 +1,22 @@
package mkdir package mkdir
import ( import (
"flag"
"fmt" "fmt"
"os" "os"
"github.com/surdeus/goblin/src/pathx" "github.com/surdeus/goblin/src/pathx"
"github.com/surdeus/gomtool/src/mtool"
) )
func Run(args []string) { func Run(flagSet *mtool.Flags) {
arg0 := args[0]
args = args[1:]
var ( var (
parentFlag bool parentFlag bool
modeArg int modeArg int
) )
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
flagSet.BoolVar(&parentFlag, "p", false, "No error if existing, make parent as needed.") flagSet.BoolVar(&parentFlag, "p", false, "No error if existing, make parent as needed.")
flagSet.IntVar(&modeArg, "m", 0766, "Set file `mode`.") flagSet.IntVar(&modeArg, "m", 0766, "Set file `mode`.")
flagSet.Usage = func() { flagSet.Parse()
fmt.Fprintf(os.Stderr, "Usage of %s: %s [options] [files]\n", arg0, arg0) args := flagSet.Args()
flagSet.PrintDefaults()
}
flagSet.Parse(args)
args = flagSet.Args()
if len(args) == 0 { if len(args) == 0 {
flagSet.Usage() flagSet.Usage()
os.Exit(1) os.Exit(1)
@ -38,7 +31,7 @@ func Run(args []string) {
e = os.Mkdir(pth, mode) e = os.Mkdir(pth, mode)
} }
if e != nil { if e != nil {
fmt.Fprintf(os.Stderr, "%s: %s.\n", arg0, e) fmt.Fprintf(os.Stderr, "%s\n", e)
} }
} }
} }

View file

@ -3,8 +3,7 @@ package noext
import( import(
"fmt" "fmt"
"flag" "github.com/surdeus/gomtool/src/mtool"
"os"
) )
var( var(
@ -25,15 +24,9 @@ func NoExt(p string) string {
return p[:i] return p[:i]
} }
func Run(argv []string) { func Run(flagSet *mtool.Flags) {
arg0 = argv[0] flagSet.Parse()
args = argv[1:] args := flagSet.Args()
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError) if len(args) < 1 { flagSet.Usage() }
flagSet.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: %s [files]\n", arg0, arg0)
flagSet.PrintDefaults()
}
flagSet.Parse(args)
args = flagSet.Args()
fmt.Printf("%s", NoExt(args[0])) fmt.Printf("%s", NoExt(args[0]))
} }

View file

@ -4,10 +4,10 @@ package quote
import( import(
"os" "os"
"io" "io"
"flag"
"fmt" "fmt"
"unicode" "unicode"
"bufio" "bufio"
"github.com/surdeus/gomtool/src/mtool"
) )
func HasWhiteSpace(s string) bool { func HasWhiteSpace(s string) bool {
@ -19,16 +19,8 @@ func HasWhiteSpace(s string) bool {
return false return false
} }
func Run(args []string) { func Run(flagSet *mtool.Flags) {
arg0 := args[0] flagSet.Parse()
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)
args = flagSet.Args()
r := bufio.NewReader(os.Stdin) r := bufio.NewReader(os.Stdin)
for{ for{

View file

@ -3,26 +3,18 @@ package read
import( import(
"os" "os"
"bufio" "bufio"
"flag"
"fmt" "fmt"
"github.com/surdeus/gomtool/src/mtool"
) )
var( var(
nLines int nLines int
) )
func Run(args []string) { func Run(flagSet *mtool.Flags) {
arg0 := args[0]
args = args[1:]
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
flagSet.IntVar(&nLines, "n", 1, "amount of lines") flagSet.IntVar(&nLines, "n", 1, "amount of lines")
flagSet.Usage = func() { flagSet.Parse()
fmt.Fprintf(os.Stderr, "usage: %s [options] [files]\n", arg0) //args := flagSet.Args()
flagSet.PrintDefaults()
os.Exit(1)
}
flagSet.Parse(args)
args = flagSet.Args()
if nLines <= 0 { if nLines <= 0 {
flagSet.Usage() flagSet.Usage()

View file

@ -4,14 +4,11 @@ import (
"fmt" "fmt"
"os" "os"
"sort" "sort"
"flag"
"github.com/surdeus/goblin/src/input" "github.com/surdeus/goblin/src/input"
"github.com/surdeus/gomtool/src/mtool"
) )
func Run(args []string) { func Run(flagSet *mtool.Flags) {
flagSet := flag.NewFlagSet(args[0], flag.ExitOnError)
flagSet.Parse(args[1:])
lines, _ := input.ReadAllLines(os.Stdin) lines, _ := input.ReadAllLines(os.Stdin)
sort.Strings(lines) sort.Strings(lines)
for _, l := range lines { for _, l := range lines {

View file

@ -3,9 +3,9 @@ package tac
import( import(
"os" "os"
"io" "io"
"flag"
"fmt" "fmt"
"bufio" "bufio"
"github.com/surdeus/gomtool/src/mtool"
) )
func reverse(a []string) chan string { func reverse(a []string) chan string {
@ -47,21 +47,14 @@ func ftac(f *os.File) error {
return nil return nil
} }
func Run(args []string) { func Run(flagSet *mtool.Flags) {
arg0 := args[0] flagSet.Parse()
args = args[1:] args := flagSet.Args()
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
flagSet.Usage = func () {
fmt.Fprintf(os.Stderr, "Usage of %s: %s [files]\n", arg0, arg0)
flagSet.PrintDefaults()
}
flagSet.Parse(args)
args = flagSet.Args()
if len(args)>0 { if len(args)>0 {
for _, p := range args { for _, p := range args {
e := tac(p) e := tac(p)
if e != nil { if e != nil {
fmt.Fprintf(os.Stderr, "%s: %s.\n", arg0, e) fmt.Fprintf(os.Stderr, "%s.\n", e)
} }
} }
} else { } else {

View file

@ -4,25 +4,18 @@ package uniq
import( import(
"os" "os"
"fmt" "fmt"
"flag"
"bufio" "bufio"
"io" "io"
"github.com/surdeus/gomtool/src/mtool"
) )
func Run(args []string) { func Run(flagSet *mtool.Flags) {
var( var(
Uflag bool Uflag bool
) )
arg0 := args[0]
args = args[1:]
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
flagSet.BoolVar(&Uflag, "U", false, "Print every line just one time.") flagSet.BoolVar(&Uflag, "U", false, "Print every line just one time.")
flagSet.Usage = func() { flagSet.Parse()
fmt.Fprintf(os.Stderr, "Usage of %s: %s [options] [string]\n", arg0, arg0) //args := flagSet.Args()
flagSet.PrintDefaults()
}
flagSet.Parse(args)
args = flagSet.Args()
r := bufio.NewReader(os.Stdin) r := bufio.NewReader(os.Stdin)
u := make(map[string]int) u := make(map[string]int)

View file

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"net" "net"
"net/url" "net/url"
"github.com/surdeus/gomtool/src/mtool"
) )
var nilStr = "_" var nilStr = "_"
@ -16,10 +17,13 @@ func printNil(s string) {
fmt.Println(nilStr) fmt.Println(nilStr)
} }
} }
func Run(args []string) { func Run(flags *mtool.Flags) {
nilStr = "_" nilStr = "_"
s := args[1] flags.Parse()
args := flags.Args()
if len(args) < 1 { flags.Usage() }
s := args[0]
u, err := url.Parse(s) u, err := url.Parse(s)
if err != nil { if err != nil {

View file

@ -6,8 +6,8 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"unicode" "unicode"
"flag"
"strconv" "strconv"
"github.com/surdeus/gomtool/src/mtool"
) )
var ( var (
@ -46,21 +46,17 @@ func finish() {
os.Exit(0) os.Exit(0)
} }
func Run(args []string) { func Run(flagSet *mtool.Flags) {
var ( var (
r rune r rune
siz int siz int
) )
arg0 := args[0]
args = args[1:]
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
flagSet.BoolVar(&flags[charsFlag], "c", false, "print amount of chars(bytes)") flagSet.BoolVar(&flags[charsFlag], "c", false, "print amount of chars(bytes)")
flagSet.BoolVar(&flags[runesFlag], "r", false, "print amount of runes in UTF stream") flagSet.BoolVar(&flags[runesFlag], "r", false, "print amount of runes in UTF stream")
flagSet.BoolVar(&flags[wordsFlag], "w", false, "print amount of words") flagSet.BoolVar(&flags[wordsFlag], "w", false, "print amount of words")
flagSet.BoolVar(&flags[linesFlag], "l", false, "print amount of lines") flagSet.BoolVar(&flags[linesFlag], "l", false, "print amount of lines")
flagSet.Parse(args) flagSet.Parse()
for i, v := range flags { for i, v := range flags {
if v { if v {

View file

@ -4,9 +4,9 @@ package yes
import( import(
"os" "os"
"fmt" "fmt"
"flag"
"strings" "strings"
"github.com/surdeus/goblin/src/input" "github.com/surdeus/goblin/src/input"
"github.com/surdeus/gomtool/src/mtool"
) )
var( var(
nArg int nArg int
@ -24,24 +24,18 @@ func yes(s string){
} }
} }
func Run(args []string) { func Run(flagSet *mtool.Flags) {
var( var(
stdinFlag bool stdinFlag bool
nFlag bool nFlag bool
s string s string
) )
arg0 := args[0]
args = args[1:]
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
flagSet.BoolVar(&stdinFlag, "s", false, "Read string from stdin.") flagSet.BoolVar(&stdinFlag, "s", false, "Read string from stdin.")
flagSet.BoolVar(&nFlag, "n", false, "Do not add net line character.") flagSet.BoolVar(&nFlag, "n", false, "Do not add net line character.")
flagSet.IntVar(&nArg, "N", -1, "Repeat input N times. Negative value means infinite cycle.") flagSet.IntVar(&nArg, "N", -1, "Repeat input N times. Negative value means infinite cycle.")
flagSet.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s: %s [options] [string]\n", arg0, arg0) flagSet.Parse()
flagSet.PrintDefaults() args := flagSet.Args()
}
flagSet.Parse(args)
args = flagSet.Args()
if stdinFlag { if stdinFlag {
in, _ := input.ReadAllRaw(os.Stdin) in, _ := input.ReadAllRaw(os.Stdin)