yes: Simple implementation added.
This commit is contained in:
parent
6eba399613
commit
722947a2cd
3 changed files with 60 additions and 2 deletions
|
@ -12,6 +12,7 @@ import(
|
|||
"github.com/jienfak/goblin/sort"
|
||||
"github.com/jienfak/goblin/tac"
|
||||
"github.com/jienfak/goblin/ls"
|
||||
"github.com/jienfak/goblin/yes"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -30,6 +31,7 @@ func main() {
|
|||
"sort" : sort.Run,
|
||||
"tac" : tac.Run,
|
||||
"ls" : ls.Run,
|
||||
"yes" : yes.Run,
|
||||
}
|
||||
|
||||
if binBase := path.Base(os.Args[0]) ; binBase != "goblin" {
|
||||
|
|
|
@ -9,7 +9,7 @@ import (
|
|||
"flag"
|
||||
)
|
||||
|
||||
func readLines() []string {
|
||||
func ReadLines() []string {
|
||||
|
||||
r := bufio.NewReader(os.Stdin)
|
||||
a := make([]string, 0)
|
||||
|
@ -28,7 +28,7 @@ func Run(args []string) int {
|
|||
flagSet.Parse(args[1:])
|
||||
status := 0
|
||||
|
||||
lines := readLines()
|
||||
lines := ReadLines()
|
||||
sort.Strings(lines)
|
||||
for _, l := range lines {
|
||||
fmt.Print(l)
|
||||
|
|
56
yes/yes.go
Normal file
56
yes/yes.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
/* Yes program implementation. */
|
||||
package yes
|
||||
|
||||
import(
|
||||
"os"
|
||||
"fmt"
|
||||
"flag"
|
||||
"strings"
|
||||
"github.com/jienfak/goblin/sort"
|
||||
)
|
||||
|
||||
func yes(s string){
|
||||
for{
|
||||
fmt.Print(s)
|
||||
}
|
||||
}
|
||||
|
||||
func Run(args []string) int {
|
||||
var(
|
||||
stdinFlag bool
|
||||
nFlag bool
|
||||
s string
|
||||
)
|
||||
status := 0
|
||||
arg0 := args[0]
|
||||
args = args[1:]
|
||||
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
|
||||
flagSet.BoolVar(&stdinFlag, "s", false, "Read string from stdin.")
|
||||
flagSet.BoolVar(&nFlag, "n", false, "Do not add net line character.")
|
||||
flagSet.Usage = func() {
|
||||
fmt.Fprintf(os.Stderr, "Usage of %s: %s [options] [string]\n", arg0, arg0)
|
||||
flagSet.PrintDefaults()
|
||||
|
||||
}
|
||||
flagSet.Parse(args)
|
||||
args = flagSet.Args()
|
||||
|
||||
if stdinFlag {
|
||||
in := sort.ReadLines()
|
||||
s = strings.Join(in, "")
|
||||
} else {
|
||||
if len(args)>0 {
|
||||
s = strings.Join(args, " ")
|
||||
} else {
|
||||
s = "y"
|
||||
}
|
||||
}
|
||||
|
||||
if !nFlag {
|
||||
s += "\n"
|
||||
}
|
||||
|
||||
yes(s)
|
||||
|
||||
return status
|
||||
}
|
Loading…
Reference in a new issue