From 6ff935729e1c4157e52d4a16bcfa5fd795541a6f Mon Sep 17 00:00:00 2001 From: Jared Grubb Date: Sat, 17 May 2025 13:12:20 -0700 Subject: [PATCH] Bash Completion: fix bug in dash vs underscore (#780) The generated script tries to call sub-functions that have hyphens in them instead of underscores. --- CHANGELOG.md | 4 +++- .../ArgumentParser/Completions/BashCompletionsGenerator.swift | 2 +- .../Snapshots/testMathBashCompletionScript().bash | 4 ++-- Tests/ArgumentParserUnitTests/Snapshots/testBase_Bash().bash | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e3a7cb37..b594ad2d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,9 @@ Add new items at the end of the relevant section under **Unreleased**. ## [Unreleased] -*No new changes.* +### Fixes + +- Bash completion scripts correctly handles subcommands with hyphens. ([#780]) --- diff --git a/Sources/ArgumentParser/Completions/BashCompletionsGenerator.swift b/Sources/ArgumentParser/Completions/BashCompletionsGenerator.swift index 216884f7f..630e7326f 100644 --- a/Sources/ArgumentParser/Completions/BashCompletionsGenerator.swift +++ b/Sources/ArgumentParser/Completions/BashCompletionsGenerator.swift @@ -290,7 +290,7 @@ extension [ParsableCommand.Type] { case "${subcommand}" in \(subcommands.map { $0._commandName }.joined(separator: "|"))) # Offer subcommand argument completions - "\(functionName)_${subcommand}" + "\(functionName)_${subcommand//-/_}" ;; *) # Offer subcommand completions diff --git a/Tests/ArgumentParserExampleTests/Snapshots/testMathBashCompletionScript().bash b/Tests/ArgumentParserExampleTests/Snapshots/testMathBashCompletionScript().bash index 130f09334..64e7eb9c3 100644 --- a/Tests/ArgumentParserExampleTests/Snapshots/testMathBashCompletionScript().bash +++ b/Tests/ArgumentParserExampleTests/Snapshots/testMathBashCompletionScript().bash @@ -169,7 +169,7 @@ _math() { case "${subcommand}" in add|multiply|stats|help) # Offer subcommand argument completions - "_math_${subcommand}" + "_math_${subcommand//-/_}" ;; *) # Offer subcommand completions @@ -202,7 +202,7 @@ _math_stats() { case "${subcommand}" in average|stdev|quantiles) # Offer subcommand argument completions - "_math_stats_${subcommand}" + "_math_stats_${subcommand//-/_}" ;; *) # Offer subcommand completions diff --git a/Tests/ArgumentParserUnitTests/Snapshots/testBase_Bash().bash b/Tests/ArgumentParserUnitTests/Snapshots/testBase_Bash().bash index 32d67dc9e..421c6ac54 100644 --- a/Tests/ArgumentParserUnitTests/Snapshots/testBase_Bash().bash +++ b/Tests/ArgumentParserUnitTests/Snapshots/testBase_Bash().bash @@ -214,7 +214,7 @@ _base_test() { case "${subcommand}" in sub-command|escaped-command|help) # Offer subcommand argument completions - "_base_test_${subcommand}" + "_base_test_${subcommand//-/_}" ;; *) # Offer subcommand completions