Skip to content

Commit 291226c

Browse files
committed
docs(examples): commit finished "hooks" and "positional" and "shared" examples
1 parent 663fc1c commit 291226c

File tree

11 files changed

+191
-5
lines changed

11 files changed

+191
-5
lines changed

examples/black-flag-extensions/checks/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
# Black Flag Extensions: Custom `check`s
44

55
In this example we demonstrate taking full advantage of BFE's declarative
6-
per-option [check][3] functionality.
6+
per-option [check][3] functionality, including making use of [Black Flag
7+
Checks][4].
78

89
## Run This Example
910

@@ -16,3 +17,4 @@ You can also run this example's tests by executing `npm test`.
1617
[1]: ../../README.md
1718
[2]: ../README.md
1819
[3]: ../../../packages/extensions/README.md#check
20+
[4]: ../../../packages/checks/README.md

examples/black-flag/hooks/cli.js

100644100755
Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
#!/usr/bin/env node
2+
// @ts-check
23

34
import { runProgram } from '@black-flag/core';
45

5-
export default runProgram(import.meta.resolve('./commands'));
6+
export default runProgram(import.meta.resolve('./commands'), {
7+
configureArguments(rawArgv, context) {
8+
console.log('configureArguments -> rawArgv:', rawArgv);
9+
console.log('configureArguments -> context:', context);
10+
return rawArgv;
11+
},
12+
configureErrorHandlingEpilogue(meta, argv, _context) {
13+
console.log('configureErrorHandlingEpilogue -> meta:', meta);
14+
console.log('configureErrorHandlingEpilogue -> argv:', argv);
15+
console.log('configureErrorHandlingEpilogue -> context:', '(the same)');
16+
},
17+
configureExecutionContext(context) {
18+
context.customContextKey = { custom: 'value' };
19+
console.log('configureExecutionContext -> context:', context);
20+
return context;
21+
},
22+
configureExecutionEpilogue(argv, _context) {
23+
console.log('configureExecutionEpilogue -> argv:', argv);
24+
console.log('configureExecutionEpilogue -> context:', '(the same)');
25+
return argv;
26+
},
27+
configureExecutionPrologue(rootPrograms, _context) {
28+
console.log('configureExecutionPrologue -> rootPrograms:', rootPrograms);
29+
console.log('configureExecutionPrologue -> context:', '(the same)');
30+
}
31+
});
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
// @ts-check
22

3-
export {};
3+
export function builder(bf) {
4+
bf.example('$0', '');
5+
bf.example('$0 --help', '');
6+
bf.example('$0 --something "goes here"', '');
7+
bf.example('$0 --error-on-purpose', '');
8+
9+
return {
10+
something: {
11+
string: true
12+
}
13+
};
14+
}
15+
16+
export function handler() {
17+
console.log('ran command');
18+
}

examples/black-flag/positional/cli.js

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

3-
export {};
3+
export const command = '$0 <type> <value>';
4+
5+
/**
6+
* @type {Extract<import('@black-flag/core').ChildConfiguration<{ type: 'string' | 'boolean', value: string, force: boolean }>['builder'], function>}
7+
*/
8+
export function builder(blackFlag, _, argv) {
9+
blackFlag.positional('type', {
10+
description: 'The type of the value you want to pass in',
11+
choices: ['string', 'boolean']
12+
});
13+
14+
blackFlag.positional('value', {
15+
description: 'The value you want to pass in',
16+
...(argv?.type ? { type: argv.type } : {})
17+
});
18+
19+
return {
20+
force: {
21+
alias: 'f',
22+
boolean: true,
23+
description: 'Override protections',
24+
default: false
25+
}
26+
};
27+
}
28+
29+
export async function handler(
30+
/** @type {import('@black-flag/core').Arguments<{ type: 'string' | 'boolean',
31+
* value: string, force: boolean },
32+
* import('@black-flag/core/util').ExecutionContext>} */ { type, value, force }
33+
) {
34+
if (force) {
35+
console.log('Running with --force, recommended protections DISABLED!');
36+
}
37+
38+
console.log(`Doing something with ${type} value: ${value}`);
39+
}

examples/black-flag/shared/cli.js

100644100755
File mode changed.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @ts-check
2+
3+
import { sharedBuilder, sharedHandler } from '../shared.js';
4+
5+
/**
6+
* @type {() => import('@black-flag/core').RootConfiguration}
7+
*/
8+
export default function command() {
9+
return {
10+
builder: sharedBuilder({
11+
flag: { string: true, description: 'An option specific to this command' }
12+
}),
13+
14+
handler: sharedHandler((argv) => {
15+
console.log('(custom functionality) argv:', argv);
16+
})
17+
};
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @ts-check
2+
3+
import { sharedBuilder, sharedHandler } from '../shared.js';
4+
5+
/**
6+
* @type {() => import('@black-flag/core').RootConfiguration}
7+
*/
8+
export default function command() {
9+
return {
10+
builder: sharedBuilder({
11+
flag: { count: true, description: 'An option specific to this command' }
12+
}),
13+
14+
handler: sharedHandler((argv) => {
15+
console.log('(custom functionality) argv:', argv);
16+
})
17+
};
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @ts-check
2+
3+
import { sharedBuilder, sharedHandler } from '../shared.js';
4+
5+
/**
6+
* @type {() => import('@black-flag/core').RootConfiguration}
7+
*/
8+
export default function command() {
9+
return {
10+
builder: sharedBuilder({
11+
flag: { boolean: true, description: 'An option specific to this command' }
12+
}),
13+
14+
handler: sharedHandler((argv) => {
15+
console.log('(custom functionality) argv:', argv);
16+
})
17+
};
18+
}
Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
11
// @ts-check
22

3-
export {};
3+
import { sharedBuilder, sharedHandler } from '../shared.js';
4+
5+
/**
6+
* @type {() => import('@black-flag/core').RootConfiguration}
7+
*/
8+
export default function command() {
9+
return {
10+
builder: sharedBuilder((bf) => {
11+
bf.group(['version'], 'Global Options:');
12+
13+
return {
14+
'custom-flag': {
15+
string: true,
16+
description: 'An option specific to this command'
17+
}
18+
};
19+
}),
20+
21+
handler: sharedHandler((argv) => {
22+
console.log('(custom functionality) argv:', argv);
23+
})
24+
};
25+
}

0 commit comments

Comments
 (0)