Comment-based help examples now support optional titles#27387
Open
MariusStorhaug wants to merge 12 commits intoPowerShell:masterfrom
Open
Comment-based help examples now support optional titles#27387MariusStorhaug wants to merge 12 commits intoPowerShell:masterfrom
MariusStorhaug wants to merge 12 commits intoPowerShell:masterfrom
Conversation
…into feature/example-titles-in-comment-help
… tests - Revert CommentHelpInfo.Examples to ReadOnlyCollection<string>; add parallel ExampleTitles property per issue spec (non-breaking) - Update HelpCommentsParser to maintain parallel _exampleTitles list - Make ProxyCommand.ExtractExampleTitle culture-agnostic (anchor on dashes/colon, not the literal English word EXAMPLE) - Fix \n vs backtick-n in ProxyCommand round-trip tests - Add tests: line-comment titled examples, ExampleTitles count parity, title ending with dash, untitled-output regression
…is not blocked by an unrelated pre-existing failure in ScriptHelp.Tests.ps1
…fix and function-scope isolation
Author
|
If this is interesting, ill continue with the other repos to align those with the added feature. |
Contributor
There was a problem hiding this comment.
Pull request overview
Adds support for optional titles on comment-based help examples using the .EXAMPLE <Title> syntax, and preserves those titles through XML generation and proxy help comment round-tripping.
Changes:
- Extend comment-based help parsing and XML generation to capture/render optional
.EXAMPLEtitles. - Add a new
CommentHelpInfo.ExampleTitlesparallel collection and updateGetCommentBlock()to round-trip titled examples. - Update
ProxyCommand.GetHelpComments()to emit.EXAMPLE <title>when a user title can be recovered, and add Pester coverage for the new behavior.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
src/System.Management.Automation/help/HelpCommentsParser.cs |
Captures .EXAMPLE <title> in the parser and appends titles to the generated example heading. |
src/System.Management.Automation/engine/parser/ast.cs |
Adds CommentHelpInfo.ExampleTitles and updates GetCommentBlock() to emit .EXAMPLE <title> when present. |
src/System.Management.Automation/engine/ProxyCommand.cs |
Extracts user titles from decorated MAML headings and emits .EXAMPLE <title> during proxy help comment generation. |
test/powershell/Language/Scripting/ScriptHelp.ExampleTitles.Tests.ps1 |
New Pester coverage for titled .EXAMPLE parsing, round-trip via GetCommentBlock(), and backward compatibility. |
test/powershell/engine/Api/ProxyCommand.Tests.ps1 |
Adds tests asserting ProxyCommand.GetHelpComments() preserves/emits titled examples and handles edge cases. |
…ng comment in ExtractExampleTitle
…hen ProxyCommand title round-trip tests
- Updated the XML doc on ExtractExampleTitle to say ':' (matching the
IndexOf(':') implementation) instead of the misleading ': '.
- Changed the existing round-trip assertion from Should -Not -BeNullOrEmpty
(trivially true for all examples) to exact title equality comparison.
- Added a dedicated test 'ProxyCommand.GetHelpComments preserves custom
example titles' that defines a function with titled and untitled examples,
round-trips through GetHelpComments, and asserts the custom title text
survives.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Comment-based help examples can now carry an optional title using the
.EXAMPLE Title textsyntax, mirroring the inline pattern already used by.PARAMETER <Name>. Previously, putting any text on the same line as.EXAMPLEfell through to an unhandleddefault:case in the parser and silently discarded the entire help block — now that text is captured as the example's title and rendered alongside the auto-generated heading. Examples without titles continue to work exactly as before, so no existing help content is affected..EXAMPLEbody)New: Titled examples in comment-based help
Authors can now give each example a meaningful name on the same line as the
.EXAMPLEkeyword:Get-Help Show-Example -Examplesrenders titles alongside the auto-generated number, matching the format MAML-based help has always used for compiled cmdlets:Line-comment syntax (
# .EXAMPLE Title) is supported as well.New:
CommentHelpInfo.ExampleTitlespublic propertyExternal tools (PlatyPS, doc generators, formatters) can now read titles via a new public property on
CommentHelpInfo:ExampleTitlesruns in parallel with the existingExamplescollection — same length, same index mapping. An empty string entry means that example has no title.Round-trip preserved
Both serializers retain titles when regenerating comment-based help:
CommentHelpInfo.GetCommentBlock()emits.EXAMPLE <title>when the title is non-empty, and.EXAMPLEon its own line when empty.ProxyCommand.GetHelpComments()reconstructs the user-supplied title from the decorated MAML heading via a new culture-agnostic helper (ExtractExampleTitle) that anchors on the dash-padding and:separator rather than the literal English wordEXAMPLE— so it works under any UI culture.Backward compatibility
Fully backward compatible:
.EXAMPLEwith no trailing text continues to behave exactly as before.CommentHelpInfo.Examplesproperty is unchanged (stillReadOnlyCollection<string>of bodies).ExampleTitlesis purely additive..EXAMPLE <text>no longer breaks the help block.Out of scope
These follow-ups are tracked in the linked issue and need separate work in other repositories:
about_Comment_Based_Helpand the developer authoring guide in MicrosoftDocs/PowerShell-DocsExampleTitlesproperty (#627).EXAMPLE <Title>is recognized as a directive keyword the same way.PARAMETER <Name>isTechnical Details
Files changed:
src/System.Management.Automation/help/HelpCommentsParser.cs— Added a newcase "EXAMPLE"to theGroups[3].Success == truebranch ofAnalyzeCommentBlockthat capturesmatch.Groups[3].Value.Trim()into a new parallelList<string> _exampleTitlesfield, then collects the body viaGetSection(). The existingcase "EXAMPLE"in the no-arguments branch appends an empty string to_exampleTitlesto keep the title and body indices aligned. The XML-building section conditionally appends: <title>to theEXAMPLE Nheading when the title is non-empty. After parsing,_sections.ExampleTitlesis assigned as aReadOnlyCollection<string>.src/System.Management.Automation/engine/parser/ast.cs— Added the newReadOnlyCollection<string> ExampleTitles { get; internal set; }property onCommentHelpInfo. UpdatedGetCommentBlock()to emit.EXAMPLE <title>whenExampleTitles[index]is non-empty, and.EXAMPLEon its own line otherwise. The existingExamplesproperty type is unchanged — the design is intentionally a parallel collection, not a redesign ofExamples, so the public API stays binary- and source-compatible.src/System.Management.Automation/engine/ProxyCommand.cs— UpdatedGetHelpCommentsto emit.EXAMPLE <title>when the underlying MAML title contains a user-supplied portion. Added the privateExtractExampleTitlehelper that recovers the user title from the decorated MAML heading by anchoring on the dash-padding and the:separator, deliberately avoiding the literal wordEXAMPLEso the logic remains correct under non-English UI cultures.test/powershell/Language/Scripting/ScriptHelp.Tests.ps1— Added Pester coverage for: titled examples (verifying$help.examples.example.title), mixed titled/untitled examples, untitled-output regression (identical to current rendering),GetCommentBlock()round-tripping, regression test for the previousdefault: return falseparser bug, line-comment syntax (# .EXAMPLE Title), edge cases (titles containing colons and dashes, titles ending with a dash), and a parity assertion thatExamples.Count == ExampleTitles.Count.test/powershell/engine/Api/ProxyCommand.Tests.ps1— Added round-trip tests forProxyCommand.GetHelpCommentswith titled examples.Implementation plan progress (from #23966):
ExampleTitlescollection; non-breaking)GetCommentBlock+ProxyCommand.GetHelpComments+ culture-agnosticExtractExampleTitle) — doneDesign history. An earlier revision changed
CommentHelpInfo.ExamplestoReadOnlyCollection<KeyValuePair<string, string>>to carry title and body together. That was reverted in commit38f9720based on review feedback to keep the public API non-breaking — the parallelExampleTitlescollection delivers the same capability without altering the existingExamplessignature.PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header