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

showHelpOnFail does not show the message when set to false #2085

Closed
neha-sri opened this issue Dec 6, 2021 · 3 comments
Closed

showHelpOnFail does not show the message when set to false #2085

neha-sri opened this issue Dec 6, 2021 · 3 comments
Assignees
Labels

Comments

@neha-sri
Copy link

neha-sri commented Dec 6, 2021

From the docs:

showHelpOnFail(enable: boolean, message?: string): Argv<T>;
By default, yargs outputs a usage string if any error is detected. Use the .showHelpOnFail() method to customize this behavior.
Params:
enable – If false, the usage string is not output.
message – Message that is output after the error message.

We are using it in the following manner:

export const builder = function (yargs: yargs.Argv): yargs.Argv {
  return yargs
    .commandDir('feedback-commands', {
      extensions: ['js'],
    })
    .strict()
    .showHelpOnFail(false, `Use ' --help' for usage`)
    .demandCommand();
};

However, the string passed in the parameter message is never displayed. Only the error message Not enough non-option arguments: got 0, need at least 1 is being displayed.

To be clear, we want to not display the help page but print a string after the error message instead.

PS: The string also doesn't get displayed when the parameter value is set true since --help output instead.

We are on version 17.0.1 but I tried with 17.0.3 as well and same result.

@jly36963
Copy link
Contributor

jly36963 commented Dec 9, 2021

@neha-sri

showHelpOnFail doesn't display the message if it is chained onto yargs globally. It works as expected if chained onto the command builder, within command.

In the example below, only the showHelpOnFail that is chained onto the cmd1 builder has any effect.

yargs('cmd1 --opt1 hello --extraOpt oops')
  .command(
    'cmd1',
    'cmd1 desc',
    yargs => yargs
      .option('opt1', {
        alias: 'o',
        demandOption: true,
      })
      .showHelpOnFail(false, "What have you done????"), // this one works
    argv => console.log(argv)
  )
  .showHelpOnFail(false, "What have you done????") // this one does nothing
  .strict()
  .parse()

This is the output from the above example:

Unknown argument: extraOpt

What have you done????

I made a PR that would make the second showHelpOnFail work as well, but I will likely close it. (I feel it is unnecessary, and I'm nervous about the side effects of the solution)

@jly36963 jly36963 self-assigned this Dec 9, 2021
@bcoe
Copy link
Member

bcoe commented Dec 29, 2021

The discrepancy between the global configuration of showHelpOnFail and the command builder feels a bit weird, but since there's a workaround, I advocate calling this a p2.

@bcoe
Copy link
Member

bcoe commented Mar 31, 2022

fixed in #2154, which will go out soon.

@bcoe bcoe closed this as completed Mar 31, 2022
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