Skip to content

Commit 64188d9

Browse files
Merge pull request #707 from MervinPraison/claude/issue-67-20250628_070140
feat: add --file/-f flag for file input support
2 parents fe29aea + 179ad5f commit 64188d9

File tree

1 file changed

+48
-9
lines changed

1 file changed

+48
-9
lines changed

src/praisonai/praisonai/cli.py

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,25 @@ def read_stdin_if_available(self):
170170
pass
171171
return None
172172

173+
def read_file_if_provided(self, file_path):
174+
"""
175+
Read content from a file if the file path is provided.
176+
Returns the file content or None if file cannot be read.
177+
"""
178+
if not file_path:
179+
return None
180+
181+
try:
182+
with open(file_path, 'r', encoding='utf-8') as f:
183+
file_content = f.read().strip()
184+
return file_content if file_content else None
185+
except FileNotFoundError:
186+
print(f"[red]ERROR: File not found: {file_path}[/red]")
187+
sys.exit(1)
188+
except Exception as e:
189+
print(f"[red]ERROR: Failed to read file: {e}[/red]")
190+
sys.exit(1)
191+
173192
def main(self):
174193
"""
175194
The main function of the PraisonAI object. It parses the command-line arguments,
@@ -189,15 +208,24 @@ def main(self):
189208

190209
# Check for piped input from stdin
191210
stdin_input = self.read_stdin_if_available()
211+
212+
# Check for file input if --file is provided
213+
file_input = self.read_file_if_provided(getattr(args, 'file', None))
192214

193215
if args.command:
194216
if args.command.startswith("tests.test") or args.command.startswith("tests/test"): # Argument used for testing purposes
195217
print("test")
196218
return "test"
197219
else:
198-
# If stdin input is available, append it to the command
220+
# Combine command with any available inputs (stdin and/or file)
221+
combined_inputs = []
199222
if stdin_input:
200-
combined_prompt = f"{args.command} {stdin_input}"
223+
combined_inputs.append(stdin_input)
224+
if file_input:
225+
combined_inputs.append(file_input)
226+
227+
if combined_inputs:
228+
combined_prompt = f"{args.command} {' '.join(combined_inputs)}"
201229
result = self.handle_direct_prompt(combined_prompt)
202230
print(result)
203231
return result
@@ -206,20 +234,30 @@ def main(self):
206234
elif hasattr(args, 'direct_prompt') and args.direct_prompt:
207235
# Only handle direct prompt if agent_file wasn't explicitly set in constructor
208236
if original_agent_file == "agents.yaml": # Default value, so safe to use direct prompt
209-
# If stdin input is available, append it to the direct prompt
210-
prompt = args.direct_prompt
237+
# Combine direct prompt with any available inputs (stdin and/or file)
238+
prompt_parts = [args.direct_prompt]
211239
if stdin_input:
212-
prompt = f"{args.direct_prompt} {stdin_input}"
240+
prompt_parts.append(stdin_input)
241+
if file_input:
242+
prompt_parts.append(file_input)
243+
prompt = ' '.join(prompt_parts)
213244
result = self.handle_direct_prompt(prompt)
214245
print(result)
215246
return result
216247
else:
217248
# Agent file was explicitly set, ignore direct prompt and use the file
218249
pass
219-
elif stdin_input:
220-
# If only stdin input is provided (no command), use it as direct prompt
221-
if original_agent_file == "agents.yaml": # Default value, so safe to use stdin as prompt
222-
result = self.handle_direct_prompt(stdin_input)
250+
elif stdin_input or file_input:
251+
# If only stdin/file input is provided (no command), use it as direct prompt
252+
if original_agent_file == "agents.yaml": # Default value, so safe to use input as prompt
253+
# Combine any available inputs
254+
inputs = []
255+
if stdin_input:
256+
inputs.append(stdin_input)
257+
if file_input:
258+
inputs.append(file_input)
259+
combined_input = ' '.join(inputs)
260+
result = self.handle_direct_prompt(combined_input)
223261
print(result)
224262
return result
225263
# If no command or direct_prompt, preserve agent_file from constructor (don't overwrite)
@@ -530,6 +568,7 @@ def parse_args(self):
530568
parser.add_argument("--public", action="store_true", help="Use ngrok to expose the server publicly (only with --call)")
531569
parser.add_argument("--merge", action="store_true", help="Merge existing agents.yaml with auto-generated agents instead of overwriting")
532570
parser.add_argument("--claudecode", action="store_true", help="Enable Claude Code integration for file modifications and coding tasks")
571+
parser.add_argument("--file", "-f", type=str, help="Read input from a file and append it to the prompt")
533572

534573
# If we're in a test environment, parse with empty args to avoid pytest interference
535574
if in_test_env:

0 commit comments

Comments
 (0)