Skip to main content

action

action

The action property is a function that defines the command's behavior. When a command is executed, the action function is called with the command's arguments and options as arguments. This function should contain the logic for the command's behavior, such as making API requests, reading and writing files, or executing other commands.

{
action: (data, details) => {
// command logic here
}
}

Parameters

The action function receives two parameters:

  • data - an object containing the command's data (via arguments, options, flags, and prompts)
  • details - an object containing additional information about the command data, such as tags
    • args - an object containing the command's arguments
    • opts - an object containing the command's options
    • flags - an object containing the command's flags
    • subcommand - the subcommand, if any
    • unknown - an array of unknown arguments
    • tags - an object containing the command's tags
{
data: {},
details: {
args: {},
opts: {},
flags: {},
subcommand: null,
unknown: [],
tags: {},
}
}

Example

import { Command } from '@panda/command'

new Command({
name: 'create',
arguments: {
name: 'name',
required: true,
tags: ['info']
},
options: [
{
name: 'description',
type: 'string',
description: 'A description of the item',
required: true,
tags: ['info']
}
],
action: (data, details) => {
console.log(`Creating ${data.name}...`)
console.log(details)
}
})
$ panda create my-app --description "A new app" bar
Creating my-app...
{
args: { name: 'my-app' },
opts: { description: 'A new app' },
flags: {},
subcommand: null,
unknown: ['bar'],
tags: {
info: {
name: 'my-app',
description: 'A new app'
}
}
}