--help and --version, and you can add custom ones.
Built-in Global Flags
Bunli automatically provides these flags:packages/core/src/global-flags.ts:7-18
How Global Flags Work
Global flags are merged with command-specific options:packages/core/src/cli.ts:505-506
Built-in Global Flags Definition
Here’s how Bunli defines global flags internally:packages/core/src/global-flags.ts
Help Flag Behavior
The--help flag shows contextual help:
packages/core/src/cli.ts:686-703
Version Flag Behavior
The--version flag displays your CLI version:
packages/core/src/cli.ts:681-684
Adding Custom Global Flags
To add your own global flags, use a plugin:Global Flags with Plugin Middleware
Use plugin middleware to handle global behavior:Practical Example: Debug Mode
Add a global debug flag:Accessing Global Flags
Global flags appear alongside command options:Flag Priority
Command options override global flags with the same name:packages/core/src/cli.ts:505-506
Environment-Based Defaults
Set global flag defaults from environment variables:Testing Global Flags
Best Practices
Keep global flags minimal
Keep global flags minimal
Only add flags that truly apply to every command. Most options should be command-specific.
Use short aliases wisely
Use short aliases wisely
Reserve single-letter aliases (
-v, -h) for the most common flags.Document global flags
Document global flags
Make sure users know about global flags by including them in your CLI’s main help output.
Avoid conflicts
Avoid conflicts
Check that global flag names don’t conflict with common command option names.
Use plugins for complex behavior
Use plugins for complex behavior
For global flags that need logic (like debug mode), implement them with plugin middleware.
Next Steps
Defining Commands
Learn how to create commands that use global flags
Working with Options
Master all types of command options
Plugins
Use plugins to implement global behavior
Building Binaries
Compile your CLI with global flags