Add ArgsOrIn

pull/53/head v0.0.29
rwxrob 2 years ago
parent 5b43af2346
commit aec7bee85b
No known key found for this signature in database
GPG Key ID: 2B9111F33082AE77

@ -30,6 +30,7 @@ import (
config "github.com/rwxrob/config/pkg"
"github.com/rwxrob/fn/maps"
"github.com/rwxrob/fs/file"
"github.com/rwxrob/term"
)
func init() {
@ -183,6 +184,17 @@ func ArgsFrom(line string) []string {
return args
}
// ArgsOrIn takes an slice or nil as argument and if the slice has any
// length greater than 0 returns all the argument joined together with
// a single space between them. Otherwise, will read standard input
// until end of file reached (Cntl-D).
func ArgsOrIn(args []string) string {
if args == nil || len(args) == 0 {
return term.Read()
}
return strings.Join(args, " ")
}
// Files returns a slice of strings matching the names of the files
// within the given directory adding a slash to the end of any
// directories and escaping any spaces by adding backslash. Note that

@ -19,6 +19,38 @@ func ExampleArgsFrom() {
// ["greet" "hi" "french" ""]
}
func ExampleArgsOrIn_read_Nil() {
orig := os.Stdin
defer func() { os.Stdin = orig }()
os.Stdin, _ = os.Open(`testdata/in`)
fmt.Println(bonzai.ArgsOrIn(nil))
// Output:
// some thing
}
func ExampleArgsOrIn_read_Zero_Args() {
orig := os.Stdin
defer func() { os.Stdin = orig }()
os.Stdin, _ = os.Open(`testdata/in`)
fmt.Println(bonzai.ArgsOrIn([]string{}))
// Output:
// some thing
}
func ExampleArgsOrIn_args_Joined() {
fmt.Println(bonzai.ArgsOrIn([]string{"some", "thing"}))
// Output:
// some thing
}
func ExampleFiles() {
each.Println(bonzai.Files("testdata/files"))
// Output:

4
testdata/in vendored

@ -0,0 +1,4 @@
some thing
input
with blank line
Loading…
Cancel
Save