Skip to content
Open
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
9 changes: 9 additions & 0 deletions apps/spfx-cli/src/cli/actions/CreateAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
];
}
Comment on lines +273 to +280
}

/**
Expand Down
33 changes: 33 additions & 0 deletions apps/spfx-cli/src/cli/actions/SPFxActionBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Comment on lines +183 to +188

// 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('');
Comment on lines +190 to +199
return lines.join('\n');
}
}
10 changes: 10 additions & 0 deletions common/changes/@microsoft/spfx-cli/help-examples_2026-07-13.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft/spfx-cli",
"comment": "",
"type": "none"
}
],
"packageName": "@microsoft/spfx-cli"
}