Skip to content

Commit 4f63cee

Browse files
authored
Hide hidden subcommands from completions (#443)
1 parent f3c9084 commit 4f63cee

File tree

4 files changed

+114
-0
lines changed

4 files changed

+114
-0
lines changed

Sources/ArgumentParser/Completions/BashCompletionsGenerator.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct BashCompletionsGenerator {
3535

3636
// Include 'help' in the list of subcommands for the root command.
3737
var subcommands = type.configuration.subcommands
38+
.filter { $0.configuration.shouldDisplay }
3839
if !subcommands.isEmpty && isRootCommand {
3940
subcommands.append(HelpCommand.self)
4041
}

Sources/ArgumentParser/Completions/FishCompletionsGenerator.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct FishCompletionsGenerator {
3030
let isRootCommand = commands.count == 1
3131
let programName = commandChain[0]
3232
var subcommands = type.configuration.subcommands
33+
.filter { $0.configuration.shouldDisplay }
3334

3435
if !subcommands.isEmpty {
3536
if isRootCommand {

Sources/ArgumentParser/Completions/ZshCompletionsGenerator.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct ZshCompletionsGenerator {
3737

3838
var args = generateCompletionArguments(commands)
3939
var subcommands = type.configuration.subcommands
40+
.filter { $0.configuration.shouldDisplay }
4041
var subcommandHandler = ""
4142
if !subcommands.isEmpty {
4243
args.append("'(-): :->command'")

Tests/ArgumentParserUnitTests/CompletionScriptTests.swift

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,114 @@ complete -c base -n '_swift_base_using_command base' -f -r -l path3
268268
complete -c base -n '_swift_base_using_command base --path3' -f -k -a 'a b c'
269269
complete -c base -n '_swift_base_using_command base' -f -s h -l help -d 'Show help information.'
270270
"""
271+
272+
// MARK: - Test Hidden Subcommand
273+
struct Parent: ParsableCommand {
274+
static var configuration = CommandConfiguration(subcommands: [HiddenChild.self])
275+
}
276+
277+
struct HiddenChild: ParsableCommand {
278+
static var configuration = CommandConfiguration(shouldDisplay: false)
279+
}
280+
281+
extension CompletionScriptTests {
282+
func testHiddenSubcommand_Zsh() throws {
283+
let script1 = try CompletionsGenerator(command: Parent.self, shell: .zsh)
284+
.generateCompletionScript()
285+
XCTAssertEqual(zshHiddenCompletion, script1)
286+
287+
let script2 = try CompletionsGenerator(command: Parent.self, shellName: "zsh")
288+
.generateCompletionScript()
289+
XCTAssertEqual(zshHiddenCompletion, script2)
290+
291+
let script3 = Parent.completionScript(for: .zsh)
292+
XCTAssertEqual(zshHiddenCompletion, script3)
293+
}
294+
295+
func testHiddenSubcommand_Bash() throws {
296+
let script1 = try CompletionsGenerator(command: Parent.self, shell: .bash)
297+
.generateCompletionScript()
298+
XCTAssertEqual(bashHiddenCompletion, script1)
299+
300+
let script2 = try CompletionsGenerator(command: Parent.self, shellName: "bash")
301+
.generateCompletionScript()
302+
XCTAssertEqual(bashHiddenCompletion, script2)
303+
304+
let script3 = Parent.completionScript(for: .bash)
305+
XCTAssertEqual(bashHiddenCompletion, script3)
306+
}
307+
308+
func testHiddenSubcommand_Fish() throws {
309+
let script1 = try CompletionsGenerator(command: Parent.self, shell: .fish)
310+
.generateCompletionScript()
311+
XCTAssertEqual(fishHiddenCompletion, script1)
312+
313+
let script2 = try CompletionsGenerator(command: Parent.self, shellName: "fish")
314+
.generateCompletionScript()
315+
XCTAssertEqual(fishHiddenCompletion, script2)
316+
317+
let script3 = Parent.completionScript(for: .fish)
318+
XCTAssertEqual(fishHiddenCompletion, script3)
319+
}
320+
}
321+
322+
let zshHiddenCompletion = """
323+
#compdef parent
324+
local context state state_descr line
325+
_parent_commandname=$words[1]
326+
typeset -A opt_args
327+
328+
_parent() {
329+
integer ret=1
330+
local -a args
331+
args+=(
332+
'(-h --help)'{-h,--help}'[Show help information.]'
333+
)
334+
_arguments -w -s -S $args[@] && ret=0
335+
336+
return ret
337+
}
338+
339+
340+
_custom_completion() {
341+
local completions=("${(@f)$($*)}")
342+
_describe '' completions
343+
}
344+
345+
_parent
346+
"""
347+
348+
let bashHiddenCompletion = """
349+
#!/bin/bash
350+
351+
_parent() {
352+
cur="${COMP_WORDS[COMP_CWORD]}"
353+
prev="${COMP_WORDS[COMP_CWORD-1]}"
354+
COMPREPLY=()
355+
opts="-h --help"
356+
if [[ $COMP_CWORD == "1" ]]; then
357+
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
358+
return
359+
fi
360+
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
361+
}
362+
363+
364+
complete -F _parent parent
365+
"""
366+
367+
let fishHiddenCompletion = """
368+
function _swift_parent_using_command
369+
set -l cmd (commandline -opc)
370+
if [ (count $cmd) -eq (count $argv) ]
371+
for i in (seq (count $argv))
372+
if [ $cmd[$i] != $argv[$i] ]
373+
return 1
374+
end
375+
end
376+
return 0
377+
end
378+
return 1
379+
end
380+
complete -c parent -n '_swift_parent_using_command parent' -f -s h -l help -d 'Show help information.'
381+
"""

0 commit comments

Comments
 (0)