Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't Show options when there are no options #689

Closed
mateodelnorte opened this issue Sep 5, 2017 · 6 comments
Closed

Don't Show options when there are no options #689

mateodelnorte opened this issue Sep 5, 2017 · 6 comments

Comments

@mateodelnorte
Copy link

The simplest possible executable with a single command and no options still forces showing of options:

const program = require('commander');

program
  .command('cmd', 'a single cmd with no options')

program
  .parse(process.argv);

yields:

./bin/my-cmd

  Usage: my-cmd [options] [command]


  Options:

    -h, --help  output usage information


  Commands:

    cmd         a single cmd with no options
    help [cmd]  display help for [cmd]

Why show options here? Help is already redundant, and the resulting UI is less usable because the intended call to action obscured by a big in-your-face prompt about -h.

Can we allow setting a property to now show options?

@grant
Copy link
Contributor

grant commented Oct 3, 2017

The line to change would be here:
https://github.com/tj/commander.js/blob/master/index.js#L956

Probably a change like this:

Current

Command.prototype.optionHelp = function() {
  var width = this.largestOptionLength();

  // Append the help information
  return this.options.map(function(option) {
      return pad(option.flags, width) + '  ' + option.description
        + (option.defaultValue !== undefined ? ' (default: ' + option.defaultValue + ')' : '');
  }).concat([pad('-h, --help', width) + '  ' + 'output usage information'])
    .join('\n');
};

Proposed

Command.prototype.optionHelp = function() {
  if (!this.options.length) return [];
  var width = this.largestOptionLength();

  // Append the help information
  return this.options.map(function(option) {
      return pad(option.flags, width) + '  ' + option.description
        + (option.defaultValue !== undefined ? ' (default: ' + option.defaultValue + ')' : '');
  }).concat([pad('-h, --help', width) + '  ' + 'output usage information'])
    .join('\n');
};

Only join if the array is not empty.

@shadowspawn
Copy link
Collaborator

This has got a few likes and I have thought the same myself at times, so although no recent activity on this issue I am adding a comment rather than closing as stale.

A reason for not returning empty .optionHelp when no custom options (as suggested) is that the help may be displayed programatically and in that case the help entry itself is not redundant. Otherwise this would be a nice simple approach.

If we end up with a parameter for other help behaviours that have been requested, like changing exit behaviour, then there would at least be a place to add a property for suppressing option help as suggested here.

@oa495

This comment has been minimized.

@shadowspawn

This comment has been minimized.

@shadowspawn
Copy link
Collaborator

This issue has not had any activity in over six months. Closing in favour of higher level #1225.

Feel free to open a new issue if it comes up again, with new information and renewed interest.

Thank you for your contributions.

@shadowspawn
Copy link
Collaborator

shadowspawn commented Dec 17, 2023

You can suppress all the options from the help using .configureHelp() like this:

program
  .command('cmd')
  .description('a single cmd with no options');

// Hide the options for the top-level help without affecting subcommand help
// by adding configuration after adding the subcommand.
program
  .configureHelp({
    visibleOptions: () => [],
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants