Skip to content

Commit 852580f

Browse files
committed
fix(@angular/cli): allow schematic arguments named help
The initial argv parse treated bare `help` as the global help flag, so commands like `ng generate m help` showed help instead of running the schematic.
1 parent 92bb487 commit 852580f

2 files changed

Lines changed: 59 additions & 3 deletions

File tree

‎packages/angular/cli/src/command-builder/command-runner.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,41 @@ import {
3333
import { jsonHelpUsage } from './utilities/json-help';
3434
import { createNormalizeOptionsMiddleware } from './utilities/normalize-options-middleware';
3535

36-
export async function runCommand(args: string[], logger: logging.Logger): Promise<number> {
36+
export function parseInitialCommandArguments(args: string[]) {
3737
const {
3838
$0,
3939
_,
40-
help = false,
4140
dryRun = false,
4241
jsonHelp = false,
4342
getYargsCompletions = false,
4443
...rest
4544
} = yargsParser(args, {
46-
boolean: ['help', 'json-help', 'get-yargs-completions', 'dry-run'],
45+
// Do not treat bare `help` as the help flag; it can be a schematic argument (e.g. `ng g m help`).
46+
boolean: ['json-help', 'get-yargs-completions', 'dry-run'],
4747
alias: { 'collection': 'c' },
4848
});
4949

50+
const help =
51+
jsonHelp || args.some((arg) => arg === '--help' || arg === '-h' || arg.startsWith('--help='));
52+
5053
// When `getYargsCompletions` is true the scriptName 'ng' at index 0 is not removed.
5154
const positional = getYargsCompletions ? _.slice(1) : _;
5255

56+
return {
57+
$0,
58+
positional,
59+
help,
60+
dryRun,
61+
jsonHelp,
62+
getYargsCompletions,
63+
rest,
64+
};
65+
}
66+
67+
export async function runCommand(args: string[], logger: logging.Logger): Promise<number> {
68+
const { positional, help, dryRun, jsonHelp, getYargsCompletions, rest } =
69+
parseInitialCommandArguments(args);
70+
5371
let workspace: AngularWorkspace | undefined;
5472
let globalConfiguration: AngularWorkspace;
5573
try {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import { parseInitialCommandArguments } from './command-runner';
10+
11+
describe('parseInitialCommandArguments', () => {
12+
it('should not treat a schematic argument named help as the help flag', () => {
13+
const { help, positional } = parseInitialCommandArguments(['generate', 'm', 'help']);
14+
15+
expect(help).toBeFalse();
16+
expect(positional).toEqual(['generate', 'm', 'help']);
17+
});
18+
19+
it('should treat --help as the help flag', () => {
20+
const { help, positional } = parseInitialCommandArguments(['generate', 'm', '--help']);
21+
22+
expect(help).toBeTrue();
23+
expect(positional).toEqual(['generate', 'm']);
24+
});
25+
26+
it('should treat -h as the help flag', () => {
27+
const { help } = parseInitialCommandArguments(['generate', '-h']);
28+
29+
expect(help).toBeTrue();
30+
});
31+
32+
it('should treat --json-help as the help flag', () => {
33+
const { help, jsonHelp } = parseInitialCommandArguments(['generate', '--json-help']);
34+
35+
expect(jsonHelp).toBeTrue();
36+
expect(help).toBeTrue();
37+
});
38+
});

0 commit comments

Comments
 (0)