|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/arran4/strings2" |
| 10 | +) |
| 11 | + |
| 12 | +func process(input string, output string, args []string, opts []any, 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), opts...) |
| 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 | + |
| 58 | +func buildOpts(delimiter string, screaming bool, whispering bool, firstUpper bool, firstLower bool, mixCaseSupport bool, noSmartAcronyms bool, numberSplitting bool, strict bool) []any { |
| 59 | + var opts []any |
| 60 | + if delimiter != "" { |
| 61 | + opts = append(opts, strings2.OptionDelimiter(delimiter)) |
| 62 | + } |
| 63 | + if screaming { |
| 64 | + opts = append(opts, strings2.OptionCaseMode(strings2.CMScreaming)) |
| 65 | + } |
| 66 | + if whispering { |
| 67 | + opts = append(opts, strings2.OptionCaseMode(strings2.CMWhispering)) |
| 68 | + } |
| 69 | + if firstUpper { |
| 70 | + opts = append(opts, strings2.OptionFirstUpper()) |
| 71 | + } |
| 72 | + if firstLower { |
| 73 | + opts = append(opts, strings2.OptionFirstLower()) |
| 74 | + } |
| 75 | + if mixCaseSupport { |
| 76 | + opts = append(opts, strings2.OptionMixCaseSupport()) |
| 77 | + } |
| 78 | + if noSmartAcronyms { |
| 79 | + opts = append(opts, strings2.WithSmartAcronyms(false)) |
| 80 | + } |
| 81 | + if numberSplitting { |
| 82 | + opts = append(opts, strings2.WithNumberSplitting(true)) |
| 83 | + } |
| 84 | + if strict { |
| 85 | + opts = append(opts, strings2.OptionStrict()) |
| 86 | + } |
| 87 | + return opts |
| 88 | +} |
| 89 | + |
| 90 | +// Camel is a subcommand `strings2 camel` |
| 91 | +// |
| 92 | +// Flags: |
| 93 | +// |
| 94 | +// input: -i --input (default: "") Input file or - for stdin |
| 95 | +// output: -o --output (default: "") Output file or - for stdout |
| 96 | +// delimiter: -d --delimiter (default: "") Delimiter |
| 97 | +// screaming: -S --screaming (default: false) Screaming mode |
| 98 | +// whispering: -w --whispering (default: false) Whispering mode |
| 99 | +// firstUpper: -U --first-upper (default: false) First char upper |
| 100 | +// firstLower: -l --first-lower (default: false) First char lower |
| 101 | +// mixCaseSupport: -m --mix-case-support (default: false) Mix case support |
| 102 | +// noSmartAcronyms: --no-smart-acronyms (default: false) Disable smart acronyms |
| 103 | +// numberSplitting: --number-splitting (default: false) Enable number splitting |
| 104 | +// strict: --strict (default: false) Strict UTF8 mode |
| 105 | +// args: ... String to convert if file/stdin not provided |
| 106 | +func Camel(input string, output string, delimiter string, screaming bool, whispering bool, firstUpper bool, firstLower bool, mixCaseSupport bool, noSmartAcronyms bool, numberSplitting bool, strict bool, args ...string) { |
| 107 | + opts := buildOpts(delimiter, screaming, whispering, firstUpper, firstLower, mixCaseSupport, noSmartAcronyms, numberSplitting, strict) |
| 108 | + process(input, output, args, opts, strings2.ToCamel) |
| 109 | +} |
| 110 | + |
| 111 | +// Snake is a subcommand `strings2 snake` |
| 112 | +// |
| 113 | +// Flags: |
| 114 | +// |
| 115 | +// input: -i --input (default: "") Input file or - for stdin |
| 116 | +// output: -o --output (default: "") Output file or - for stdout |
| 117 | +// delimiter: -d --delimiter (default: "") Delimiter |
| 118 | +// screaming: -S --screaming (default: false) Screaming mode |
| 119 | +// whispering: -w --whispering (default: false) Whispering mode |
| 120 | +// firstUpper: -U --first-upper (default: false) First char upper |
| 121 | +// firstLower: -l --first-lower (default: false) First char lower |
| 122 | +// mixCaseSupport: -m --mix-case-support (default: false) Mix case support |
| 123 | +// noSmartAcronyms: --no-smart-acronyms (default: false) Disable smart acronyms |
| 124 | +// numberSplitting: --number-splitting (default: false) Enable number splitting |
| 125 | +// strict: --strict (default: false) Strict UTF8 mode |
| 126 | +// args: ... String to convert if file/stdin not provided |
| 127 | +func Snake(input string, output string, delimiter string, screaming bool, whispering bool, firstUpper bool, firstLower bool, mixCaseSupport bool, noSmartAcronyms bool, numberSplitting bool, strict bool, args ...string) { |
| 128 | + opts := buildOpts(delimiter, screaming, whispering, firstUpper, firstLower, mixCaseSupport, noSmartAcronyms, numberSplitting, strict) |
| 129 | + process(input, output, args, opts, strings2.ToSnake) |
| 130 | +} |
| 131 | + |
| 132 | +// Kebab is a subcommand `strings2 kebab` |
| 133 | +// |
| 134 | +// Flags: |
| 135 | +// |
| 136 | +// input: -i --input (default: "") Input file or - for stdin |
| 137 | +// output: -o --output (default: "") Output file or - for stdout |
| 138 | +// delimiter: -d --delimiter (default: "") Delimiter |
| 139 | +// screaming: -S --screaming (default: false) Screaming mode |
| 140 | +// whispering: -w --whispering (default: false) Whispering mode |
| 141 | +// firstUpper: -U --first-upper (default: false) First char upper |
| 142 | +// firstLower: -l --first-lower (default: false) First char lower |
| 143 | +// mixCaseSupport: -m --mix-case-support (default: false) Mix case support |
| 144 | +// noSmartAcronyms: --no-smart-acronyms (default: false) Disable smart acronyms |
| 145 | +// numberSplitting: --number-splitting (default: false) Enable number splitting |
| 146 | +// strict: --strict (default: false) Strict UTF8 mode |
| 147 | +// args: ... String to convert if file/stdin not provided |
| 148 | +func Kebab(input string, output string, delimiter string, screaming bool, whispering bool, firstUpper bool, firstLower bool, mixCaseSupport bool, noSmartAcronyms bool, numberSplitting bool, strict bool, args ...string) { |
| 149 | + opts := buildOpts(delimiter, screaming, whispering, firstUpper, firstLower, mixCaseSupport, noSmartAcronyms, numberSplitting, strict) |
| 150 | + process(input, output, args, opts, strings2.ToKebab) |
| 151 | +} |
| 152 | + |
| 153 | +// Pascal is a subcommand `strings2 pascal` |
| 154 | +// |
| 155 | +// Flags: |
| 156 | +// |
| 157 | +// input: -i --input (default: "") Input file or - for stdin |
| 158 | +// output: -o --output (default: "") Output file or - for stdout |
| 159 | +// delimiter: -d --delimiter (default: "") Delimiter |
| 160 | +// screaming: -S --screaming (default: false) Screaming mode |
| 161 | +// whispering: -w --whispering (default: false) Whispering mode |
| 162 | +// firstUpper: -U --first-upper (default: false) First char upper |
| 163 | +// firstLower: -l --first-lower (default: false) First char lower |
| 164 | +// mixCaseSupport: -m --mix-case-support (default: false) Mix case support |
| 165 | +// noSmartAcronyms: --no-smart-acronyms (default: false) Disable smart acronyms |
| 166 | +// numberSplitting: --number-splitting (default: false) Enable number splitting |
| 167 | +// strict: --strict (default: false) Strict UTF8 mode |
| 168 | +// args: ... String to convert if file/stdin not provided |
| 169 | +func Pascal(input string, output string, delimiter string, screaming bool, whispering bool, firstUpper bool, firstLower bool, mixCaseSupport bool, noSmartAcronyms bool, numberSplitting bool, strict bool, args ...string) { |
| 170 | + opts := buildOpts(delimiter, screaming, whispering, firstUpper, firstLower, mixCaseSupport, noSmartAcronyms, numberSplitting, strict) |
| 171 | + process(input, output, args, opts, strings2.ToPascal) |
| 172 | +} |
0 commit comments