oclif: The Open CLI Framework
  • Getting Started
  • API Reference
  • Blog
  • GitHub

›API Reference

Getting Started

  • Introduction
  • Features
  • FAQs
  • Generator Commands

API Reference

  • Commands
  • Command Arguments
  • Command Flags
  • Configuration
  • Topics
  • Topic Separators
  • Hooks
  • Plugins
  • Help Classes
  • Error Handling
  • JSON

How to

  • Release
  • Testing
  • Running Commands Programmatically
  • Aliases
  • Custom Base Class
  • Prompting
  • Spinner
  • Table
  • Notifications
  • Debugging
  • Flexible Taxonomy
  • Global Flags

Also See

  • Examples
  • External Links
  • Related Repositories
  • How We Work
  • Feedback
Edit

JSON

If you want to use the --json flag to return JSON output to the user, then you can set the enableJsonFlag property on the Command class.

When this property is set and the user supplies the --json flag, the command will supress all console logs and instead log the return value to the console in JSON format

import {Command} from '@oclif/core'
export class HelloCommand extends Command {
  private static enableJsonFlag = true
  public async run(): Promise<{ message: string }> {
    console.log('hello, world!')
    return { message: 'hello, world!' }
  }
}

$ my-cli hello
hello, world!
$ my-cli hello --json
{
  "message": "hello, world!"
}
Last updated on 10/27/2021
← Error HandlingRelease →
Made with 💜 by Salesforce — MIT License