Tech News
← Back to articles

CLI's completion should know what options you've typed

read original related products more articles

Consider Git's -C option:

git -C /path/to/repo checkout < TA B >

When you hit Tab , Git completes branch names from /path/to/repo , not your current directory. The completion is context-aware—it depends on the value of another option.

Most CLI parsers can't do this. They treat each option in isolation, so completion for --branch has no way of knowing the --repo value. You end up with two unpleasant choices: either show completions for all possible branches across all repositories (useless), or give up on completion entirely for these options.

Optique 0.10.0 introduces a dependency system that solves this problem while preserving full type safety.

Static dependencies with or()

Optique already handles certain kinds of dependent options via the or() combinator:

import { flag , object , option , or , string } from " @optique/core " ; const outputOptions = or ( object ({ json : flag ( " --json " ), pretty : flag ( " --pretty " ), }), object ({ csv : flag ( " --csv " ), delimiter : option ( " --delimiter " , string ()), }), );

TypeScript knows that if json is true , you'll have a pretty field, and if csv is true , you'll have a delimiter field. The parser enforces this at runtime, and shell completion will suggest --pretty only when --json is present.

This works well when the valid combinations are known at definition time. But it can't handle cases where valid values depend on runtime input—like branch names that vary by repository.

... continue reading