DetectInputMode determines the input mode based on arguments and stdin state Returns: (useStdin bool, inputArg string, error)
(args []string)
| 157 | // DetectInputMode determines the input mode based on arguments and stdin state |
| 158 | // Returns: (useStdin bool, inputArg string, error) |
| 159 | func DetectInputMode(args []string) (bool, string, error) { |
| 160 | // Case 1: Explicit stdin via "-" |
| 161 | if len(args) > 0 && args[0] == "-" { |
| 162 | return true, "-", nil |
| 163 | } |
| 164 | |
| 165 | // Case 2: No arguments |
| 166 | if len(args) == 0 { |
| 167 | // Check if stdin is piped |
| 168 | if IsStdinPipe() { |
| 169 | return true, "-", nil |
| 170 | } |
| 171 | // No piped stdin and no args = error |
| 172 | return false, "", fmt.Errorf("no input provided") |
| 173 | } |
| 174 | |
| 175 | // Case 3: Arguments provided |
| 176 | // Always prefer explicit arguments over stdin |
| 177 | return false, args[0], nil |
| 178 | } |
| 179 | |
| 180 | // ReadInputWithFallback tries to read from the specified source with stdin fallback |
| 181 | // This provides a convenient way to handle both file and stdin inputs |