Skip to content

main command action handler options empty #1869

@machinshin

Description

@machinshin

Hey all, i'm trying to separate out the action handler for a top-level command with the code below.

the options variable in my action handler is undefined, and it's unclear why.

  // src/ts/program.ts
import { Command } from 'commander';

const program = new Command('cmd-name');
program
  .version('0.0.1') // real code loads this from package.json
  .description("program description string")
  .argument('<input-file>', 'file for operations')
  .requiredOption('-c --customer-name <comp>', 'description');
  //add more .option()

export default program;
//src/ts/cli.ts
import program from './program';
program.action((input_file: string, options: Object): Promise<any> => {
  console.log(`input_file: ${input_file}`);
  console.log(`options: ${JSON.stringify(options, null, 2)}`);
 /* more code here */
});
(async () => {
  return await program.parseAsync(process.argv);
})();

so per the documentation, options should be an Object, but running this code, i get
$> node dist/cli-bundle.js -- -c upt ./foo-bar.txt
which displays:

input_file: ./foo-bar.txt
options: undefined

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