# Print an optspec for argparse to handle cmd's options that are independent of any subcommand.
function __fish_bear_global_optspecs
    string join \n c/config= o/output= a/append h/help V/version
end

function __fish_bear_needs_command
    # Figure out if the current invocation already has a command.
    set -l cmd (commandline -opc)
    set -e cmd[1]
    argparse -s (__fish_bear_global_optspecs) -- $cmd 2>/dev/null
    or return
    if set -q argv[1]
        # Also print the command, so this can be used to figure out what it is.
        echo $argv[1]
        return 1
    end
    return 0
end

function __fish_bear_using_subcommand
    set -l cmd (__fish_bear_needs_command)
    test -z "$cmd"
    and return 1
    contains -- $cmd[1] $argv
end

complete -c bear -n "__fish_bear_needs_command" -s c -l config -d 'Path of the config file' -r
complete -c bear -n "__fish_bear_needs_command" -s o -l output -d 'Path of the result file' -r
complete -c bear -n "__fish_bear_needs_command" -s a -l append -d 'Append result to an existing output file'
complete -c bear -n "__fish_bear_needs_command" -s h -l help -d 'Print help'
complete -c bear -n "__fish_bear_needs_command" -s V -l version -d 'Print version'
complete -c bear -n "__fish_bear_needs_command" -a "intercept" -d 'intercepts command execution'
complete -c bear -n "__fish_bear_needs_command" -a "semantic" -d 'detect semantics of command executions'
complete -c bear -n "__fish_bear_needs_command" -a "parse-sh" -d 'produces the compilation database from build-system dry-run text (e.g. `make -n` output)'
complete -c bear -n "__fish_bear_needs_command" -a "help" -d 'Print this message or the help of the given subcommand(s)'
complete -c bear -n "__fish_bear_using_subcommand intercept" -s o -l output -d 'Path of the event file to write' -r
complete -c bear -n "__fish_bear_using_subcommand intercept" -s h -l help -d 'Print help'
complete -c bear -n "__fish_bear_using_subcommand semantic" -s i -l input -d 'Path of the event file to read' -r
complete -c bear -n "__fish_bear_using_subcommand semantic" -s o -l output -d 'Path of the result file' -r
complete -c bear -n "__fish_bear_using_subcommand semantic" -s a -l append -d 'Append result to an existing output file'
complete -c bear -n "__fish_bear_using_subcommand semantic" -l print-compilers -d 'Print the compilers Bear recognizes and exit'
complete -c bear -n "__fish_bear_using_subcommand semantic" -s h -l help -d 'Print help'
complete -c bear -n "__fish_bear_using_subcommand parse-sh" -s i -l input -d 'Path of the shell text to parse' -r
complete -c bear -n "__fish_bear_using_subcommand parse-sh" -s o -l output -d 'Path of the result file' -r
complete -c bear -n "__fish_bear_using_subcommand parse-sh" -s C -l directory -d 'Initial working directory for the parsed commands' -r
complete -c bear -n "__fish_bear_using_subcommand parse-sh" -s a -l append -d 'Append result to an existing output file'
complete -c bear -n "__fish_bear_using_subcommand parse-sh" -s h -l help -d 'Print help'
complete -c bear -n "__fish_bear_using_subcommand help; and not __fish_seen_subcommand_from intercept semantic parse-sh help" -f -a "intercept" -d 'intercepts command execution'
complete -c bear -n "__fish_bear_using_subcommand help; and not __fish_seen_subcommand_from intercept semantic parse-sh help" -f -a "semantic" -d 'detect semantics of command executions'
complete -c bear -n "__fish_bear_using_subcommand help; and not __fish_seen_subcommand_from intercept semantic parse-sh help" -f -a "parse-sh" -d 'produces the compilation database from build-system dry-run text (e.g. `make -n` output)'
complete -c bear -n "__fish_bear_using_subcommand help; and not __fish_seen_subcommand_from intercept semantic parse-sh help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
