diff --git a/apps/spfx-cli/src/cli/actions/CreateAction.ts b/apps/spfx-cli/src/cli/actions/CreateAction.ts index 8da46af7..4802405c 100644 --- a/apps/spfx-cli/src/cli/actions/CreateAction.ts +++ b/apps/spfx-cli/src/cli/actions/CreateAction.ts @@ -269,6 +269,15 @@ export class CreateAction extends SPFxActionBase { throw error; } } + + protected override getExamples(): string[] { + return [ + 'Scaffold a new React web part for SharePoint:\n spfx create --template webpart-react --component-name "HelloWorld" --library-name "helloworld-library" --solution-name "helloworld" --component-description "A Hello World web part"', + 'Scaffold a field customizer extension using pnpm for dependency installation:\n spfx create --template extension-field --component-name "ColorField" --package-manager pnpm', + 'Scaffold a web part without running npm install, specifying a local template source:\n spfx create --template webpart-react --component-name "MyWebPart" --library-name "mywebpart" --solution-name "mywebpart" --local-source ./my-templates', + 'Scaffold a web part for a specific SPFx version, writing output to a custom directory:\n spfx create --template webpart-react --component-name "MyWP" --component-description "Description" --spfx-version 1.22 --target-dir ./my-webpart' + ]; + } } /** diff --git a/apps/spfx-cli/src/cli/actions/SPFxActionBase.ts b/apps/spfx-cli/src/cli/actions/SPFxActionBase.ts index d6ca0250..6fb9dddb 100644 --- a/apps/spfx-cli/src/cli/actions/SPFxActionBase.ts +++ b/apps/spfx-cli/src/cli/actions/SPFxActionBase.ts @@ -166,4 +166,37 @@ export abstract class SPFxActionBase extends CommandLineAction { } } } + + /** + * Returns usage examples for this action. Each string is a self-contained example + * with the command invocation and a brief description of what it does. + * Override in subclasses to provide examples. The default returns an empty array. + */ + protected getExamples(): string[] { + return []; + } + + /** + * Overrides the base help text rendering to append a formatted EXAMPLES section + * when examples are defined. + */ + public override renderHelpText(): string { + const base: string = super.renderHelpText(); + const examples: string[] = this.getExamples(); + if (examples.length === 0) { + return base; + } + + // Append examples after the last line of base help + // Normalize Windows CRLF to LF before splitting so every line is clean + const lines: string[] = base.replace(/\r\n/g, '\n').split('\n'); + lines.push(''); + lines.push('EXAMPLES'); + for (const example of examples) { + lines.push(''); + lines.push(` ${example}`); + } + lines.push(''); + return lines.join('\n'); + } } diff --git a/common/changes/@microsoft/spfx-cli/help-examples_2026-07-13.json b/common/changes/@microsoft/spfx-cli/help-examples_2026-07-13.json new file mode 100644 index 00000000..a0a328ef --- /dev/null +++ b/common/changes/@microsoft/spfx-cli/help-examples_2026-07-13.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@microsoft/spfx-cli", + "comment": "", + "type": "none" + } + ], + "packageName": "@microsoft/spfx-cli" +}