swift-argument-parser icon indicating copy to clipboard operation
swift-argument-parser copied to clipboard

Bug: Inconsistent visibility string interpolation in `generateHelpNames` for `.longWithSingleDash` vs `.long` names

Open AkshatRaj00 opened this issue 6 months ago • 2 comments

Bug Report

Description

In Sources/ArgumentParser/Usage/HelpGenerator.swift, the generateHelpNames(visibility:) method on NameSpecification handles the .long and .longWithSingleDash cases inconsistently when generating non-default visibility help flag names.

Affected file: Sources/ArgumentParser/Usage/HelpGenerator.swift

The Inconsistency

fileprivate func generateHelpNames(visibility: ArgumentVisibility) -> [Name] {
    self
      .makeNames(InputKey(name: "help", parent: nil))
      .compactMap { name in
        guard visibility.base != .default else { return name }
        switch name {
        case .long(let helpName):
          return .long("\(helpName)-\(visibility.base)")      // ✅ uses visibility.base
        case .longWithSingleDash(let helpName):
          return .longWithSingleDash("\(helpName)-\(visibility)") // ❌ uses visibility (full), not visibility.base
        case .short:
          return nil
        }
      }
      .sorted(by: >)
}
  • The .long case correctly appends visibility.base (e.g., "hidden")
  • The .longWithSingleDash case appends the full visibility description instead of visibility.base

This means if a command uses single-dash long names (e.g., -help), the generated hidden/private help flag name would be different from what the double-dash variant generates, causing inconsistent CLI behavior where -help-hidden does not behave the same as --help-hidden.

Steps to Reproduce

  1. Create a command that uses .longWithSingleDash in its helpNames configuration
  2. Call the command with a non-default visibility help flag
  3. Observe the appended suffix differs from the .long variant

Expected Behavior

Both .long and .longWithSingleDash cases should use visibility.base for consistent flag name generation:

case .longWithSingleDash(let helpName):
  return .longWithSingleDash("\(helpName)-\(visibility.base)") // ✅ fix: use .base

Environment

  • swift-argument-parser version: main branch (commit af51a49)
  • Swift version: 5.9+
  • Platform: All platforms

Impact

Low-to-medium. Affects only commands that configure helpNames with .longWithSingleDash at non-default visibility levels, but the inconsistency is a latent bug that could surface in toolchains and developer tools built on top of this library.


Reported after manual code review of HelpGenerator.swift.

AkshatRaj00 avatar May 23 '26 20:05 AkshatRaj00

I've submitted a fix for this in PR #902.

The change is a single-line correction — replacing visibility with visibility.base in the .longWithSingleDash case of generateHelpNames, making it consistent with the .long case that already correctly uses visibility.base.

Happy to add a test case for this if the team would like one before merging. Let me know!

AkshatRaj00 avatar May 23 '26 20:05 AkshatRaj00

I can reproduce this — taking it on. Opening a PR with the fix shortly.

aniketatgithub avatar Oct 06 '26 08:10 aniketatgithub