Skip to content

global options with default values are missing when running actions #203

@johnwebbcole

Description

@johnwebbcole

If you have a global option, with a default value, that value does not appear in the options object inside an action. However it does appear on the --help page.

Here is a playground example:

import { program } from "@caporal/core"


program
  .option('--foo <baz>', 'a global option', { default: 'bar', global: true })
  .command('order', 'Order command')

  .argument('<pizza-type>', 'Pizza type')
  .action(({ logger, args, options }) => {
    logger.info(JSON.stringify({ args, options }));
    logger.info('Ordering pizza %s', args.pizzaType);
  });

program.run();

Expected behavior
The global options with default values should appear in the action options object.

I'd expect: {"args":{"pizzaType":"one"},"options":{"baz": "bar"}}

Actual behavior
Here is the output from the example above:

~$ play order one
{"args":{"pizzaType":"one"},"options":{}}
Ordering pizza one

And the help, which shows the default value.

~$ play --help

  Caporal Playground 1.0.0 — Dynamicaly generated playground program

  USAGE 
  
    ▸ play <command> [ARGUMENTS...] [OPTIONS...]


  COMMANDS — Type 'play help <command>' to get some help about a command

    order                                Order command                                          

  GLOBAL OPTIONS

    -h, --help                           Display global help or command-related help.           
    -V, --version                        Display version.                                       
    --no-color                           Disable use of colors in output.                       
    -v, --verbose                        Verbose mode: will also output debug messages.         
    --quiet                              Quiet mode - only displays warn and error messages.    
    --silent                             Silent mode: does not output anything, giving no       
                                         indication of success or failure other than the exit   
                                         code.                                                  
    --foo <baz>                          a global option                                        
                                         default: "bar"          

Environment information (please complete the following information):

  • OS: Mac and in the ground
  • OS version: 10.15.7
  • Shell: zsh
  • Caporal version: 2.0.2

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions