@@ -2,63 +2,99 @@ package cli
22
33import (
44 "fmt"
5+ "io"
56 "os"
7+ "strings"
68
79 "github.com/arran4/strings2"
810)
911
12+ func process (input string , output string , args []string , fn func (string , ... any ) (string , error )) {
13+ var in io.Reader
14+ if input == "-" {
15+ in = os .Stdin
16+ } else if input != "" {
17+ f , err := os .Open (input )
18+ if err != nil {
19+ fmt .Fprintf (os .Stderr , "Error opening input file: %v\n " , err )
20+ os .Exit (1 )
21+ }
22+ defer f .Close ()
23+ in = f
24+ } else if len (args ) > 0 {
25+ in = strings .NewReader (strings .Join (args , " " ))
26+ } else {
27+ in = os .Stdin
28+ }
29+
30+ b , err := io .ReadAll (in )
31+ if err != nil {
32+ fmt .Fprintf (os .Stderr , "Error reading input: %v\n " , err )
33+ os .Exit (1 )
34+ }
35+
36+ res , err := fn (string (b ))
37+ if err != nil {
38+ fmt .Fprintf (os .Stderr , "Error processing: %v\n " , err )
39+ os .Exit (1 )
40+ }
41+
42+ var out io.Writer
43+ if output == "-" || output == "" {
44+ out = os .Stdout
45+ } else {
46+ f , err := os .Create (output )
47+ if err != nil {
48+ fmt .Fprintf (os .Stderr , "Error creating output file: %v\n " , err )
49+ os .Exit (1 )
50+ }
51+ defer f .Close ()
52+ out = f
53+ }
54+
55+ fmt .Fprintln (out , res )
56+ }
57+
1058// Camel is a subcommand `strings2 camel`
1159//
1260// Flags:
1361//
14- // input: @1 Input string
15- func Camel (input string ) {
16- res , err := strings2 .ToCamel (input )
17- if err != nil {
18- fmt .Fprintf (os .Stderr , "Error: %v\n " , err )
19- os .Exit (1 )
20- }
21- fmt .Println (res )
62+ // input: -i --input (default: "") Input file or - for stdin
63+ // output: -o --output (default: "") Output file or - for stdout
64+ // args: ... Positional arguments
65+ func Camel (input string , output string , args ... string ) {
66+ process (input , output , args , strings2 .ToCamel )
2267}
2368
2469// Snake is a subcommand `strings2 snake`
2570//
2671// Flags:
2772//
28- // input: @1 Input string
29- func Snake (input string ) {
30- res , err := strings2 .ToSnake (input )
31- if err != nil {
32- fmt .Fprintf (os .Stderr , "Error: %v\n " , err )
33- os .Exit (1 )
34- }
35- fmt .Println (res )
73+ // input: -i --input (default: "") Input file or - for stdin
74+ // output: -o --output (default: "") Output file or - for stdout
75+ // args: ... Positional arguments
76+ func Snake (input string , output string , args ... string ) {
77+ process (input , output , args , strings2 .ToSnake )
3678}
3779
3880// Kebab is a subcommand `strings2 kebab`
3981//
4082// Flags:
4183//
42- // input: @1 Input string
43- func Kebab (input string ) {
44- res , err := strings2 .ToKebab (input )
45- if err != nil {
46- fmt .Fprintf (os .Stderr , "Error: %v\n " , err )
47- os .Exit (1 )
48- }
49- fmt .Println (res )
84+ // input: -i --input (default: "") Input file or - for stdin
85+ // output: -o --output (default: "") Output file or - for stdout
86+ // args: ... Positional arguments
87+ func Kebab (input string , output string , args ... string ) {
88+ process (input , output , args , strings2 .ToKebab )
5089}
5190
5291// Pascal is a subcommand `strings2 pascal`
5392//
5493// Flags:
5594//
56- // input: @1 Input string
57- func Pascal (input string ) {
58- res , err := strings2 .ToPascal (input )
59- if err != nil {
60- fmt .Fprintf (os .Stderr , "Error: %v\n " , err )
61- os .Exit (1 )
62- }
63- fmt .Println (res )
95+ // input: -i --input (default: "") Input file or - for stdin
96+ // output: -o --output (default: "") Output file or - for stdout
97+ // args: ... Positional arguments
98+ func Pascal (input string , output string , args ... string ) {
99+ process (input , output , args , strings2 .ToPascal )
64100}
0 commit comments