The issue #81 is about implementing the -c option for jarvish. This is a common shell feature where -c allows passing a command string to the shell for execution.
Let me explore the codebase to understand the current structure, especially how the main entry point works and how commands are executed.
Assistant
コードベースの全体像を把握しました。次に、実装に必要な具体的なファイルを確認します。
Now I have a good understanding of the project structure. Let me look at the specific files I'll need to modify:
src/main.rs - to add the -c argument to the Args struct
src/shell/mod.rs - to add a method for non-interactive execution (running a single command string)
Now I have a clear picture. Let me create the implementation plan.
The -c option is a standard shell feature (like bash -c "command" or sh -c "command"). It takes a string argument and executes it as a command, then exits.
Key changes needed:
src/main.rs: Add -c option to the Args struct
src/shell/mod.rs: Add a new method run_command(&mut self, command: &str) -> i32 that:
Does NOT show the welcome banner
Does NOT enter the REPL loop
Executes the given command string via handle_input()
Returns the exit code
src/main.rs: Branch based on whether -c was provided
For the -c option, jarvish -c "ls -la" should execute that command and exit, jarvish -c "echo hello && echo world" should run both commands in sequence, and the exit code should reflect the last command's result. Since handle_input returns a boolean for continue/exit, I need to map that to an appropriate exit code.
The tricky part is that handle_input is async and lives on the Shell instance, which normally initializes the full interactive editor. For -c mode I don't really need the REPL, but creating a separate lightweight initialization would add complexity. I'll reuse Shell::new() and add a run_command method that executes the command string without entering the REPL loop. I should also consider how bash handles this—bash -c "command_string" executes the string, and you can optionally pass $0 and arguments after it.
For the implementation, I'll add the -c flag to clap as an optional string argument, then check for it in main and call the new run_command method if present. This should work alongside --debug and other flags without issues.
Adding the clap argument and handling it in main... I'm debating whether -c mode should handle multiple commands separated by newlines like bash does. Since our shell already processes operators like ;, &&, and ||, a single handle_input call with the full string should work for most cases. For simplicity, I'll start with that approach rather than splitting by newlines.
My implementation plan is straightforward: add the -c option to the argument parser in main.rs, branch on it, and add a run_command() method to the shell. I should also enable allow_hyphen_values for the -c argument since the command string might start with a hyphen, like jarvish -c "-v". I'm also considering how bash handles trailing arguments after -c—they become positional parameters in the script context.
Since jarvish doesn't support $0 and $1 yet, I'll just capture the command string for now and skip the trailing arguments.
Implement the plan as specified, it is attached for your reference. Do NOT edit the plan file itself.
To-do's from the plan have already been created. Do not create them again. Mark them as in_progress as you work, starting with the first one. Don't stop until you have completed all the to-dos.