Optique 0.6.0: Shell completion support for type-safe CLI parsers #34
dahlia
announced in
Announcements
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Optique 0.6.0 is here, bringing intelligent shell completion to your type-safe command-line applications. This release introduces built-in completion support for Bash, zsh, fish, PowerShell, and Nushell, making your CLIs more discoverable and user-friendly—all without sacrificing type safety or requiring duplicate definitions.
For those new to Optique: it's a TypeScript CLI parser library that takes a fundamentally different approach from traditional configuration-based parsers. Instead of describing your CLI with configuration objects, you compose parsers from small, type-safe functions. TypeScript automatically infers the exact types of your parsed data, ensuring compile-time safety while the parser structure itself provides runtime validation. Think of it as bringing the composability of parser combinators (inspired by Haskell's optparse-applicative) together with the type safety of TypeScript's type system.
Shell completion that just works
The standout feature of this release is comprehensive shell completion support. Unlike many CLI frameworks that require separate completion definitions, Optique's completion system leverages the same parser structure used for argument parsing. This means your completion suggestions automatically stay synchronized with your CLI's actual behavior—no duplicate definitions, no
manual maintenance.
Users can now press Tab to get intelligent suggestions:
Setting up completion is straightforward. Users generate a completion script for their shell and source it:
The completion system works automatically with all Optique parser types. When you use
choice()value parsers, the available options become completion suggestions. When you usepath()parsers, file system completion kicks in with proper handling of extensions and file types. Subcommands, options, and arguments all provide context-aware suggestions.What makes Optique's completion special is that it leverages the same parser structure used for argument parsing. Every parser has an optional
suggest()method that provides context-aware suggestions based on the current input. Parser combinators likeobject()andor()automatically aggregate suggestions from their constituent parsers, ensuring your completion logic stays in your TypeScript code where it benefits from type safety and testing.Optique handles the differences between shells transparently. Bash uses the
completecommand with proper handling of word splitting, zsh leverages its powerfulcompdefsystem with completion descriptions, fish provides tab-separated format with automatic file type detection, PowerShell usesRegister-ArgumentCompleterwith AST-based parsing, and Nushell integrates with its external completer system. For file and directory completions, Optique delegates to each shell's native file completion system, ensuring proper handling of spaces, symlinks, and platform-specific path conventions.Custom completion suggestions
For domain-specific value parsers, you can implement custom completion logic that provides intelligent suggestions based on your application's needs:
The built-in value parsers also provide intelligent suggestions. For instance, the
locale()parser suggests common locale identifiers, theurl()parser offers protocol completions when configured withallowedProtocols, and the timezone parsers from @optique/temporal useIntl.supportedValuesOf()for dynamic timezone suggestions.Enhanced command documentation
This release also introduces new documentation capabilities for the
command()parser. You can now provide separatebriefanddescriptiontexts, along with afooterfor examples and additional information:The
brieftext appears when listing commands (likemyapp help), whiledescriptionprovides detailed information when viewing command-specific help (myapp deploy --helpormyapp help deploy). Thefooterappears at the bottom of the help text, perfect for examples and additional resources.Command-line example formatting
To make help text and examples clearer, we've added a new
commandLine()message term type. This displays command-line snippets with distinct cyan coloring in terminals, making it immediately clear what users should type:These command examples stand out visually in help text, making it easier for users to understand how to use your CLI.
Migration guide
If you're already using Optique, adding completion support is straightforward:
completionoption to yourrun()configuration:That's it! Your CLI now supports shell completion. The
completionoption accepts three modes:"command": Only thecompletionsubcommand (e.g.,myapp completion bash)"option": Only the--completionoption (e.g.,myapp --completion bash)"both": Both patterns workFor custom value parsers, you can optionally add a
suggest()method to provide domain-specific completions. Existing parsers continue to work without modification—they just won't provide custom suggestions beyond what the parser structure implies.Looking forward
Shell completion has been one of the most requested features for Optique, and we're thrilled to deliver it in a way that maintains our core principles: type safety, composability, and zero duplication. Your parser definitions remain the single source of truth for both parsing and completion behavior.
This release represents a significant step toward making Optique-based CLIs as user-friendly as they are developer-friendly. The completion system proves that we can provide sophisticated runtime features without sacrificing the compile-time guarantees that make Optique unique.
We hope you find the new shell completion feature useful and look forward to seeing what you build with it!
Getting started
To start using Optique 0.6.0:
For complete documentation, visit optique.dev. Check out the new shell completion guide for detailed setup instructions and advanced usage patterns.
For bug reports and feature requests, please visit our GitHub repository.
All reactions