Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide hidden subcommands from completions #443

Merged
merged 1 commit into from Apr 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -35,6 +35,7 @@ struct BashCompletionsGenerator {

// Include 'help' in the list of subcommands for the root command.
var subcommands = type.configuration.subcommands
.filter { $0.configuration.shouldDisplay }
if !subcommands.isEmpty && isRootCommand {
subcommands.append(HelpCommand.self)
}
Expand Down
Expand Up @@ -30,6 +30,7 @@ struct FishCompletionsGenerator {
let isRootCommand = commands.count == 1
let programName = commandChain[0]
var subcommands = type.configuration.subcommands
.filter { $0.configuration.shouldDisplay }

if !subcommands.isEmpty {
if isRootCommand {
Expand Down
Expand Up @@ -37,6 +37,7 @@ struct ZshCompletionsGenerator {

var args = generateCompletionArguments(commands)
var subcommands = type.configuration.subcommands
.filter { $0.configuration.shouldDisplay }
var subcommandHandler = ""
if !subcommands.isEmpty {
args.append("'(-): :->command'")
Expand Down
111 changes: 111 additions & 0 deletions Tests/ArgumentParserUnitTests/CompletionScriptTests.swift
Expand Up @@ -268,3 +268,114 @@ complete -c base -n '_swift_base_using_command base' -f -r -l path3
complete -c base -n '_swift_base_using_command base --path3' -f -k -a 'a b c'
complete -c base -n '_swift_base_using_command base' -f -s h -l help -d 'Show help information.'
"""

// MARK: - Test Hidden Subcommand
struct Parent: ParsableCommand {
static var configuration = CommandConfiguration(subcommands: [HiddenChild.self])
}

struct HiddenChild: ParsableCommand {
static var configuration = CommandConfiguration(shouldDisplay: false)
}

extension CompletionScriptTests {
func testHiddenSubcommand_Zsh() throws {
let script1 = try CompletionsGenerator(command: Parent.self, shell: .zsh)
.generateCompletionScript()
XCTAssertEqual(zshHiddenCompletion, script1)

let script2 = try CompletionsGenerator(command: Parent.self, shellName: "zsh")
.generateCompletionScript()
XCTAssertEqual(zshHiddenCompletion, script2)

let script3 = Parent.completionScript(for: .zsh)
XCTAssertEqual(zshHiddenCompletion, script3)
}

func testHiddenSubcommand_Bash() throws {
let script1 = try CompletionsGenerator(command: Parent.self, shell: .bash)
.generateCompletionScript()
XCTAssertEqual(bashHiddenCompletion, script1)

let script2 = try CompletionsGenerator(command: Parent.self, shellName: "bash")
.generateCompletionScript()
XCTAssertEqual(bashHiddenCompletion, script2)

let script3 = Parent.completionScript(for: .bash)
XCTAssertEqual(bashHiddenCompletion, script3)
}

func testHiddenSubcommand_Fish() throws {
let script1 = try CompletionsGenerator(command: Parent.self, shell: .fish)
.generateCompletionScript()
XCTAssertEqual(fishHiddenCompletion, script1)

let script2 = try CompletionsGenerator(command: Parent.self, shellName: "fish")
.generateCompletionScript()
XCTAssertEqual(fishHiddenCompletion, script2)

let script3 = Parent.completionScript(for: .fish)
XCTAssertEqual(fishHiddenCompletion, script3)
}
}

let zshHiddenCompletion = """
#compdef parent
local context state state_descr line
_parent_commandname=$words[1]
typeset -A opt_args

_parent() {
integer ret=1
local -a args
args+=(
'(-h --help)'{-h,--help}'[Show help information.]'
)
_arguments -w -s -S $args[@] && ret=0

return ret
}


_custom_completion() {
local completions=("${(@f)$($*)}")
_describe '' completions
}

_parent
"""

let bashHiddenCompletion = """
#!/bin/bash

_parent() {
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
COMPREPLY=()
opts="-h --help"
if [[ $COMP_CWORD == "1" ]]; then
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
return
fi
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
}


complete -F _parent parent
"""

let fishHiddenCompletion = """
function _swift_parent_using_command
set -l cmd (commandline -opc)
if [ (count $cmd) -eq (count $argv) ]
for i in (seq (count $argv))
if [ $cmd[$i] != $argv[$i] ]
return 1
end
end
return 0
end
return 1
end
complete -c parent -n '_swift_parent_using_command parent' -f -s h -l help -d 'Show help information.'
"""