From 6628b533c3925f7cbdee7fc33aa73049a4535157 Mon Sep 17 00:00:00 2001 From: k1574 Date: Fri, 17 Jun 2022 03:53:25 +0500 Subject: [PATCH] read: basic implementation. --- m/cmd/goblin/main.go | 2 ++ m/sep/read/main.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 m/sep/read/main.go diff --git a/m/cmd/goblin/main.go b/m/cmd/goblin/main.go index 56cc954..5590c17 100644 --- a/m/cmd/goblin/main.go +++ b/m/cmd/goblin/main.go @@ -19,6 +19,7 @@ import( "github.com/k1574/goblin/m/sep/mergelbl" "github.com/k1574/goblin/m/sep/basename" "github.com/k1574/goblin/m/sep/ec" + "github.com/k1574/goblin/m/sep/read" ) func main() { @@ -40,6 +41,7 @@ func main() { "noext" : noext.Run, "mergelbl" : mergelbl.Run, "ec" : ec.Run, + "read" : read.Run, } multitool.Main("goblin", tools) diff --git a/m/sep/read/main.go b/m/sep/read/main.go new file mode 100644 index 0000000..6955268 --- /dev/null +++ b/m/sep/read/main.go @@ -0,0 +1,40 @@ +package read +/* Plan9 read or something to read into variables. */ +import( + "os" + "bufio" + "flag" + "fmt" +) + +var( + nLines int +) + +func Run(args []string) { + arg0 := args[0] + args = args[1:] + flagSet := flag.NewFlagSet(arg0, flag.ExitOnError) + flagSet.IntVar(&nLines, "n", 1, "amount of lines") + flagSet.Usage = func() { + fmt.Fprintf(os.Stderr, "usage: %s [options] [files]\n", arg0) + flagSet.PrintDefaults() + os.Exit(1) + } + flagSet.Parse(args) + args = flagSet.Args() + + if nLines <= 0 { + flagSet.Usage() + } + + rd := bufio.NewReader(os.Stdin) + for nLines != 0 { + line, err := rd.ReadString('\n') + if err != nil { + os.Exit(0) + } + fmt.Print(line) + nLines -= 1 + } +}