Language Detection¶
run can automatically detect the programming language from code patterns or file extensions.
How It Works¶
Detection happens in this order:
- Explicit
--langflag (highest priority) - File extension (for file paths)
- Code pattern matching (for inline code)
- Default fallback (Python)
File Extension Detection¶
File extensions automatically determine the language:
No need to specify --lang!
Pattern Matching¶
Distinctive syntax is auto-detected:
Rust¶
Go¶
# package main declaration
run "package main; import \"fmt\"; func main() { fmt.Println(\"Hello\") }"
JavaScript¶
# console.log
run "console.log('Hello')"
# Node.js require
run "const fs = require('fs'); console.log('Hello')"
C/C++¶
Ambiguous Cases¶
Some syntax looks similar across languages:
Solution: Use --lang explicitly:
# Explicit
run --lang python "print('hello')"
run --lang ruby "print('hello')"
run --lang lua "print('hello')"
Detection Confidence¶
| Syntax | Confidence | Languages |
|---|---|---|
fn main() | High | Rust |
package main | High | Go |
console.log() | High | JavaScript |
println!() | High | Rust |
fmt.Println() | High | Go |
print() | Low | Python, Ruby, Lua, Perl |
puts | Medium | Ruby, C |
Best Practices¶
When to Use Auto-Detection¶
- File execution (extension available)
- Distinctive syntax
- Quick one-off commands
- Interactive exploration
When to Use --lang¶
- Ambiguous syntax
- Scripts and automation
- CI/CD pipelines
- When correctness is critical
Checking Detection¶
Test what language is detected: