I've been working on a tricky problem in Optique (my CLI parser library): how do you make one option's value affect another option's validation and shell completion?
Think git -C <path> branch --delete <TAB>—the branch completions should come from the repo at <path>, not the current directory.
I think I've found a solution that fits naturally with Optique's architecture: declare dependencies between value parsers, then topologically sort them at parse time.
const cwdString = dependency(string());
const parser = object({
cwd: optional(option("-C", cwdString)),
branches: multiple(argument(
cwdString.derive({
metavar: "BRANCH",
factory: dir => gitBranch({ dir }),
defaultValue: () => process.cwd(),
})
)),
});Details in the issue:
https://github.com/dahlia/optique/issues/74#issuecomment-3738381049