Anypoint CLI Command Library

1.6.8 · active · verified Wed Apr 22

The `@mulesoft/anypoint-cli-command` package provides the foundational `Core` class for developing custom commands within the Anypoint CLI version 4 and later ecosystem. It extends `@oclif/core`, inheriting its robust command-line interface framework while adding Anypoint-specific functionalities. Developers leverage this library to gain convenient access to common CLI parameters, a standardized logging mechanism, output formatting utilities, and support for Anypoint platform features like scratch orgs. The current stable version, 1.6.8, is closely aligned with the Anypoint CLI's development cycle, receiving updates as part of the broader toolset's evolution. Its key differentiator is the deep integration with Anypoint platform concepts, streamlining the creation of purpose-built Anypoint CLI extensions.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates creating a new CLI command by extending the `Core` class, parsing flags, and using the built-in logger.

import { Command, Flags } from '@oclif/core';
import { Core } from '@mulesoft/anypoint-cli-command';

interface MyCommandFlags {
  name: string;
  force: boolean;
}

export default class MyHelloCommand extends Core<typeof MyHelloCommand> {
  static description = 'Say hello to a specified name using Anypoint CLI command base.';

  static examples = [
    '<%= config.bin %> <%= command.id %> --name Alice',
    '<%= config.bin %> <%= command.id %> -f'
  ];

  static flags = {
    name: Flags.string({ char: 'n', description: 'Name to greet.', required: false }),
    force: Flags.boolean({ char: 'f', description: 'Force a greeting even if no name is provided.', default: false }),
  };

  public async run(): Promise<void> {
    const { flags } = await this.parse(MyHelloCommand);
    const { name, force } = flags;

    if (force) {
      this.log('Forcing greeting operation...');
    }

    if (name) {
      this.log(`Hello, ${name} from your Anypoint CLI Command!`);
    } else if (force) {
      this.log('Hello, anonymous user (forced greeting)!');
    } else {
      this.error('Please provide a name or use the --force flag.');
    }

    this.logger.info(`MyHelloCommand executed with name: ${name ?? 'N/A'}, force: ${force}`);
    this.exit(0);
  }
}

view raw JSON →