mkdir: Simplified; usage function changed.

cat: usage function changed.
tac: Fixed incorrect "ftac" function realization; usage function changed.
This commit is contained in:
jienfak 2020-03-07 16:14:40 +05:00
parent 4e41cf7aa6
commit 9ee2ce65b4
3 changed files with 29 additions and 22 deletions

View file

@ -38,6 +38,10 @@ func Run(args []string) int {
arg0 := args[0] arg0 := args[0]
args = args[1:] args = args[1:]
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError) 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) flagSet.Parse(args)
args = flagSet.Args() args = flagSet.Args()
if len(args)>0 { if len(args)>0 {

View file

@ -1,44 +1,43 @@
package mkdir package mkdir
import( import(
"fmt"
"os" "os"
"flag" "flag"
"log"
) )
func Run(args []string) int { func Run(args []string) int {
status := 0
arg0 := args[0]
args = args[1:]
var ( var (
parentFlag, verbFlag bool parentFlag bool
modeArg int modeArg int
) )
flagSet := flag.NewFlagSet(args[0], flag.ExitOnError) flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
status := 0
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.BoolVar(&verbFlag, "v", false, "Print a message for each created directory.") flagSet.Usage = func() {
if len(args) < 2 { 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.Usage()
} }
flagSet.Parse(args[1:])
mode := os.FileMode(modeArg) mode := os.FileMode(modeArg)
var verb *log.Logger for _, path := range args {
if verbFlag { var e error
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
if parentFlag { if parentFlag {
err = os.MkdirAll(path, mode) e = os.MkdirAll(path, mode)
} else { } else {
err = os.Mkdir(path, mode) e = os.Mkdir(path, mode)
} }
if err != nil { if e != nil {
warn.Println(err) fmt.Fprintf(os.Stderr, "%s: %s.\n", arg0, e)
status = 1 status = 1
} else if verbFlag {
verb.Printf("Created directory '%s'.", path)
} }
} }
return status return status
} }

View file

@ -32,7 +32,7 @@ func tac(p string) error {
func ftac(f *os.File) error { func ftac(f *os.File) error {
r := bufio.NewReader(os.Stdin) r := bufio.NewReader(f)
var lines []string var lines []string
for { for {
line, e := r.ReadString('\n') line, e := r.ReadString('\n')
@ -52,6 +52,10 @@ func Run(args []string) int {
arg0 := args[0] arg0 := args[0]
args = args[1:] args = args[1:]
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError) 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) flagSet.Parse(args)
args = flagSet.Args() args = flagSet.Args()
if len(args)>0 { if len(args)>0 {