最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

tcl - How can I automate the action of terminal tools like Claude Code? - Stack Overflow

programmeradmin0浏览0评论

Basically what I am trying to do is to make a shell script file that can make a single command call for Claude Code. Since it requires user's confirmation for sensitive actions like file modification/creation/deletion, I wish to make it auto-accept all actions.

Based on the official documentation, such an action cannot be made automatic accepted via configuration, so I am trying to make the script automatically enter ENTER (a new line) when it detects the options. It seems to be able to detect the options when they appears, but sending ENTER does not trigger the selection.

│ ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
│ Do you want to create user.test.js?                                                                                                                                                                                                 │
│ ❯ Yes                                                                                                                                                                                                                               │
│   Yes, and don't ask again this session                                                                                                                                                                                             │
│   No, and tell Claude what to do differently (esc)                                                                                                                                                                                  │
│                                                                                                                                                                                                                                     │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

This is the message I detected, and I am supposed to press enter here.

#!/usr/bin/env expect
set timeout -1

# Function to handle interactive prompts
proc handle_prompts {} {
    set prev_lines [list "" "" "" "" "" "" "" "" "" ""]
    expect {
        -re ".*\\S.*" {
            set last_line $expect_out(buffer)
            lappend prev_lines $last_line
            set prev_lines [lrange $prev_lines end-9 end]
            set combined_lines [join $prev_lines "\n"]
            set file [open "last_detected_lines.txt" "w"]
            puts $file $combined_lines
            close $file
            if { $combined_lines ne "" && $combined_lines ne [join [lrange $prev_lines 0 8] "\n"] } {
                foreach line $prev_lines {
                    if {[regexp {.*❯.*Yes.*|.*Yes.*❯.*} $line]} {
                        # puts "Detected line: $line"
                        # Apparently the prompt is detected correctly, but the send command was not recognised as Enter by claude code.
                        # puts "Sending enter..."
                        send "\r\n"
                        break
                    }
                }
            }
            exp_continue
        }
        timeout {
            puts "Timeout waiting for prompt"
            exit 1
        }
        eof {
            set combined_lines [join $prev_lines "\n"]
            if { $combined_lines ne "" } {
                puts "Detected last lines: $combined_lines"
                set file [open "last_detected_lines.txt" "w"]
                puts $file $combined_lines
                close $file
            }
        }
    }
}

# First run claude init to ensure the environment is set up
# spawn claude init
# handle_prompts

# Now run the actual command
spawn claude --verbose "Write me some unit testing for services/user.js"
handle_prompts

The script I have right now is like this. I can see messages like Detected line: $line show up when the message above shows up, so I am sure the matching condition is working.

But the send "\r\n" wasn't triggering the option selection. Also tried send "\r" and send -- "\r", neither of them work. (As suggested by AI)

It is a terminal tool btw.

发布评论

评论列表(0)

  1. 暂无评论