83 lines
2.5 KiB
Plaintext
83 lines
2.5 KiB
Plaintext
// ny-echo - 最小CLI実装
|
||
// 目的: I/O・StringBoxの道通し確認
|
||
// 使用法: ny-echo [--upper|--lower]
|
||
|
||
static box Main {
|
||
init { console, options }
|
||
|
||
main(args) {
|
||
me.console = new ConsoleBox()
|
||
me.options = me.parseArgs(args)
|
||
|
||
// バージョン表示(グローバルNYASH_VERSION依存を避ける)
|
||
if me.options.get("version") {
|
||
local ver = "1.0.0"
|
||
me.console.log("ny-echo v" + ver)
|
||
return 0
|
||
}
|
||
|
||
// ヘルプ表示
|
||
if me.options.get("help") {
|
||
me.showHelp()
|
||
return 0
|
||
}
|
||
|
||
// メインループ
|
||
me.processInput()
|
||
return 0
|
||
}
|
||
|
||
parseArgs(args) {
|
||
local options = new MapBox()
|
||
// foreach は未対応のため、index で明示ループ
|
||
local i = 0
|
||
local n = args.length()
|
||
loop(i < n) {
|
||
local arg = args.get(i)
|
||
if arg == "--upper" {
|
||
options.set("upper", true)
|
||
} else if arg == "--lower" {
|
||
options.set("lower", true)
|
||
} else if arg == "--version" || arg == "-v" {
|
||
options.set("version", true)
|
||
} else if arg == "--help" || arg == "-h" {
|
||
options.set("help", true)
|
||
}
|
||
i = i + 1
|
||
}
|
||
|
||
return options
|
||
}
|
||
|
||
processInput() {
|
||
loop(true) {
|
||
local input = me.console.readLine()
|
||
if input == null { return 0 } // EOF → 関数終了に変更(break未対応MIR対策)
|
||
local output = me.transformText(input)
|
||
me.console.log(output)
|
||
}
|
||
}
|
||
|
||
transformText(text) {
|
||
if me.options.get("upper") {
|
||
return text.toUpperCase()
|
||
} else if me.options.get("lower") {
|
||
return text.toLowerCase()
|
||
} else {
|
||
return text // そのまま出力
|
||
}
|
||
}
|
||
|
||
showHelp() {
|
||
me.console.log("Usage: ny-echo [OPTIONS]")
|
||
me.console.log("")
|
||
me.console.log("Options:")
|
||
me.console.log(" --upper Convert input to uppercase")
|
||
me.console.log(" --lower Convert input to lowercase")
|
||
me.console.log(" -v, --version Show version information")
|
||
me.console.log(" -h, --help Show this help message")
|
||
me.console.log("")
|
||
me.console.log("Reads from stdin and echoes to stdout with optional transformation.")
|
||
}
|
||
}
|