3

I use yargs for a complexe command line (with subcommands via commandDir). I'd like to use the .fail(fn) to send an email every time the given command failed. The .fail(fn) is correctly triggered but I'd like to be able to have access to the args given to the command. .fail(fn) only gives me access to msg, err, yargs.

require('yargs')
.commandDir('commands', {recurse: false})
.option('verbose', {
    alias: 'v',
    type: 'boolean',
    description: 'Run with verbose logging',
    default: false
})
.option('senderr', {
    alias: 'se',
    type: 'boolean',
    description: 'Send error(s) via email',
    default: true
})
.demandCommand()
.help()
.fail(function (msg, err, yargs) {
    console.log(err);
    process.exit(1);
})
.locale('fr')
.argv

Is it possible to access the argv.senderr value inside the fail function ? Thanks !

Morgan
  • 93
  • 1
  • 10

2 Answers2

0

I ran into the same issue and there doesn't appear to be a current solution, however I recently posted a github issue: https://github.com/yargs/yargs/issues/2133

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31032538) – Bert Blommers Feb 15 '22 at 15:58
-1
const argv = require('yargs')
.commandDir('commands', {recurse: false})
.option('verbose', {
    alias: 'v',
    type: 'boolean',
    description: 'Run with verbose logging',
    default: false
})
.option('senderr', {
    alias: 'se',
    type: 'boolean',
    description: 'Send error(s) via email',
    default: true
})
.demandCommand()
.help()
.fail(function (msg, err, yargs) {
    if (argv.verbose) console.log(err);
    process.exit(1);
})
.locale('fr')
.argv
temich
  • 1
  • Please provide details to explain your suggested solution, such as how/why it works, how is it responding the question? – tmilar Feb 24 '21 at 21:42