Replies: 2 comments
-
|
Thanks for the suggestion! This is an interesting use case. I believe this is already achievable with the current API. The For example, you can inject the basename as a synthetic subcommand: import path from "node:path";
import { run } from "@optique/run";
import { command, or } from "@optique/core/parser";
const invokedName = path.basename(process.argv[1]);
const parser = or(
command("cat", catParser),
command("ls", lsParser),
command("grep", grepParser),
);
await run(parser, {
programName: invokedName,
args: [invokedName, ...process.argv.slice(2)],
});Since Alternatively, you can simply select a parser based on the program name without using const parsers: Record<string, Parser<"sync", unknown, unknown>> = {
cat: catParser,
ls: lsParser,
};
await run(parsers[invokedName] ?? defaultParser, {
programName: invokedName,
});Do you think this covers your use case, or is there something more you'd need from the library? |
Beta Was this translation helpful? Give feedback.
-
|
Ahh that is an interesting solution! I'll be sure to give this a shot and report back if I run into any issues. Thanks for your work and help on this issue! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Use case:
In some circumstances, it's extremely useful to change a program's behavior based on the name it is invoked with.
A common example is something like BusyBox, where it is in fact a single binary that is symlinked to, often multiple times (like
ls -> busybox,cp -> busybox, etc) . It then changes its behavior based off the name of the program (the symlink).It would be useful for optique to support something like this, even if it's just exposing the name of the program. Something more advanced would be something like a
command, where a specific set of program names are supported, and they take their own subset of options, and possibly their own help text.Beta Was this translation helpful? Give feedback.
All reactions