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

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

complete -c asak -n "__fish_asak_needs_command" -s h -l help -d 'Print help'
complete -c asak -n "__fish_asak_needs_command" -s V -l version -d 'Print version'
complete -c asak -n "__fish_asak_needs_command" -f -a "rec" -d 'Record an audio file'
complete -c asak -n "__fish_asak_needs_command" -f -a "play" -d 'Play an audio file'
complete -c asak -n "__fish_asak_needs_command" -f -a "monitor" -d 'Monitor audio input with scopes'
complete -c asak -n "__fish_asak_needs_command" -f -a "list" -d 'List available audio devices'
complete -c asak -n "__fish_asak_needs_command" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
complete -c asak -n "__fish_asak_using_subcommand rec" -s d -l device -d 'The audio device index to use' -r
complete -c asak -n "__fish_asak_using_subcommand rec" -s h -l help -d 'Print help'
complete -c asak -n "__fish_asak_using_subcommand play" -s d -l device -d 'The audio device index to use' -r
complete -c asak -n "__fish_asak_using_subcommand play" -s h -l help -d 'Print help'
complete -c asak -n "__fish_asak_using_subcommand monitor" -s b -l buffer-size -d 'Buffer size for the audio input monitoring, defaults to 1024, the higher the value the more latency' -r
complete -c asak -n "__fish_asak_using_subcommand monitor" -s h -l help -d 'Print help'
complete -c asak -n "__fish_asak_using_subcommand list" -s h -l help -d 'Print help'
complete -c asak -n "__fish_asak_using_subcommand help; and not __fish_seen_subcommand_from rec play monitor list help" -f -a "rec" -d 'Record an audio file'
complete -c asak -n "__fish_asak_using_subcommand help; and not __fish_seen_subcommand_from rec play monitor list help" -f -a "play" -d 'Play an audio file'
complete -c asak -n "__fish_asak_using_subcommand help; and not __fish_seen_subcommand_from rec play monitor list help" -f -a "monitor" -d 'Monitor audio input with scopes'
complete -c asak -n "__fish_asak_using_subcommand help; and not __fish_seen_subcommand_from rec play monitor list help" -f -a "list" -d 'List available audio devices'
complete -c asak -n "__fish_asak_using_subcommand help; and not __fish_seen_subcommand_from rec play monitor list help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
