Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions packages/angular/cli/src/command-builder/command-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,41 @@ import {
import { jsonHelpUsage } from './utilities/json-help';
import { createNormalizeOptionsMiddleware } from './utilities/normalize-options-middleware';

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

const help =
jsonHelp || args.some((arg) => arg === '--help' || arg === '-h' || arg.startsWith('--help='));
Comment on lines +50 to +51

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Checking args.some(...) directly on the raw args array will incorrectly detect --help or -h even if they are specified after the -- (double dash) option delimiter. In CLI tools, arguments after -- are meant to be passed raw to the underlying command/schematic/script and should not be parsed as options for the outer CLI.\n\nFor example, running ng run my-project -- --help or ng test -- --help would incorrectly trigger the global Angular CLI help instead of passing --help to the builder or test runner.\n\nTo fix this, we should only scan the arguments that appear before --.

  const dashDashIndex = args.indexOf('--');\n  const argsToParse = dashDashIndex === -1 ? args : args.slice(0, dashDashIndex);\n  const help =\n    jsonHelp || argsToParse.some((arg) => arg === '--help' || arg === '-h' || arg.startsWith('--help='));


// When `getYargsCompletions` is true the scriptName 'ng' at index 0 is not removed.
const positional = getYargsCompletions ? _.slice(1) : _;

return {
$0,
positional,
help,
dryRun,
jsonHelp,
getYargsCompletions,
rest,
};
}

export async function runCommand(args: string[], logger: logging.Logger): Promise<number> {
const { positional, help, dryRun, jsonHelp, getYargsCompletions, rest } =
parseInitialCommandArguments(args);

let workspace: AngularWorkspace | undefined;
let globalConfiguration: AngularWorkspace;
try {
Expand Down
38 changes: 38 additions & 0 deletions packages/angular/cli/src/command-builder/command-runner_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/

import { parseInitialCommandArguments } from './command-runner';

describe('parseInitialCommandArguments', () => {
it('should not treat a schematic argument named help as the help flag', () => {
const { help, positional } = parseInitialCommandArguments(['generate', 'm', 'help']);

expect(help).toBeFalse();
expect(positional).toEqual(['generate', 'm', 'help']);
});

it('should treat --help as the help flag', () => {
const { help, positional } = parseInitialCommandArguments(['generate', 'm', '--help']);

expect(help).toBeTrue();
expect(positional).toEqual(['generate', 'm']);
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It would be beneficial to add a test case to verify that --help is not treated as a help flag when it is passed after the -- delimiter.

  });\n\n  it('should not treat --help after -- as the help flag', () => {\n    const { help } = parseInitialCommandArguments(['run', 'my-project', '--', '--help']);\n\n    expect(help).toBeFalse();\n  });


it('should treat -h as the help flag', () => {
const { help } = parseInitialCommandArguments(['generate', '-h']);

expect(help).toBeTrue();
});

it('should treat --json-help as the help flag', () => {
const { help, jsonHelp } = parseInitialCommandArguments(['generate', '--json-help']);

expect(jsonHelp).toBeTrue();
expect(help).toBeTrue();
});
});
Loading