diff options
| author | Soccera1 <[email protected]> | 2026-07-18 09:27:17 +1000 |
|---|---|---|
| committer | Soccera1 <[email protected]> | 2026-07-18 09:27:17 +1000 |
| commit | 6f5241afb5bd1d6a1a8724bf06cc6f205c279402 (patch) | |
| tree | 2aa852e968f6b89f2c8458333ce3e7c38b3c2450 /src/commands/coreutils.js | |
| parent | a753f669c9bedfccb9f7f400da018bb487dca5d0 (diff) | |
| download | bnu-6f5241afb5bd1d6a1a8724bf06cc6f205c279402.tar.gz bnu-6f5241afb5bd1d6a1a8724bf06cc6f205c279402.zip | |
Refactor to single call
Diffstat (limited to 'src/commands/coreutils.js')
| -rw-r--r-- | src/commands/coreutils.js | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/commands/coreutils.js b/src/commands/coreutils.js new file mode 100644 index 0000000..b5083af --- /dev/null +++ b/src/commands/coreutils.js @@ -0,0 +1,42 @@ +#!/usr/bin/env bun + +import { commandNames, hasCommand } from "../shared/catalog.js"; +import { invalidOptionMessage } from "../shared/common.js"; +import { UsageError, VERSION, fail, stdout } from "../shared/diagnostics.js"; +import { main } from "../shared/runtime.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export async function coreutilsCmd(args) { + if (args[0]?.startsWith("--coreutils-prog=")) { + const program = args[0].slice("--coreutils-prog=".length); + if (!program || !hasCommand(program)) return fail("coreutils", `unknown program '${program}'`); + return main([program, ...args.slice(1)]); + } + if (args[0]?.startsWith("--coreutils-prog-shebang=")) { + const program = args[0].slice("--coreutils-prog-shebang=".length); + if (!program || !hasCommand(program)) return fail("coreutils", `unknown program '${program}'`); + if (args.length < 2) return fail("coreutils", `unknown program '${program}'`); + return main([program, ...args.slice(2)]); + } + if (!args.length) throw new UsageError("missing operand"); + if (args[0] === "--help") { + stdout("Usage: coreutils --coreutils-prog=PROGRAM_NAME [PARAMETERS]...\n"); + stdout("Execute the PROGRAM_NAME built-in program with the given PARAMETERS.\n\n"); + stdout(" --help display this help and exit\n"); + stdout(" --version output version information and exit\n\n"); + stdout(`Built-in programs:\n ${commandNames.join(" ")}\n\n`); + stdout("Use: 'coreutils --coreutils-prog=PROGRAM_NAME --help' for individual program help.\n"); + return 0; + } + if (args[0] === "--version") { + stdout(`${VERSION}\n`); + return 0; + } + if (args[0].startsWith("-")) throw new UsageError(invalidOptionMessage(args[0]), true); + return main(args); +} + +const singleCall = defineCommand("coreutils", coreutilsCmd, null); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); |
