75lb / command-line-args

A mature, feature-complete library to parse command-line options.
MIT License
679 stars 107 forks source link

defaultOption is now required in definitions #91

Closed bfabec closed 5 years ago

bfabec commented 5 years ago

The defaultOption is now being required in the definitions which is breaking our scripts in 5.0.1. This is new for the current version, as 4.0.7 was/is working fine.

Sample test program:

#!/usr/bin/env node
'use strict';

const commandLineCommands = require('command-line-commands');
const commandLineArgs = require('command-line-args');
const commandLineUsage = require('command-line-usage');

const argData = {
  working: {
    definitions: [
      { name: 'topic', type: String, description: 'Working!', defaultOption: true }
    ]
  },
  broken: {
    definitions: [
      { name: 'topic', type: String, description: 'Broken!', defaultOption: false }
    ]
  }
};

const { command, argv } = commandLineCommands([ 'working', 'broken' ]);
const options = commandLineArgs(argData[command].definitions, argv);
const usage = commandLineUsage(argData[command].usage);

switch (command) {
case 'working':
  console.log('Working!');
  break;
case 'broken':
  console.log('Broken!');
  break;
default:
}

Sample output:

brians-mbp:common-tools bfabec@us.ibm.com$ node scripts/test.js working
Working!
brians-mbp:common-tools bfabec@us.ibm.com$ node scripts/test.js broken
/Users/bfabec@us.ibm.com/workspace/common-tools/node_modules/command-line-args/index.js:57
        throw err
        ^

UNKNOWN_VALUE: Unknown value: broken
    at commandLineArgs (/Users/bfabec@us.ibm.com/workspace/common-tools/node_modules/command-line-args/index.js:54:21)
    at Object.<anonymous> (/Users/bfabec@us.ibm.com/workspace/common-tools/scripts/test.js:36:17)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:191:16)
    at bootstrap_node.js:612:3
75lb commented 5 years ago

Hi, the syntax you want is:

const options = commandLineArgs(argData[command].definitions, { argv });

The second argument to commandLineArgs is an options object, not an array. See the docs.

bfabec commented 5 years ago

@75lb thanks for that!