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

.help(false) does not disable --help when used in a default command #1516

Closed
daguej opened this issue Dec 19, 2019 · 8 comments
Closed

.help(false) does not disable --help when used in a default command #1516

daguej opened this issue Dec 19, 2019 · 8 comments
Labels

Comments

@daguej
Copy link

daguej commented Dec 19, 2019

.help(false) does not appear to be working correctly in v15.0.2 when used in a default command. It does hide the --help option from the help text, but yargs still prints the help text if you run myapp --help or myapp help.

This is contrary to the docs, which say

If the boolean argument false is provided, it will disable --help.

I got the desired behavior (ie, yargs did nothing with --help) in v8.0.2 by not calling .help(…) at all. I noticed this issue when trying to upgrade.

@mleguen
Copy link
Member

mleguen commented Jan 8, 2020

Reproduced with master branch.

@mleguen
Copy link
Member

mleguen commented Jan 8, 2020

Oups! Sorry, inverted help(false) and parse() in my test code.

I cannot reproduce this issue with the following test code, using either 15.0.2, 15.1.0 or master branch:

index.js

const yargs = require('yargs')

console.log(
  yargs
    .help(false)
    .parse()
)

--help does not show help, as expected (it is only seen as a regular option):

$ node index.js --help
{ _: [], help: true, '$0': 'index.js' }

@daguej Could you please provide a sample code reproducing this issue?

@daguej
Copy link
Author

daguej commented Jan 8, 2020

@mleguen looks like my situation was a bit complex. You have to have a default command with help disabled and another command with help enabled.

var yargs = require('yargs');

var argv = yargs
.command('foo', 'bar', function(yargs) {
	yargs.option('baz', {
		describe: 'qux'
	})
})
.command('*', 'foo', function(yargs) {
	yargs
	.option('help', {
		boolean: true
	})
	.help(false)
})
.argv;

console.log(argv);

Tested against master/v15.1.0 (7fdb3e7).

$ test --help
test

foo

Commands:
  test foo  bar
  test      foo                                                        [default]

Options:
  --version  Show version number                                       [boolean]

@daguej daguej changed the title .help(false) does not disable --help .help(false) does not disable --help when used in a default command Jan 8, 2020
@mleguen
Copy link
Member

mleguen commented Jan 9, 2020

@daguej .help() is not designed to work for subcommands, but at the global level.

If I understand well what you are intending to do:

  • test foo --helpshould mean "display help for foo"?
  • and test --help should mean something else than "display help for the default command"?

I doubt this is a use case we would cover. However, we should state in the doc that .help() is not to be called on commands.

@daguej
Copy link
Author

daguej commented Jan 9, 2020

@mleguen Yes, you understand my intention. I want to manually handle the display of help content for my default command, but let yargs handle help for all other commands.

Are you sure that help() is not meant to be used with commands?

A quick reading of the code shows that yargs.help() is mostly a shortcut for yargs.boolean('help').describe('help', …), and then also sets an internal var helpOpt (and I'd bet this bug is related to the handling of that var). So there's no reason that help() should not work with commands.

I can get my desired behavior by inverting how I call help() (disabling globally and then enabling selectively), and everything works as expected; I can configure help per-command:

var yargs = require('yargs');

var argv = yargs
.command('foo', '2', function(yargs) {
	yargs.option('baz', {
		describe: 'opt'
	})
	.help()
})
.command('bar', '3', function(yargs) {
	yargs.option('qux', {
		describe: 'opt'
	})
	.help('info')
})
.command('*', '1', function(yargs) {
	yargs
	.option('help', {
		boolean: true
	})
})
.help(false)
.argv;

console.log(argv);
$ test --help
{ _: [], help: true, '$0': 'test' }

$ test foo --help
test foo

2

Options:
  --version  Show version number                                       [boolean]
  --baz      opt
  --help     Show help                                                 [boolean]

$ test bar --help
{ _: [ 'bar' ], help: true, '$0': 'test' }

$ test bar --info
test bar

3

Options:
  --version  Show version number                                       [boolean]
  --qux      opt
  --info     Show help                                                 [boolean]

And, having help enabled globally and disabling it selectively in normal commands also works:

var yargs = require('yargs');

var argv = yargs
.command('foo', '2', function(yargs) {
	yargs.option('baz', {
		describe: 'opt'
	})
	.help(false)
})
.argv;

console.log(argv);
$ test --help
test [command]

Commands:
  test foo  2

Options:
  --help     Show help                                                 [boolean]
  --version  Show version number                                       [boolean]

$ test foo --help
{ _: [ 'foo' ], help: true, '$0': 'test' }

So it's only when a default command is involved that the behavior of help() is broken.

This wouldn't be the first time there's a bug with default command help: #810

@mleguen
Copy link
Member

mleguen commented Jan 15, 2020

@daguej Thanks for your analysis.

In fact this occurs only when there is other commands, or when the default command has no aliases, and I think it is rather a wanted behavior than a bug (set skipDefaultCommand to false in yargs.js, run npm test and look at the uses cases described by broken tests).

As I understand it, yargs distinguishes global help and default command help:

  • global help (node index.js --help) lists the default command with its aliases and the other commands
  • default command help (node index.js alias --help with alias being a default command alias) displays only the default command options and subcommands

The only exception is when there are neither other commands, nor aliases to the default command. In that case, yargs replaces the global help (which would have no interest) by the default command help.

So in your use case, as you stated, you indeed have first to disable the global help, then to reenable it in the commands where you do not want to handle help yourself.

@1dotd4
Copy link

1dotd4 commented Apr 24, 2020

closing w/o fixing

true opensource!

@1dotd4
Copy link

1dotd4 commented Apr 24, 2020

yargs
  .command('doStuff', '', () => {}, doStuff)
  .command('help', '', () => {}, myHelp)
  .help(false)
  .parse()

yargs doesn't use myHelp and it's related to help(false).

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

No branches or pull requests

3 participants