Skip to content

[duplicate-code] Duplicate Code: Command-line --option value lookup (TryGetOptionValue) reimplemented in PackagedApp project #11569

Closed

Description

@github-actions

Summary

Two nearly-identical private helpers named TryGetOptionValue(IReadOnlyList<string> arguments, string option) — both scanning a command-line argument array for --option value or --option=value style entries — exist in the same Microsoft.Testing.Extensions.PackagedApp project, with subtly different option-name conventions (bare option string vs. a ---prefixed key built from a bare optionKey).

Duplication Details

Pattern: Duplicated command-line option scanning helper

  • Severity: Medium

  • Occurrences: 2 (plus a third related helper, TryGetInlineOptionValue, only in one of the two files)

  • Locations:

    • src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppTestHostLauncher.cs (lines ~731-747), TryGetOptionValue, which also delegates to a sibling TryGetInlineOptionValue (lines ~770+) to support --option=value syntax. Used for ResultsDirectoryOption / DiagnosticOutputDirectoryOption lookups added in Enable MTP for UWP and AppContainer Windows apps #11484.
    • src/Platform/Microsoft.Testing.Extensions.PackagedApp/PackagedAppConnectBackHandshake.cs (lines ~104-113), TryGetOptionValue, a simpler variant (no inline =value support, and it prefixes the key with -- internally) used for TestHostControllerPidOptionKey / RetryPipeNameOptionKey lookups.
  • Code Sample (PackagedAppConnectBackHandshake.cs):

    private static string? TryGetOptionValue(IReadOnlyList<string> arguments, string optionKey)
    {
        for (int i = 0; i < arguments.Count - 1; i++)
        {
            if (string.Equals(arguments[i], $"--{optionKey}", StringComparison.Ordinal))
            {
                return arguments[i + 1];
            }
        }
    
        return null;
    }

    vs. (PackagedAppTestHostLauncher.cs):

    private static string? TryGetOptionValue(IReadOnlyList<string> arguments, string option)
    {
        for (int i = 0; i < arguments.Count; i++)
        {
            if (string.Equals(arguments[i], option, StringComparison.Ordinal))
            {
                return i + 1 < arguments.Count ? arguments[i + 1] : null;
            }
    
            if (TryGetInlineOptionValue(arguments[i], option, out string? value))
            {
                return value;
            }
        }
    
        return null;
    }

Impact Analysis

  • Maintainability: Two subtly different argument-parsing conventions live in the same project: one caller passes the bare option name and expects the callee to add --, the other expects the full --option string and additionally supports inline =value syntax. This inconsistency itself is a maintenance hazard — a developer copying one call pattern to the other file will silently break lookups (e.g. double -- prefix, or missing inline-value support).
  • Bug Risk: PackagedAppConnectBackHandshake.TryGetOptionValue does not support --option=value syntax that PackagedAppTestHostLauncher.TryGetOptionValue does. If a future option (e.g. --internal-testhostcontroller-pid=1234) is ever passed in the =value form, the handshake lookup will silently fail to find it while the launcher's lookup would succeed — a hard-to-diagnose inconsistency.
  • Code Bloat: ~35 combined lines for what is fundamentally one command-line parsing primitive.

Refactoring Recommendations

  1. Extract a single shared CommandLineArgumentHelper
    • Add one internal static helper (e.g. CommandLineOptionParser.TryGetOptionValue(IReadOnlyList<string> arguments, string option)) supporting both --option value and --option=value forms, take a fully-prefixed option string as input (matching the more capable PackagedAppTestHostLauncher convention), and have both PackagedAppTestHostLauncher and PackagedAppConnectBackHandshake call it, updating call sites to pass ---prefixed keys consistently.
    • Estimated effort: Low (~1 hour), including updating call sites and existing unit tests (PackagedAppConnectBackHandshakeTests.cs, PackagedAppTestHostLauncherTests.cs).
    • Benefits: One parsing convention repo-wide, inline =value support becomes available everywhere, eliminates the risk of the two helpers silently diverging further.

Implementation Checklist

  • Review duplication findings
  • Prioritize refactoring tasks
  • Create refactoring plan
  • Implement changes
  • Update tests
  • Verify no functionality broken

Analysis Metadata

  • Analyzed Files: 2 (PackagedAppTestHostLauncher.cs, PackagedAppConnectBackHandshake.cs)
  • Detection Method: Semantic code analysis of command-line option parsing helpers
  • Commit: 7287c75 (HEAD at analysis time)

🤖 Automated content by GitHub Copilot. Generated by the Duplicate Code Detector workflow. · copilot · auto · 100.8 AIC · ⌖ 7.37 AIC · ⊞ 13.4K · [◷]( · ◷)

Add this agentic workflow to your repo

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/duplicate-code-detector.md@main
  • expires on Sep 30, 2026, 4:38 AM UTC

Metadata

Metadata

Labels

type/automationCreated or maintained by an agentic workflow.type/tech-debtCode health, refactoring, simplification.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions