Skip to content

Commit e1087ed

Browse files
committed
chore(developer): fix commander global options
1 parent a9fb7a1 commit e1087ed

File tree

6 files changed

+51
-47
lines changed

6 files changed

+51
-47
lines changed

developer/src/kmc/src/commands/analyze.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,21 @@ interface AnalysisActivityOptions /* not inheriting from CompilerBaseOptions */
2222

2323
export function declareAnalyze(program: Command) {
2424
let command = program.command('analyze [infile...]');
25-
BaseOptions.addVersion(command);
2625
declareOskCharUse(command);
2726
declareOskRewrite(command);
2827
}
2928

3029
function declareOskCharUse(command: Command) {
3130
let subCommand = command.command('osk-char-use');
32-
BaseOptions.addVersion(subCommand);
3331
BaseOptions.addLogLevel(subCommand);
3432
subCommand
3533
.description('Analyze On Screen Keyboards for character usage')
3634
.option('-b, --base', 'First PUA codepoint to use, in hexadecimal (default F100)', 'F100')
3735
.option('--include-counts', 'Include number of times each character is referenced', false)
3836
.option('--strip-dotted-circle', 'Strip U+25CC (dotted circle base) from results', false)
3937
.addOption(new Option('-m, --mapping-file <filename>', 'Result file to write to (.json, .md, or .txt)').makeOptionMandatory())
40-
.action(async (filenames: string[], options: any) => {
38+
.action(async (filenames: string[], _options: any, commander: any) => {
39+
const options = commander.optsWithGlobals();
4140
if(!filenames.length) {
4241
// If there are no filenames provided, then we are building the current
4342
// folder ('.') as a project-style build

developer/src/kmc/src/commands/build.ts

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { expandFileLists } from '../util/fileLists.js';
1111
import { isProject } from '../util/projectLoader.js';
1212
import { buildTestData } from './buildTestData/index.js';
1313
import { buildWindowsPackageInstaller } from './buildWindowsPackageInstaller/index.js';
14+
//import { buildWindowsPackageInstaller } from './buildWindowsPackageInstaller/index.js';
1415

1516

1617
function commandOptionsToCompilerOptions(options: any): CompilerOptions {
@@ -32,31 +33,38 @@ function commandOptionsToCompilerOptions(options: any): CompilerOptions {
3233
}
3334

3435
export function declareBuild(program: Command) {
35-
const command = BaseOptions.addAll(program
36-
.command('build [infile...]')
37-
.description(`Compile one or more source files or projects.`)
36+
const buildCommand = program
37+
.command('build')
38+
.option('--color', 'Force colorization for log messages')
39+
.option('--no-color', 'No colorization for log messages; if both omitted, detects from console')
40+
41+
// These options are only used with build file but are included here so that
42+
// they are visible in `kmc build --help`
43+
.option('-d, --debug', 'Include debug information in output')
44+
.option('-w, --compiler-warnings-as-errors', 'Causes warnings to fail the build; overrides project-level warnings-as-errors option')
45+
.option('-W, --no-compiler-warnings-as-errors', 'Warnings do not fail the build; overrides project-level warnings-as-errors option')
46+
.option('--no-compiler-version', 'Exclude compiler version metadata from output')
47+
.option('--no-warn-deprecated-code', 'Turn off warnings for deprecated code styles');
48+
49+
BaseOptions.addAll(buildCommand);
50+
51+
buildCommand.command('file [infile...]', {isDefault: true})
52+
.description(`Compile one or more source files or projects ('file' subcommand is default).`)
3853
.addHelpText('after', `
3954
Supported file types:
40-
* folder: Keyman project in folder
41-
* .kpj: Keyman project
42-
* .kmn: Keyman keyboard
43-
* .xml: LDML keyboard
44-
* .model.ts: Keyman lexical model
45-
* .kps: Keyman keyboard or lexical model package
55+
* folder: Keyman project in folder
56+
* .kpj: Keyman project
57+
* .kmn: Keyman keyboard
58+
* .xml: LDML keyboard
59+
* .model.ts: Keyman lexical model
60+
* .kps: Keyman keyboard or lexical model package
4661
4762
File lists can be referenced with @filelist.txt.
4863
4964
If no input file is supplied, kmc will build the current folder.`)
50-
)
51-
.option('-d, --debug', 'Include debug information in output')
52-
.option('-w, --compiler-warnings-as-errors', 'Causes warnings to fail the build; overrides project-level warnings-as-errors option')
53-
.option('-W, --no-compiler-warnings-as-errors', 'Warnings do not fail the build; overrides project-level warnings-as-errors option')
54-
.option('--no-compiler-version', 'Exclude compiler version metadata from output')
55-
.option('--no-warn-deprecated-code', 'Turn off warnings for deprecated code styles')
56-
.option('--color', 'Force colorization for log messages')
57-
.option('--no-color', 'No colorization for log messages; if both omitted, detects from console')
58-
.action(async (filenames: string[], options: any) => {
59-
options = commandOptionsToCompilerOptions(options);
65+
66+
.action(async (filenames: string[], _options: any, commander: any) => {
67+
const options = commandOptionsToCompilerOptions(commander.optsWithGlobals());
6068
const callbacks = new NodeCompilerCallbacks(options);
6169

6270
if(!filenames.length) {
@@ -77,12 +85,12 @@ If no input file is supplied, kmc will build the current folder.`)
7785
}
7886
});
7987

80-
command
88+
buildCommand
8189
.command('ldml-test-data <infile>')
8290
.description('Convert LDML keyboard test .xml to .json')
8391
.action(buildTestData);
8492

85-
command
93+
buildCommand
8694
.command('windows-package-installer <infile>')
8795
.description('Build an executable installer for Windows for a Keyman package')
8896
.option('--msi <msiFilename>', 'Location of keymandesktop.msi')

developer/src/kmc/src/commands/buildTestData/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import { CompilerBaseOptions, CompilerCallbacks, defaultCompilerOptions, LDMLKey
55
import { NodeCompilerCallbacks } from '../../util/NodeCompilerCallbacks.js';
66
import { fileURLToPath } from 'url';
77

8-
export function buildTestData(infile: string, options: CompilerBaseOptions) {
8+
export function buildTestData(infile: string, _options: any, commander: any) {
9+
const options: CompilerBaseOptions = commander.optsWithGlobals();
10+
911
let compilerOptions: kmcLdml.LdmlCompilerOptions = {
1012
...defaultCompilerOptions,
1113
...options,

developer/src/kmc/src/commands/buildWindowsPackageInstaller/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ interface WindowsPackageInstallerOptions extends CompilerBaseOptions {
1414
startWithConfiguration: boolean;
1515
};
1616

17-
export async function buildWindowsPackageInstaller(infile: string, options: WindowsPackageInstallerOptions) {
17+
export async function buildWindowsPackageInstaller(infile: string, _options: any, commander: any) {
18+
const options: WindowsPackageInstallerOptions = commander.optsWithGlobals();
1819
const sources: WindowsPackageInstallerSources = {
1920
licenseFilename: options.license,
2021
msiFilename: options.msi,

developer/src/kmc/src/kmc.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
* kmc - Keyman Next Generation Compiler
44
*/
55

6-
import { Command } from 'commander';
6+
import { Command, Option } from 'commander';
77
import { declareBuild } from './commands/build.js';
88
import { declareAnalyze } from './commands/analyze.js';
9-
import { BaseOptions } from './util/baseOptions.js';
109
import { KeymanSentry } from './util/KeymanSentry.js';
10+
import KEYMAN_VERSION from "@keymanapp/keyman-version";
1111

1212
await KeymanSentry.runTestIfCLRequested();
1313
try {
@@ -24,9 +24,19 @@ async function run() {
2424
/* Arguments */
2525

2626
const program = new Command();
27-
program.description('Keyman Developer Command Line Interface');
28-
BaseOptions.addVersion(program);
29-
BaseOptions.addSentry(program);
27+
program
28+
.description('Keyman Developer Command Line Interface')
29+
.configureHelp({
30+
showGlobalOptions: true
31+
})
32+
.version(KEYMAN_VERSION.VERSION_WITH_TAG)
33+
34+
// This corresponds to an option tested in KeymanSentry.ts, which is
35+
// searched for in process.argv, in order to avoid depending on Commander to
36+
// start Sentry, and to ensure that we capture errors as early as possible
37+
// in launch
38+
.addOption(new Option('--no-error-reporting', 'Disable error reporting to keyman.com (overriding user settings)'))
39+
.addOption(new Option('--error-reporting', 'Enable error reporting to keyman.com (overriding user settings)'));
3040

3141
if(await KeymanSentry.isEnabled()) {
3242
KeymanSentry.init();

developer/src/kmc/src/util/baseOptions.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
11
import { ALL_COMPILER_LOG_FORMATS, ALL_COMPILER_LOG_LEVELS } from "@keymanapp/common-types";
22
import { Command, Option } from "commander";
3-
import KEYMAN_VERSION from "@keymanapp/keyman-version";
43

54
// These options map to CompilerBaseOptions
65

76
export class BaseOptions {
8-
public static addVersion(program: Command) {
9-
return program.version(KEYMAN_VERSION.VERSION_WITH_TAG);
10-
}
11-
12-
public static addSentry(program: Command) {
13-
// This corresponds to an option tested in KeymanSentry.ts, which is
14-
// searched for in process.argv, in order to avoid depending on Commander to
15-
// start Sentry, and to ensure that we capture errors as early as possible
16-
// in launch
17-
return program
18-
.addOption(new Option('--no-error-reporting', 'Disable error reporting to keyman.com (overriding user settings)'))
19-
.addOption(new Option('--error-reporting', 'Enable error reporting to keyman.com (overriding user settings)'));
20-
}
21-
227
public static addLogLevel(program: Command) {
238
return program.addOption(new Option('-l, --log-level <logLevel>', 'Log level').choices(ALL_COMPILER_LOG_LEVELS).default('info'));
249
}
@@ -33,7 +18,6 @@ export class BaseOptions {
3318

3419
public static addAll(program: Command) {
3520
return [
36-
this.addVersion,
3721
this.addLogLevel,
3822
this.addLogFormat,
3923
this.addOutFile,

0 commit comments

Comments
 (0)