# Print an optspec for argparse to handle cmd's options that are independent of any subcommand.
function __fish_vmexec_global_optspecs
    string join \n log-level= h/help V/version
end

function __fish_vmexec_needs_command
    # Figure out if the current invocation already has a command.
    set -l cmd (commandline -opc)
    set -e cmd[1]
    argparse -s (__fish_vmexec_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_vmexec_using_subcommand
    set -l cmd (__fish_vmexec_needs_command)
    test -z "$cmd"
    and return 1
    contains -- $cmd[1] $argv
end

complete -c vmexec -n "__fish_vmexec_needs_command" -l log-level -d 'Log messages above specified level (error, warn, info, debug, trace)' -r
complete -c vmexec -n "__fish_vmexec_needs_command" -s h -l help -d 'Print help'
complete -c vmexec -n "__fish_vmexec_needs_command" -s V -l version -d 'Print version'
complete -c vmexec -n "__fish_vmexec_needs_command" -f -a "ps" -d 'List virtual machines'
complete -c vmexec -n "__fish_vmexec_needs_command" -f -a "inspect" -d 'Display detailed information on a virtual machine'
complete -c vmexec -n "__fish_vmexec_needs_command" -f -a "images" -d 'List all available OS images'
complete -c vmexec -n "__fish_vmexec_needs_command" -f -a "logs" -d 'Show terminal log of an existing virtual machine'
complete -c vmexec -n "__fish_vmexec_needs_command" -f -a "stop" -d 'Stop a virtual machine by sending a SIGTERM signal'
complete -c vmexec -n "__fish_vmexec_needs_command" -f -a "exec" -d 'Run a command in an existing virtual machine'
complete -c vmexec -n "__fish_vmexec_needs_command" -f -a "pull" -d 'Pull an image without running a virtual machine'
complete -c vmexec -n "__fish_vmexec_needs_command" -f -a "run" -d 'Run a command in a new virtual machine'
complete -c vmexec -n "__fish_vmexec_needs_command" -f -a "ksm" -d 'Check and change KSM status'
complete -c vmexec -n "__fish_vmexec_needs_command" -f -a "clean" -d 'Clean up old virtual machines'
complete -c vmexec -n "__fish_vmexec_needs_command" -f -a "prune" -d 'Delete all overlay images from the cache'
complete -c vmexec -n "__fish_vmexec_needs_command" -f -a "ready" -d 'Check if a virtual machine is ready to accept SSH connections'
complete -c vmexec -n "__fish_vmexec_needs_command" -f -a "completions" -d 'Generate completion file for a shell'
complete -c vmexec -n "__fish_vmexec_needs_command" -f -a "manpage" -d 'Print man page'
complete -c vmexec -n "__fish_vmexec_needs_command" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
complete -c vmexec -n "__fish_vmexec_using_subcommand ps" -s a -l all -d 'Show all the virtual machines, default is only running virtual machines'
complete -c vmexec -n "__fish_vmexec_using_subcommand ps" -s h -l help -d 'Print help'
complete -c vmexec -n "__fish_vmexec_using_subcommand inspect" -s h -l help -d 'Print help'
complete -c vmexec -n "__fish_vmexec_using_subcommand images" -s h -l help -d 'Print help'
complete -c vmexec -n "__fish_vmexec_using_subcommand logs" -s f -l follow -d 'Follow log output'
complete -c vmexec -n "__fish_vmexec_using_subcommand logs" -s h -l help -d 'Print help'
complete -c vmexec -n "__fish_vmexec_using_subcommand stop" -s h -l help -d 'Print help'
complete -c vmexec -n "__fish_vmexec_using_subcommand exec" -s e -l env -d 'Set environment variables for the process inside the virtual machine' -r
complete -c vmexec -n "__fish_vmexec_using_subcommand exec" -s w -l workdir -d 'Working directory inside the virtual machine' -r -F
complete -c vmexec -n "__fish_vmexec_using_subcommand exec" -s s -l ssh-timeout -d 'SSH connection timeout' -r
complete -c vmexec -n "__fish_vmexec_using_subcommand exec" -s i -l interactive -d 'Make STDIN available to the virtual machine\'s process' -r -f -a "always\t''
never\t''
auto\t''"
complete -c vmexec -n "__fish_vmexec_using_subcommand exec" -s t -l tty -d 'Allocate a pseudo-TTY for the virtual machine' -r -f -a "always\t''
never\t''
auto\t''"
complete -c vmexec -n "__fish_vmexec_using_subcommand exec" -s h -l help -d 'Print help (see more with \'--help\')'
complete -c vmexec -n "__fish_vmexec_using_subcommand pull" -s m -l memory -d 'Memory limit for the warmup virtual machine in GBs' -r
complete -c vmexec -n "__fish_vmexec_using_subcommand pull" -s s -l ssh-timeout -d 'SSH connection timeout during warmup' -r
complete -c vmexec -n "__fish_vmexec_using_subcommand pull" -l prepare -d 'Additionally prepare the image so that the first run is fast'
complete -c vmexec -n "__fish_vmexec_using_subcommand pull" -l disable-kvm -d 'Run the warmup virtual machine with TCG instead of KVM'
complete -c vmexec -n "__fish_vmexec_using_subcommand pull" -l show-vm-window -d 'Show a window with the warmup virtual machine running in it'
complete -c vmexec -n "__fish_vmexec_using_subcommand pull" -s h -l help -d 'Print help (see more with \'--help\')'
complete -c vmexec -n "__fish_vmexec_using_subcommand run" -l vmid -d 'Virtual machine identifier' -r
complete -c vmexec -n "__fish_vmexec_using_subcommand run" -s m -l memory -d 'Memory limit for the virtual machine in GBs' -r
complete -c vmexec -n "__fish_vmexec_using_subcommand run" -s e -l env -d 'Set environment variables for the process inside the virtual machine' -r
complete -c vmexec -n "__fish_vmexec_using_subcommand run" -s w -l workdir -d 'Working directory inside the virtual machine' -r -F
complete -c vmexec -n "__fish_vmexec_using_subcommand run" -s v -l volume -d 'Bind mount a volume into the virtual machine' -r
complete -c vmexec -n "__fish_vmexec_using_subcommand run" -l pmem -d 'Mount a virtio-pmem device file into the virtual machine' -r
complete -c vmexec -n "__fish_vmexec_using_subcommand run" -s p -l publish -d 'Publish a port on the virtual machine to the host' -r
complete -c vmexec -n "__fish_vmexec_using_subcommand run" -s s -l ssh-timeout -d 'SSH connection timeout' -r
complete -c vmexec -n "__fish_vmexec_using_subcommand run" -s i -l interactive -d 'Make STDIN available to the virtual machine\'s process' -r -f -a "always\t''
never\t''
auto\t''"
complete -c vmexec -n "__fish_vmexec_using_subcommand run" -s t -l tty -d 'Allocate a pseudo-TTY for the virtual machine' -r -f -a "always\t''
never\t''
auto\t''"
complete -c vmexec -n "__fish_vmexec_using_subcommand run" -l pull -d 'When to pull a new image' -r -f -a "missing\t''
never\t''
newer\t''"
complete -c vmexec -n "__fish_vmexec_using_subcommand run" -s d -l detach -d 'Run virtual machine in background and print virtual machine ID'
complete -c vmexec -n "__fish_vmexec_using_subcommand run" -l rm -d 'Remove virtual machine runtime data after exit'
complete -c vmexec -n "__fish_vmexec_using_subcommand run" -l disable-kvm -d 'Run virtual machine with TCG instead of KVM'
complete -c vmexec -n "__fish_vmexec_using_subcommand run" -l show-vm-window -d 'Show a window with the virtual machine running in it'
complete -c vmexec -n "__fish_vmexec_using_subcommand run" -s h -l help -d 'Print help (see more with \'--help\')'
complete -c vmexec -n "__fish_vmexec_using_subcommand ksm" -s e -l enable -d 'Persistently enable KSM by writing settings to /etc/tmpfiles.d/ksm.conf'
complete -c vmexec -n "__fish_vmexec_using_subcommand ksm" -s d -l disable -d 'Persistently disable KSM by deleting /etc/tmpfiles.d/ksm.conf'
complete -c vmexec -n "__fish_vmexec_using_subcommand ksm" -s h -l help -d 'Print help (see more with \'--help\')'
complete -c vmexec -n "__fish_vmexec_using_subcommand clean" -s h -l help -d 'Print help'
complete -c vmexec -n "__fish_vmexec_using_subcommand prune" -s h -l help -d 'Print help'
complete -c vmexec -n "__fish_vmexec_using_subcommand ready" -l timeout -d 'Timeout in seconds when waiting for the virtual machine to become ready' -r
complete -c vmexec -n "__fish_vmexec_using_subcommand ready" -l wait -d 'Wait for the virtual machine to become ready'
complete -c vmexec -n "__fish_vmexec_using_subcommand ready" -s h -l help -d 'Print help (see more with \'--help\')'
complete -c vmexec -n "__fish_vmexec_using_subcommand completions" -s h -l help -d 'Print help'
complete -c vmexec -n "__fish_vmexec_using_subcommand manpage" -s h -l help -d 'Print help'
complete -c vmexec -n "__fish_vmexec_using_subcommand help; and not __fish_seen_subcommand_from ps inspect images logs stop exec pull run ksm clean prune ready completions manpage help" -f -a "ps" -d 'List virtual machines'
complete -c vmexec -n "__fish_vmexec_using_subcommand help; and not __fish_seen_subcommand_from ps inspect images logs stop exec pull run ksm clean prune ready completions manpage help" -f -a "inspect" -d 'Display detailed information on a virtual machine'
complete -c vmexec -n "__fish_vmexec_using_subcommand help; and not __fish_seen_subcommand_from ps inspect images logs stop exec pull run ksm clean prune ready completions manpage help" -f -a "images" -d 'List all available OS images'
complete -c vmexec -n "__fish_vmexec_using_subcommand help; and not __fish_seen_subcommand_from ps inspect images logs stop exec pull run ksm clean prune ready completions manpage help" -f -a "logs" -d 'Show terminal log of an existing virtual machine'
complete -c vmexec -n "__fish_vmexec_using_subcommand help; and not __fish_seen_subcommand_from ps inspect images logs stop exec pull run ksm clean prune ready completions manpage help" -f -a "stop" -d 'Stop a virtual machine by sending a SIGTERM signal'
complete -c vmexec -n "__fish_vmexec_using_subcommand help; and not __fish_seen_subcommand_from ps inspect images logs stop exec pull run ksm clean prune ready completions manpage help" -f -a "exec" -d 'Run a command in an existing virtual machine'
complete -c vmexec -n "__fish_vmexec_using_subcommand help; and not __fish_seen_subcommand_from ps inspect images logs stop exec pull run ksm clean prune ready completions manpage help" -f -a "pull" -d 'Pull an image without running a virtual machine'
complete -c vmexec -n "__fish_vmexec_using_subcommand help; and not __fish_seen_subcommand_from ps inspect images logs stop exec pull run ksm clean prune ready completions manpage help" -f -a "run" -d 'Run a command in a new virtual machine'
complete -c vmexec -n "__fish_vmexec_using_subcommand help; and not __fish_seen_subcommand_from ps inspect images logs stop exec pull run ksm clean prune ready completions manpage help" -f -a "ksm" -d 'Check and change KSM status'
complete -c vmexec -n "__fish_vmexec_using_subcommand help; and not __fish_seen_subcommand_from ps inspect images logs stop exec pull run ksm clean prune ready completions manpage help" -f -a "clean" -d 'Clean up old virtual machines'
complete -c vmexec -n "__fish_vmexec_using_subcommand help; and not __fish_seen_subcommand_from ps inspect images logs stop exec pull run ksm clean prune ready completions manpage help" -f -a "prune" -d 'Delete all overlay images from the cache'
complete -c vmexec -n "__fish_vmexec_using_subcommand help; and not __fish_seen_subcommand_from ps inspect images logs stop exec pull run ksm clean prune ready completions manpage help" -f -a "ready" -d 'Check if a virtual machine is ready to accept SSH connections'
complete -c vmexec -n "__fish_vmexec_using_subcommand help; and not __fish_seen_subcommand_from ps inspect images logs stop exec pull run ksm clean prune ready completions manpage help" -f -a "completions" -d 'Generate completion file for a shell'
complete -c vmexec -n "__fish_vmexec_using_subcommand help; and not __fish_seen_subcommand_from ps inspect images logs stop exec pull run ksm clean prune ready completions manpage help" -f -a "manpage" -d 'Print man page'
complete -c vmexec -n "__fish_vmexec_using_subcommand help; and not __fish_seen_subcommand_from ps inspect images logs stop exec pull run ksm clean prune ready completions manpage help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
