Skip to content

Commit 663fc1c

Browse files
committed
docs(examples): commit finished "error" and "esm-cjs" and "exports" and "hierarchy" examples
1 parent b60bc29 commit 663fc1c

File tree

25 files changed

+475
-6
lines changed

25 files changed

+475
-6
lines changed

examples/black-flag/error/cli.js

100644100755
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,13 @@
22

33
import { runProgram } from '@black-flag/core';
44

5-
export default runProgram(import.meta.resolve('./commands'));
5+
export default runProgram(import.meta.resolve('./commands'), {
6+
configureErrorHandlingEpilogue(...args) {
7+
if (args[2].state.didOutputHelpOrVersionText) {
8+
console.log('\n--\n');
9+
}
10+
11+
console.log('handled error in error handling hook');
12+
console.log('hook args entries (reversed):', Object.entries(args).reverse());
13+
}
14+
});
Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,91 @@
11
// @ts-check
22

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+
}

examples/black-flag/esm-cjs/cli.cjs

100644100755
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#!/usr/bin/env node
22

3-
const path = require('node:path');
4-
53
const { runProgram } = require('@black-flag/core');
64

7-
module.exports = runProgram(path.dirname(require.resolve('./commands')));
5+
module.exports = runProgram('./commands');

examples/black-flag/esm-cjs/cli.js

100644100755
File mode changed.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @ts-check
2+
3+
/**
4+
* @type {() => import('@black-flag/core').RootConfiguration}
5+
*/
6+
module.exports = function command() {
7+
return {
8+
handler() {
9+
console.log('ran command at', __filename);
10+
}
11+
};
12+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @ts-check
2+
3+
/**
4+
* @type {() => import('@black-flag/core').RootConfiguration}
5+
*/
6+
export default function command() {
7+
return {
8+
handler() {
9+
console.log('ran command at', import.meta.filename);
10+
}
11+
};
12+
}

examples/black-flag/exports/cli.js

100644100755
File mode changed.
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
// @ts-check
22

3-
export {};
3+
/**
4+
* @type {import('@black-flag/core').RootConfiguration['builder']}
5+
*/
6+
export const builder = (bf) => {
7+
bf.example('$0 --help', '');
8+
bf.example('$0 recommended --help', '');
9+
bf.example('$0 recommended cjs --help', '');
10+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// @ts-check
2+
3+
const { $executionContext } = require('@black-flag/core');
4+
const { defaultUsageText } = require('@black-flag/core/util');
5+
6+
/**
7+
* @type {import('@black-flag/core').ChildConfiguration<{ name: string, id:
8+
* string, force: boolean }>}
9+
*/
10+
module.exports = {
11+
command: '$0 <name> <id>',
12+
description: 'Add a new remote',
13+
14+
usage: `${defaultUsageText}.
15+
16+
You can also provide long-form explanatory text here that will only be displayed when --help is given. Long-form usage text will not be shown when errors occur by default, but this can be configured using \`configureExecutionContext\`.
17+
18+
See the documentation for more details.`,
19+
20+
builder(blackFlag, _, argv) {
21+
blackFlag.positional('name', {
22+
description: `The name of the remote to${argv?.force ? ' forcibly' : ''} add`
23+
});
24+
25+
blackFlag.positional('id', { description: 'The remote identifier' });
26+
27+
return {
28+
force: {
29+
alias: 'f',
30+
boolean: true,
31+
description: 'Override protections',
32+
default: false
33+
}
34+
};
35+
},
36+
37+
async handler({ name, id, force, [$executionContext]: context }) {
38+
if (force) {
39+
console.log('Running with --force, recommended protections DISABLED!');
40+
}
41+
42+
console.log(`Added new remote "${name}": ${id}`);
43+
console.log('Saw context.somethingSpecial:', context.somethingSpecial);
44+
}
45+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// @ts-check
2+
3+
import { $executionContext } from '@black-flag/core';
4+
import { defaultUsageText } from '@black-flag/core/util';
5+
6+
/**
7+
* @type {import('@black-flag/core').ChildConfiguration<{ name: string, id:
8+
* string, force: boolean }>}
9+
*/
10+
export default {
11+
command: '$0 <name> <id>',
12+
description: 'Add a new remote',
13+
14+
usage: `${defaultUsageText}.
15+
16+
You can also provide long-form explanatory text here that will only be displayed when --help is given. Long-form usage text will not be shown when errors occur by default, but this can be configured using \`configureExecutionContext\`.
17+
18+
See the documentation for more details.`,
19+
20+
builder(blackFlag, _, argv) {
21+
blackFlag.positional('name', {
22+
description: `The name of the remote to${argv?.force ? ' forcibly' : ''} add`
23+
});
24+
25+
blackFlag.positional('id', { description: 'The remote identifier' });
26+
27+
return {
28+
force: {
29+
alias: 'f',
30+
boolean: true,
31+
description: 'Override protections',
32+
default: false
33+
}
34+
};
35+
},
36+
37+
async handler({ name, id, force, [$executionContext]: context }) {
38+
if (force) {
39+
console.log('Running with --force, recommended protections DISABLED!');
40+
}
41+
42+
console.log(`Added new remote "${name}": ${id}`);
43+
console.log('Saw context.somethingSpecial:', context.somethingSpecial);
44+
}
45+
};

0 commit comments

Comments
 (0)