1 | import { Option as CommanderOption } from 'commander';
|
2 | import * as App from './application.mjs';
|
3 | import { emitSuggestionResult } from './emitters/suggestionsEmitter.js';
|
4 | import { CheckFailed } from './util/errors.js';
|
5 | function collect(value, previous) {
|
6 | value = value.replace(/^=/, '');
|
7 | if (!previous) {
|
8 | return [value];
|
9 | }
|
10 | return [...previous, value];
|
11 | }
|
12 | function count(_, previous) {
|
13 | return (previous || 0) + 1;
|
14 | }
|
15 | function asNumber(value, prev) {
|
16 | return Number.parseInt(value, 10) ?? prev;
|
17 | }
|
18 | export function commandSuggestion(prog) {
|
19 | const suggestionCommand = prog.command('suggestions');
|
20 | suggestionCommand
|
21 | .aliases(['sug', 'suggest'])
|
22 | .description('Spelling Suggestions for words.')
|
23 | .option('-c, --config <cspell.json>', 'Configuration file to use. By default cspell looks for cspell.json in the current directory.')
|
24 | .option('--locale <locale>', 'Set language locales. i.e. "en,fr" for English and French, or "en-GB" for British English.')
|
25 | .option('--language-id <language>', 'Use programming language. i.e. "php" or "scala".')
|
26 | .addOption(new CommanderOption('--languageId <language>', 'Use programming language. i.e. "php" or "scala".').hideHelp())
|
27 | .option('-s, --no-strict', 'Ignore case and accents when searching for words.')
|
28 | .option('--ignore-case', 'Alias of --no-strict.')
|
29 | .option('--num-changes <number>', 'Number of changes allowed to a word', asNumber, 4)
|
30 | .option('--num-suggestions <number>', 'Number of suggestions', asNumber, 8)
|
31 | .option('--no-include-ties', 'Force the number of suggested to be limited, by not including suggestions that have the same edit cost.')
|
32 | .option('--stdin', 'Use stdin for input.')
|
33 | .addOption(new CommanderOption('--repl', 'REPL interface for looking up suggestions.'))
|
34 | .option('-v, --verbose', 'Show detailed output.', count, 0)
|
35 | .option('-d, --dictionary <dictionary name>', 'Use the dictionary specified. Only dictionaries specified will be used.', collect)
|
36 | .option('--dictionaries <dictionary names...>', 'Use the dictionaries specified. Only dictionaries specified will be used.')
|
37 | .option('--no-color', 'Turn off color.')
|
38 | .option('--color', 'Force color')
|
39 | .arguments('[words...]')
|
40 | .action(async (words, options) => {
|
41 | App.parseApplicationFeatureFlags(options.flag);
|
42 | options.useStdin = options.stdin;
|
43 | options.dictionaries = mergeArrays(options.dictionaries, options.dictionary);
|
44 | if (!words.length && !options.useStdin && !options.repl) {
|
45 | suggestionCommand.outputHelp();
|
46 | throw new CheckFailed('outputHelp', 1);
|
47 | }
|
48 | for await (const r of App.suggestions(words, options)) {
|
49 | emitSuggestionResult(r, options);
|
50 | }
|
51 | });
|
52 | return suggestionCommand;
|
53 | }
|
54 | function mergeArrays(a, b) {
|
55 | if (a === undefined)
|
56 | return b;
|
57 | if (b === undefined)
|
58 | return a;
|
59 | return [...a, ...b];
|
60 | }
|
61 |
|
\ | No newline at end of file |