From 9ee2ce65b461e07134d533735817b767bff486d5 Mon Sep 17 00:00:00 2001 From: jienfak Date: Sat, 7 Mar 2020 16:14:40 +0500 Subject: [PATCH] mkdir: Simplified; usage function changed. cat: usage function changed. tac: Fixed incorrect "ftac" function realization; usage function changed. --- cat/cat.go | 4 ++++ mkdir/mkdir.go | 41 ++++++++++++++++++++--------------------- tac/tac.go | 6 +++++- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/cat/cat.go b/cat/cat.go index 5a72933..a7aef1c 100644 --- a/cat/cat.go +++ b/cat/cat.go @@ -38,6 +38,10 @@ func Run(args []string) int { arg0 := args[0] args = args[1:] 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 { diff --git a/mkdir/mkdir.go b/mkdir/mkdir.go index e18f587..847f167 100644 --- a/mkdir/mkdir.go +++ b/mkdir/mkdir.go @@ -1,44 +1,43 @@ package mkdir import( + "fmt" "os" "flag" - "log" ) func Run(args []string) int { + status := 0 + arg0 := args[0] + args = args[1:] var ( - parentFlag, verbFlag bool + parentFlag bool modeArg int ) - flagSet := flag.NewFlagSet(args[0], flag.ExitOnError) - status := 0 + flagSet := flag.NewFlagSet(arg0, flag.ExitOnError) flagSet.BoolVar(&parentFlag, "p", false, "No error if existing, make parent as needed.") flagSet.IntVar(&modeArg, "m", 0766, "Set file `mode`.") - flagSet.BoolVar(&verbFlag, "v", false, "Print a message for each created directory.") - if len(args) < 2 { + flagSet.Usage = func() { + fmt.Fprintf(os.Stderr, "Usage of %s: %s [options] [files]\n", arg0, arg0) + flagSet.PrintDefaults() + } + flagSet.Parse(args) + args = flagSet.Args() + if len(args) == 0 { flagSet.Usage() } - flagSet.Parse(args[1:]) mode := os.FileMode(modeArg) - var verb *log.Logger - if verbFlag { - verb = log.New(os.Stdout, flagSet.Args()[0]+": ", 0) - } - warn := log.New(os.Stderr, flagSet.Args()[0]+": ", 0) - for _, path := range flagSet.Args() { - var err error + for _, path := range args { + var e error if parentFlag { - err = os.MkdirAll(path, mode) + e = os.MkdirAll(path, mode) } else { - err = os.Mkdir(path, mode) + e = os.Mkdir(path, mode) } - if err != nil { - warn.Println(err) + if e != nil { + fmt.Fprintf(os.Stderr, "%s: %s.\n", arg0, e) status = 1 - } else if verbFlag { - verb.Printf("Created directory '%s'.", path) } } return status -} \ No newline at end of file +} diff --git a/tac/tac.go b/tac/tac.go index d3f9012..a3b335a 100644 --- a/tac/tac.go +++ b/tac/tac.go @@ -32,7 +32,7 @@ func tac(p string) error { func ftac(f *os.File) error { - r := bufio.NewReader(os.Stdin) + r := bufio.NewReader(f) var lines []string for { line, e := r.ReadString('\n') @@ -52,6 +52,10 @@ func Run(args []string) int { arg0 := args[0] args = args[1:] 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 {