# Print an optspec for argparse to handle cmd's options that are independent of any subcommand.
function __fish_topiary_global_optspecs
	string join \n C/configuration= M/merge-configuration v/verbose h/help V/version
end

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

complete -c topiary -n "__fish_topiary_needs_command" -s C -l configuration -d 'Configuration file' -r -F
complete -c topiary -n "__fish_topiary_needs_command" -s M -l merge-configuration -d 'Enable merging for configuration files'
complete -c topiary -n "__fish_topiary_needs_command" -s v -l verbose -d 'Logging verbosity (increased per occurrence)'
complete -c topiary -n "__fish_topiary_needs_command" -s h -l help -d 'Print help'
complete -c topiary -n "__fish_topiary_needs_command" -s V -l version -d 'Print version'
complete -c topiary -n "__fish_topiary_needs_command" -f -a "format" -d 'Format inputs'
complete -c topiary -n "__fish_topiary_needs_command" -f -a "visualise" -d 'Visualise the input\'s Tree-sitter parse tree'
complete -c topiary -n "__fish_topiary_needs_command" -f -a "config" -d 'Print the current configuration'
complete -c topiary -n "__fish_topiary_needs_command" -f -a "prefetch" -d 'Prefetch languages in the configuration'
complete -c topiary -n "__fish_topiary_needs_command" -f -a "coverage" -d 'Checks how much of the tree-sitter query is used'
complete -c topiary -n "__fish_topiary_needs_command" -f -a "completion" -d 'Generate shell completion script'
complete -c topiary -n "__fish_topiary_needs_command" -f -a "check-grammar" -d 'Check if an input parses to the respective Tree-sitter grammar'
complete -c topiary -n "__fish_topiary_needs_command" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
complete -c topiary -n "__fish_topiary_using_subcommand format" -s l -l language -d 'Topiary language identifier (when formatting stdin)' -r
complete -c topiary -n "__fish_topiary_using_subcommand format" -s q -l query -d 'Topiary query file override (when formatting stdin)' -r -F
complete -c topiary -n "__fish_topiary_using_subcommand format" -s C -l configuration -d 'Configuration file' -r -F
complete -c topiary -n "__fish_topiary_using_subcommand format" -s t -l tolerate-parsing-errors -d 'Consume as much as possible in the presence of parsing errors'
complete -c topiary -n "__fish_topiary_using_subcommand format" -s s -l skip-idempotence -d 'Do not check that formatting twice gives the same output'
complete -c topiary -n "__fish_topiary_using_subcommand format" -s L -l follow-symlinks -d 'Follow symlinks (when formatting files)'
complete -c topiary -n "__fish_topiary_using_subcommand format" -s M -l merge-configuration -d 'Enable merging for configuration files'
complete -c topiary -n "__fish_topiary_using_subcommand format" -s v -l verbose -d 'Logging verbosity (increased per occurrence)'
complete -c topiary -n "__fish_topiary_using_subcommand format" -s h -l help -d 'Print help (see more with \'--help\')'
complete -c topiary -n "__fish_topiary_using_subcommand visualise" -s f -l format -d 'Visualisation format' -r -f -a "dot\t'GraphViz DOT serialisation'
json\t'JSON serialisation'"
complete -c topiary -n "__fish_topiary_using_subcommand visualise" -s l -l language -d 'Topiary language identifier (when formatting stdin)' -r
complete -c topiary -n "__fish_topiary_using_subcommand visualise" -s q -l query -d 'Topiary query file override (when formatting stdin)' -r -F
complete -c topiary -n "__fish_topiary_using_subcommand visualise" -s C -l configuration -d 'Configuration file' -r -F
complete -c topiary -n "__fish_topiary_using_subcommand visualise" -s M -l merge-configuration -d 'Enable merging for configuration files'
complete -c topiary -n "__fish_topiary_using_subcommand visualise" -s v -l verbose -d 'Logging verbosity (increased per occurrence)'
complete -c topiary -n "__fish_topiary_using_subcommand visualise" -s h -l help -d 'Print help (see more with \'--help\')'
complete -c topiary -n "__fish_topiary_using_subcommand config; and not __fish_seen_subcommand_from show-sources help" -s C -l configuration -d 'Configuration file' -r -F
complete -c topiary -n "__fish_topiary_using_subcommand config; and not __fish_seen_subcommand_from show-sources help" -s M -l merge-configuration -d 'Enable merging for configuration files'
complete -c topiary -n "__fish_topiary_using_subcommand config; and not __fish_seen_subcommand_from show-sources help" -s v -l verbose -d 'Logging verbosity (increased per occurrence)'
complete -c topiary -n "__fish_topiary_using_subcommand config; and not __fish_seen_subcommand_from show-sources help" -s h -l help -d 'Print help'
complete -c topiary -n "__fish_topiary_using_subcommand config; and not __fish_seen_subcommand_from show-sources help" -f -a "show-sources" -d 'Display config sources that Topiary looks through'
complete -c topiary -n "__fish_topiary_using_subcommand config; and not __fish_seen_subcommand_from show-sources help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
complete -c topiary -n "__fish_topiary_using_subcommand config; and __fish_seen_subcommand_from show-sources" -s C -l configuration -d 'Configuration file' -r -F
complete -c topiary -n "__fish_topiary_using_subcommand config; and __fish_seen_subcommand_from show-sources" -s M -l merge-configuration -d 'Enable merging for configuration files'
complete -c topiary -n "__fish_topiary_using_subcommand config; and __fish_seen_subcommand_from show-sources" -s v -l verbose -d 'Logging verbosity (increased per occurrence)'
complete -c topiary -n "__fish_topiary_using_subcommand config; and __fish_seen_subcommand_from show-sources" -s h -l help -d 'Print help'
complete -c topiary -n "__fish_topiary_using_subcommand config; and __fish_seen_subcommand_from help" -f -a "show-sources" -d 'Display config sources that Topiary looks through'
complete -c topiary -n "__fish_topiary_using_subcommand config; and __fish_seen_subcommand_from help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
complete -c topiary -n "__fish_topiary_using_subcommand prefetch" -s C -l configuration -d 'Configuration file' -r -F
complete -c topiary -n "__fish_topiary_using_subcommand prefetch" -s f -l force -d 'Re-fetch existing grammars if they already exist'
complete -c topiary -n "__fish_topiary_using_subcommand prefetch" -s M -l merge-configuration -d 'Enable merging for configuration files'
complete -c topiary -n "__fish_topiary_using_subcommand prefetch" -s v -l verbose -d 'Logging verbosity (increased per occurrence)'
complete -c topiary -n "__fish_topiary_using_subcommand prefetch" -s h -l help -d 'Print help'
complete -c topiary -n "__fish_topiary_using_subcommand coverage" -s l -l language -d 'Topiary language identifier (when formatting stdin)' -r
complete -c topiary -n "__fish_topiary_using_subcommand coverage" -s q -l query -d 'Topiary query file override (when formatting stdin)' -r -F
complete -c topiary -n "__fish_topiary_using_subcommand coverage" -s C -l configuration -d 'Configuration file' -r -F
complete -c topiary -n "__fish_topiary_using_subcommand coverage" -s M -l merge-configuration -d 'Enable merging for configuration files'
complete -c topiary -n "__fish_topiary_using_subcommand coverage" -s v -l verbose -d 'Logging verbosity (increased per occurrence)'
complete -c topiary -n "__fish_topiary_using_subcommand coverage" -s h -l help -d 'Print help (see more with \'--help\')'
complete -c topiary -n "__fish_topiary_using_subcommand completion" -s C -l configuration -d 'Configuration file' -r -F
complete -c topiary -n "__fish_topiary_using_subcommand completion" -s M -l merge-configuration -d 'Enable merging for configuration files'
complete -c topiary -n "__fish_topiary_using_subcommand completion" -s v -l verbose -d 'Logging verbosity (increased per occurrence)'
complete -c topiary -n "__fish_topiary_using_subcommand completion" -s h -l help -d 'Print help'
complete -c topiary -n "__fish_topiary_using_subcommand check-grammar" -s l -l language -d 'Topiary language identifier (when formatting stdin)' -r
complete -c topiary -n "__fish_topiary_using_subcommand check-grammar" -s q -l query -d 'Topiary query file override (when formatting stdin)' -r -F
complete -c topiary -n "__fish_topiary_using_subcommand check-grammar" -s C -l configuration -d 'Configuration file' -r -F
complete -c topiary -n "__fish_topiary_using_subcommand check-grammar" -s L -l follow-symlinks -d 'Follow symlinks (when formatting files)'
complete -c topiary -n "__fish_topiary_using_subcommand check-grammar" -s M -l merge-configuration -d 'Enable merging for configuration files'
complete -c topiary -n "__fish_topiary_using_subcommand check-grammar" -s v -l verbose -d 'Logging verbosity (increased per occurrence)'
complete -c topiary -n "__fish_topiary_using_subcommand check-grammar" -s h -l help -d 'Print help (see more with \'--help\')'
complete -c topiary -n "__fish_topiary_using_subcommand help; and not __fish_seen_subcommand_from format visualise config prefetch coverage completion check-grammar help" -f -a "format" -d 'Format inputs'
complete -c topiary -n "__fish_topiary_using_subcommand help; and not __fish_seen_subcommand_from format visualise config prefetch coverage completion check-grammar help" -f -a "visualise" -d 'Visualise the input\'s Tree-sitter parse tree'
complete -c topiary -n "__fish_topiary_using_subcommand help; and not __fish_seen_subcommand_from format visualise config prefetch coverage completion check-grammar help" -f -a "config" -d 'Print the current configuration'
complete -c topiary -n "__fish_topiary_using_subcommand help; and not __fish_seen_subcommand_from format visualise config prefetch coverage completion check-grammar help" -f -a "prefetch" -d 'Prefetch languages in the configuration'
complete -c topiary -n "__fish_topiary_using_subcommand help; and not __fish_seen_subcommand_from format visualise config prefetch coverage completion check-grammar help" -f -a "coverage" -d 'Checks how much of the tree-sitter query is used'
complete -c topiary -n "__fish_topiary_using_subcommand help; and not __fish_seen_subcommand_from format visualise config prefetch coverage completion check-grammar help" -f -a "completion" -d 'Generate shell completion script'
complete -c topiary -n "__fish_topiary_using_subcommand help; and not __fish_seen_subcommand_from format visualise config prefetch coverage completion check-grammar help" -f -a "check-grammar" -d 'Check if an input parses to the respective Tree-sitter grammar'
complete -c topiary -n "__fish_topiary_using_subcommand help; and not __fish_seen_subcommand_from format visualise config prefetch coverage completion check-grammar help" -f -a "help" -d 'Print this message or the help of the given subcommand(s)'
complete -c topiary -n "__fish_topiary_using_subcommand help; and __fish_seen_subcommand_from config" -f -a "show-sources" -d 'Display config sources that Topiary looks through'
