|
1 | 1 | // @ts-check |
2 | 2 |
|
3 | | -export {}; |
| 3 | +import { CliError, GracefulEarlyExitError } from '@black-flag/core'; |
| 4 | +import { defaultUsageText } from '@black-flag/core/util'; |
| 5 | + |
| 6 | +/** |
| 7 | + * @type {() => import('@black-flag/core').RootConfiguration<{ 'fail-style': |
| 8 | + * 'short' | 'full', 'fail-for-yargs': boolean, 'fail-for-cli': boolean, |
| 9 | + * 'fail-for-other': boolean, 'no-show-help': boolean, throw?: 'graceful' | |
| 10 | + * 'cli' | 'other' }>} |
| 11 | + */ |
| 12 | +export default function command() { |
| 13 | + return { |
| 14 | + usage: |
| 15 | + defaultUsageText + |
| 16 | + 'Use --throw to throw a specific type of error. To throw a Yargs error, enter an unknown argument.', |
| 17 | + |
| 18 | + builder(bf, _, argv) { |
| 19 | + bf.example('$0 --throw graceful', ''); |
| 20 | + bf.example('$0 --throw cli', ''); |
| 21 | + bf.example('$0 --fake', ''); |
| 22 | + bf.example('$0 --fake --no-show-help', ''); |
| 23 | + bf.example('$0 --throw other', ''); |
| 24 | + bf.example('$0 --throw other --fail-for-other', ''); |
| 25 | + bf.example('$0 --fake --fail-for-yargs false', ''); |
| 26 | + |
| 27 | + bf.parserConfiguration({ 'boolean-negation': false }); |
| 28 | + |
| 29 | + if (argv) { |
| 30 | + bf.showHelpOnFail( |
| 31 | + argv.noShowHelp |
| 32 | + ? false |
| 33 | + : { |
| 34 | + outputStyle: argv.failStyle, |
| 35 | + showFor: { |
| 36 | + cli: argv.failForCli, |
| 37 | + yargs: argv.failForYargs, |
| 38 | + other: argv.failForOther |
| 39 | + } |
| 40 | + } |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + return { |
| 45 | + 'fail-style': { choices: ['short', 'full'], default: 'short' }, |
| 46 | + 'fail-for-yargs': { boolean: true, default: true }, |
| 47 | + 'fail-for-cli': { boolean: true, default: false }, |
| 48 | + 'fail-for-other': { boolean: true, default: false }, |
| 49 | + 'no-show-help': { |
| 50 | + boolean: true, |
| 51 | + default: false |
| 52 | + // We could do this in Black Flag Extensions, but vanilla Yargs does |
| 53 | + // not support this otherwise-intuitive use of `conflicts`! |
| 54 | + // conflicts: [ |
| 55 | + // 'fail-for-style', |
| 56 | + // 'fail-for-yargs', |
| 57 | + // 'fail-for-cli', |
| 58 | + // 'fail-for-other' |
| 59 | + // ] |
| 60 | + }, |
| 61 | + throw: { |
| 62 | + choices: ['graceful', 'cli', 'other'] |
| 63 | + } |
| 64 | + }; |
| 65 | + }, |
| 66 | + |
| 67 | + handler(argv) { |
| 68 | + switch (argv.throw) { |
| 69 | + case 'graceful': { |
| 70 | + throw new GracefulEarlyExitError('thrown GracefulEarlyExit error'); |
| 71 | + break; |
| 72 | + } |
| 73 | + |
| 74 | + case 'cli': { |
| 75 | + throw new CliError('thrown CLI error'); |
| 76 | + break; |
| 77 | + } |
| 78 | + |
| 79 | + case 'other': { |
| 80 | + throw new Error('thrown error'); |
| 81 | + break; |
| 82 | + } |
| 83 | + |
| 84 | + default: { |
| 85 | + console.log('hello, world!'); |
| 86 | + break; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + }; |
| 91 | +} |
0 commit comments