dthree / vorpal

Node's framework for interactive CLIs
http://vorpal.js.org
MIT License
5.64k stars 280 forks source link

Required argument of option forces the usage of that option #76

Closed richard-nkrumah closed 8 years ago

richard-nkrumah commented 8 years ago

I am trying to add a command that has an option with a required argument but the option itself should be ... optional. The following script has been stolen from another issue and shows the problem:

"use strict";

var Vorpal = require('vorpal');
var util = require('util');
var cli = new Vorpal();

cli
  .command('login')
  .description('Login to API')
  .option('-u, --user <username>', 'A username to overwrite the default')
  .action(function(args, done) {
    this.log('args:'+util.inspect(args));
    done();
  });

cli.parse(process.argv);

The following should execute the command but it does not:

laptop$ node test.js login

  Missing required option. Showing Help:

  Usage: login [options]

  Login to API

  Options:

    --help                 output usage information
    -u, --user <username>  A username to overwrite the default

Adding the option without the required username also misbehaves:

laptop$ node test.js login -u
args:{ options: { user: true } }

The command does not fail with the "Missing required argument" error I saw in the code.

It seems that the necessity of the option depends only on the presence of "<" in the flags (option.js:20) which makes not only the argument to the option mandatory but the option itself.

Or maybe I am just totally wrong because in issue #52 a similar code snippet worked. But for me it does not.

I am using vorpal 1.5.5 on node v5.4.0.

dthree commented 8 years ago

Yeah, someone did a PR on options a bit ago to fix another issue and it created this issue. I have to do a number on options to ensure its all set straight. For now, could you make it optional and do a manual validation in your action method?

jescalan commented 8 years ago

+1 for this as well :grin:

dthree commented 8 years ago

This is now fixed.

https://github.com/dthree/vorpal/commit/8c091f948be99d55bb7120233285638c620c98b1