Enterprise CLI patterns using oclif framework with TypeScript...
Provides comprehensive patterns for building production-grade CLIs with oclif framework.
Use templates:
templates/command-basic.ts - Simple command with flagstemplates/command-advanced.ts - Complex command with validationtemplates/command-async.ts - Async operationstemplates/base-command.ts - Custom base classKey patterns:
Flags objectArgs objectCommon flags:
Flags.string({ description, required, default })Flags.boolean({ description, allowNo })Flags.integer({ description, min, max })Flags.custom<T>({ parse: async (input) => T })Flags.string({ multiple: true })Best practices:
Definition:
static args = {
name: Args.string({ description: 'Name', required: true }),
file: Args.file({ description: 'File path', exists: true })
}
Access in run():
const { args } = await this.parse(MyCommand)
Use templates:
templates/plugin-package.json - Plugin package.jsontemplates/plugin-command.ts - Plugin command structuretemplates/plugin-hooks.ts - Hook implementationsPlugin structure:
my-plugin/
├── package.json (oclif configuration)
├── src/
│ ├── commands/ (plugin commands)
│ └── hooks/ (lifecycle hooks)
├── test/ (plugin tests)
└── README.md
Use templates:
templates/test-command.ts - Command test templatetemplates/test-helpers.ts - Test utilitiestemplates/test-setup.ts - Test configurationTesting approach:
Generated automatically:
--help flagUse scripts:
scripts/generate-docs.sh - Generate all documentationscripts/update-readme.sh - Update README with commands# Use template
./scripts/create-command.sh my-command basic
# Results in: src/commands/my-command.ts
# Use template
./scripts/create-plugin.sh my-plugin
# Results in: plugin directory structure
# Use test helpers
npm test
# or with coverage
npm run test:coverage
Available validators:
scripts/validate-command.sh - Check command structurescripts/validate-plugin.sh - Verify plugin structurescripts/validate-tests.sh - Ensure test coveragecommand-basic.ts - Simple command patterncommand-advanced.ts - Full-featured commandcommand-async.ts - Async/await patternsbase-command.ts - Custom base classcommand-with-config.ts - Configuration managementplugin-package.json - Plugin package.jsonplugin-command.ts - Plugin commandplugin-hooks.ts - Hook implementationsplugin-manifest.json - Plugin manifesttest-command.ts - Command unit testtest-helpers.ts - Test utilitiestest-setup.ts - Test configurationtest-integration.ts - Integration testtsconfig.json - TypeScript configpackage.json - oclif package.json.eslintrc.json - ESLint configSee examples/ for complete working examples:
examples/basic-cli/ - Simple CLI with commandsexamples/plugin-cli/ - CLI with plugin supportexamples/enterprise-cli/ - Full enterprise setupif (!valid) {
this.error('Invalid input', { exit: 1 })
}
const spinner = ux.action.start('Processing')
// ... work
ux.action.stop()
const answer = await ux.prompt('Continue?')
ux.table(data, { columns: [...] })
Create reusable custom flag parsers for complex validation.
Implement hooks for: init, prerun, postrun, command_not_found.
Organize commands into topics (e.g., mycli topic:command).
Use @oclif/plugin-update for automatic CLI updates.
Integrate analytics to track command usage.