cli: Add -I (import)
This commit is contained in:
parent
f58ff1785a
commit
9eaa71fe2d
50
bin/shrimp
50
bin/shrimp
|
|
@ -12,16 +12,21 @@ import { join } from 'path'
|
|||
function showHelp() {
|
||||
console.log(`${colors.bright}${colors.magenta}🦐 Shrimp${colors.reset} is a scripting language in a shell.
|
||||
|
||||
${colors.bright}Usage:${colors.reset} shrimp <command> [...args]
|
||||
${colors.bright}Usage:${colors.reset} shrimp <command> [options] [...args]
|
||||
|
||||
${colors.bright}Commands:${colors.reset}
|
||||
${colors.cyan}run ${colors.yellow}./my-file.sh${colors.reset} Execute a file with Shrimp
|
||||
${colors.cyan}parse ${colors.yellow}./my-file.sh${colors.reset} Print parse tree for Shrimp file
|
||||
${colors.cyan}bytecode ${colors.yellow}./my-file.sh${colors.reset} Print bytecode for Shrimp file
|
||||
${colors.cyan}eval ${colors.yellow}'some code'${colors.reset} Evaluate a line of Shrimp code
|
||||
${colors.cyan}eval ${colors.yellow}'some code'${colors.reset} Evaluate a line of Shrimp code
|
||||
${colors.cyan}repl${colors.reset} Start REPL
|
||||
${colors.cyan}help${colors.reset} Print this help message
|
||||
${colors.cyan}version${colors.reset} Print version`)
|
||||
${colors.cyan}version${colors.reset} Print version
|
||||
|
||||
${colors.bright}Options:${colors.reset}
|
||||
${colors.cyan}eval -I${colors.reset} ${colors.yellow}<module>${colors.reset} Import module (can be repeated)
|
||||
Example: shrimp -I math -e 'random | echo'
|
||||
Example: shrimp -Imath -Istr -e 'random | echo'`)
|
||||
}
|
||||
|
||||
function showVersion() {
|
||||
|
|
@ -29,7 +34,40 @@ function showVersion() {
|
|||
}
|
||||
|
||||
async function main() {
|
||||
const args = process.argv.slice(2)
|
||||
let args = process.argv.slice(2)
|
||||
|
||||
if (args.length === 0) {
|
||||
showHelp()
|
||||
return
|
||||
}
|
||||
|
||||
// Parse -I flags for imports (supports both "-I math" and "-Imath")
|
||||
const imports: string[] = []
|
||||
|
||||
while (args.length > 0) {
|
||||
const arg = args[0]
|
||||
|
||||
if (arg === '-I') {
|
||||
// "-I math" format
|
||||
if (args.length < 2) {
|
||||
console.log(`${colors.bright}error: -I requires a module name${colors.reset}`)
|
||||
process.exit(1)
|
||||
}
|
||||
imports.push(args[1])
|
||||
args = args.slice(2)
|
||||
} else if (arg.startsWith('-I')) {
|
||||
// "-Imath" format
|
||||
const moduleName = arg.slice(2)
|
||||
if (!moduleName) {
|
||||
console.log(`${colors.bright}error: -I requires a module name${colors.reset}`)
|
||||
process.exit(1)
|
||||
}
|
||||
imports.push(moduleName)
|
||||
args = args.slice(1)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if (args.length === 0) {
|
||||
showHelp()
|
||||
|
|
@ -63,6 +101,10 @@ async function main() {
|
|||
process.exit(1)
|
||||
}
|
||||
|
||||
// Prepend import statement if -I flags were provided
|
||||
const importStatement = imports.length > 0 ? `import ${imports.join(' ')}` : ''
|
||||
const fullCode = importStatement ? `${importStatement}; ${code}` : code
|
||||
|
||||
try { mkdirSync('/tmp/shrimp') } catch { }
|
||||
const path = `/tmp/shrimp/${randomUUID()}.sh`
|
||||
writeFileSync(path, fullCode)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user