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

›API Reference

Getting Started

  • Introduction
  • Features
  • FAQs
  • Single-command CLI
  • Multi-command CLI
  • Generator Commands

API Reference

  • Commands
  • Command Arguments
  • Command Flags
  • Configuration
  • Topics
  • Hooks
  • Plugins

How to

  • Release
  • Testing
  • Running Commands Programmatically
  • Aliases
  • Custom Base Class
  • Prompting
  • Spinner
  • Table
  • Notifications
  • Debugging

Also See

  • Examples
  • External Links
  • Related Repositories
  • Feedback
Edit

Configuration

Inside a command, this.config provides useful properties you can use in your command. Here are a list of its methods and properties:

  • name - name of CLI
  • version - version of CLI
  • pjson - parsed and normalized CLI package.json
  • bin - CLI bin name
  • cacheDir - CLI cache directory
    • MacOS: ~/Library/Caches/mycli
    • Unix: ~/.cache/mycli
    • Windows: %LOCALAPPDATA%\mycli
    • Can be overridden with XDG_CACHE_HOME
  • configDir - CLI config directory
    • Unix: ~/.config/mycli
    • Windows: %LOCALAPPDATA%\mycli
    • Can be overridden with XDG_CONFIG_HOME
  • dataDir - CLI data directory
    • Unix: ~/.data/mycli
    • Windows: %LOCALAPPDATA%\mycli
    • Can be overridden with XDG_DATA_HOME
  • dirname - dirname used with cacheDir|configDir|dataDir. Can be overridden in package.json.
  • errlog - path to error log inside of cacheDir
  • home - user home directory
  • platform - operating system darwin|linux|win32
  • arch - process architecture x64|x86
  • shell - current shell in use
  • userAgent - user-agent intended for http calls. example: mycli/1.2.3 (darwin-x64) node-9.0.0
  • windows - boolean
  • debug - set to 1 if debug is enabled (with ${BIN}_DEBUG=1 or DEBUG=$BIN). In the future this may be used for multiple debug levels.
  • npmRegistry - current npm registry to use with the plugins plugin
  • plugins - loaded plugins
  • commands - all commands in CLI
  • topics - all topics in CLI
  • commandIDs - string IDs of all commands
  • async runHook(event, opts) - trigger a hook

Custom User Configuration

Often it's useful to have a custom configuration for your users. One way to implement this is to read a config.json file from the CLI's config directory:

import {Command} from '@oclif/command'
import * as fs from 'fs-extra'
import * as path from 'path'

export class extends Command {
  async run() {
    const userConfig = await fs.readJSON(path.join(this.config.configDir, 'config.json'))

    this.log('User config:')
    console.dir(userConfig)
  }
}

To share this logic between different commands, use a base class.

← PreviousNext →

Last updated: 2018-3-25 18:40:58

Made with 💜 by Heroku — MIT License