Skip to content

How to access options value? #1654

@znat

Description

@znat

Hi there, sorry if this is a noob question but I am struggling to get the command option value in the following code:

const program = new Command()
    const version = getVersion()
    await Config.init()
    program
      .version(version)
      .command('default')
      .description('My CLI')
      .action(async () => { defaultCommand() })

    program
      .command('import')
      .description('Import data ')
      .addArgument(new Argument('[account]', 'Account to import'))
      .addArgument(new Argument('[transaction-type]', 'Type of transactions to import').choices(['deposits', 'withdrawals]))
      .addOption(new Option('-f, --file <file>', 'File to import data from'))
      .action((account, transactionType, options) => {
        console.log(account, transactionType, options)
      })

Then when I execute the import command:

$ npm run dev import acc deposits --file blabla

> [email protected] dev /Users/nathanzylbersztejn/Documents/repos/greedy_cli
> DEV=true npx -r ts-node src/cli/cli.ts "import" "acc" "deposits" "/path/to/file"

acc deposits
{}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions