The --prompt (or -p) option runs Qwen Code in non-interactive mode, executing a single prompt and exiting. This is ideal for automation, scripting, and CI/CD pipelines.
qwen --prompt "Create a Python function to calculate factorial"
Output:
def factorial(n): """Calculate the factorial of n.""" if n < 0: raise ValueError("Factorial is not defined for negative numbers") if n == 0 or n == 1: return 1 return n * factorial(n - 1)
# Read file and append promptcat mycode.py | qwen --prompt "Add type hints to this code"# Git diff reviewgit diff | qwen --prompt "Review these changes"# Error analysisnpm test 2>&1 | qwen --prompt "Explain these test failures"
The stdin content is automatically prepended to your prompt.
qwen --prompt "Build a React app" --output-format stream-json
Outputs newline-delimited JSON events:
{"type":"system_message","session_id":"abc123",...}{"type":"assistant_message_start",...}{"type":"content","value":"I'll help you build a React app..."}{"type":"tool_call_request","name":"write",...}{"type":"assistant_message_end",...}{"type":"result","isError":false,...}
# Continue previous sessionqwen --continue --prompt "Now add tests for that function"# Use specific sessionqwen --resume abc123 --prompt "Update the authentication"
# Plan only, no executionqwen --approval-mode plan --prompt "Refactor the codebase"# Auto-approve edits onlyqwen --approval-mode auto-edit --prompt "Update all imports"
#!/bin/bash# generate-tests.shfor file in src/**/*.ts; do if [[ ! -f "${file%.ts}.test.ts" ]]; then echo "Generating tests for $file" qwen --yolo --prompt "Create comprehensive unit tests for $file" fidone
#!/bin/bash# analyze-errors.sh# Run tests and capture errorsERRORS=$(npm test 2>&1 || true)# Analyze with AIecho "$ERRORS" | qwen --prompt "Analyze these test failures and suggest fixes" \ --output-format text
# Using heredocqwen --prompt "$(cat <<'EOF'Create a REST API with the following endpoints:- GET /users - List all users- POST /users - Create user- GET /users/:id - Get user- PUT /users/:id - Update user- DELETE /users/:id - Delete userUse Express.js and TypeScript.EOF)"
These features don’t work in headless mode:❌ Model selection dialog (/model)
❌ Authentication dialog (/auth)
❌ Session picker (--resume without ID)
❌ Manual approval prompts
❌ Terminal UI navigation