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

›API Reference

Getting Started

  • Introduction
  • Features
  • FAQs
  • Generator Commands
  • Command Execution

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
  • Single Command CLI
  • ESM

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 suppress all logs and instead log the return value to the console in JSON format. Note log suppression will only work if you use the logging methods on the Command class instance. In other words, this.log will be automatically suppressed but console.log will not be.

import {Command} from '@oclif/core'
export class HelloCommand extends Command {
  public 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 1/26/2023
← Error HandlingRelease →
Made with 💜 by Salesforce — MIT License