apple / swift-argument-parser

Straightforward, type-safe argument parsing for Swift
Apache License 2.0
3.34k stars 320 forks source link

Custom fish completions do not work #376

Open floam opened 2 years ago

floam commented 2 years ago

I was looking over some issues, and for fun cloned https://gitlab.com/frizlab-demo-projects/swift-argument-parser-example-for-pr-320 which was a test case for a problem that affected bash and not zsh for issue #320. Was just interested in verifying it had the correct behavior for fish. This is the first project using argument-parser I've compiled. It seems custom completions are not generated for fish at all. I expect there to be something executing ---completion … in the output?

$ example --generate-completion-script=fish
function __fish_example_using_command
    set 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 example -n '__fish_example_using_command example' -f -r -l option1

$ example --generate-completion-script=zsh
#compdef example
local context state state_descr line
_example_commandname=$words[1]
typeset -A opt_args

_example() {
    integer ret=1
    local -a args
    args+=(
        '--option1:option1:'
        ':arg:{_custom_completion $_example_commandname ---completion  -- arg $words}'
        '(-h --help)'{-h,--help}'[Print help information.]'
    )
    _arguments -w -s -S $args[@] && ret=0

    return ret
}

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

_example
natecook1000 commented 2 years ago

I’ve just gotten to the same point when investigating why the shortcuts tool doesn’t provide the kind of autocompletions you had suggested in your previous issue. It looks like the fish completions leave out positional arguments entirely, and only include completions for flags and options - not ideal!

floam commented 2 years ago

I ought to learn Swift before I get too dangerous making code changes in a shipping product.

rauhul commented 2 years ago

@floam if you describe the changes needed in detail, I (if I can find time) or another contributor could implement them.

floam commented 2 years ago

I'm a hacker and degenerate procrastinator. I'll probably fix it all up anyhow or disappear. But I'll keep your offer in mind! I'll try to take a look at this soon.

hassila commented 2 years ago

I made a simple go at using the fish completion too now and couldn't figure out what I was doing wrong yet.

Just a small test project for testing some stuff:

hassila@max ~/D/G/d/.b/debug (main)> ./dm --help
USAGE: dm [--generate-flatbuffers] [--generate-fast-binary-encoding]

OPTIONS:
  --generate-flatbuffers  Generate Flatbuffers schema.
  --generate-fast-binary-encoding
                          Generate Fast Binary Encoding schema.
  -h, --help              Show help information.

hassila@max ~/D/G/dm (main)> swift run dm --generate-completion-script fish
Building for debugging...
Build complete! (0.06s)
function _swift_dm_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 dm -n '_swift_dm_using_command dm' -f -l generate-flatbuffers -d 'Generate Flatbuffers schema.'
complete -c dm -n '_swift_dm_using_command dm' -f -l generate-fast-binary-encoding -d 'Generate Fast Binary Encoding schema.'
complete -c dm -n '_swift_dm_using_command dm' -f -s h -l help -d 'Show help information.'

Tried manually but didn't work as expected. @floam - thanks for your contributions to fish, enjoy using it, echoing @rauhul comment and would be happy to try to help out if you can articulate what's needed here - would be great to get it running.