diff --git a/spec/completion_spec.lua b/spec/completion_spec.lua index 2def596..fe78537 100644 --- a/spec/completion_spec.lua +++ b/spec/completion_spec.lua @@ -259,9 +259,9 @@ complete -c foo -n '__fish_seen_subcommand_from install' -s v -l verbose parser:command "install" :add_help(false) assert.equal([[ +complete -c foo -n '__fish_use_subcommand' -xa 'help' -d 'Show help for commands' complete -c foo -n '__fish_seen_subcommand_from help' -xa 'help' complete -c foo -n '__fish_seen_subcommand_from help' -xa 'install' -complete -c foo -n '__fish_use_subcommand' -xa 'help' -d 'Show help for commands' complete -c foo -n '__fish_use_subcommand' -xa 'install' ]], parser:get_fish_complete()) end) diff --git a/src/argparse.lua b/src/argparse.lua index a93b2e1..d7c069d 100644 --- a/src/argparse.lua +++ b/src/argparse.lua @@ -1081,7 +1081,6 @@ function Parser:add_help_command(value) end help._is_help_command = true - self._help_command = help return self end @@ -1222,7 +1221,7 @@ end function Parser:_bash_get_cmd(buf) local cmds = {} for _, command in ipairs(self._commands) do - if command ~= self._help_command then + if not command._is_help_command then table.insert(cmds, (" "):rep(12) .. ("%s)"):format(table.concat(command._aliases, "|"))) table.insert(cmds, (" "):rep(16) .. ('cmd="%s"'):format(command._aliases[1])) table.insert(cmds, (" "):rep(16) .. "break") @@ -1242,7 +1241,7 @@ end function Parser:_bash_cmd_completions(buf) local subcmds = {} for _, command in ipairs(self._commands) do - if #command._options > 0 and command ~= self._help_command then + if #command._options > 0 and not command._is_help_command then table.insert(subcmds, (" "):rep(8) .. command._aliases[1] .. ")") command:_bash_option_args(subcmds, 12) table.insert(subcmds, (" "):rep(12) .. ('opts="$opts %s"'):format(get_options(command))) @@ -1413,14 +1412,22 @@ function Parser:_fish_complete_help(lines, prefix) for _, command in ipairs(self._commands) do for _, alias in ipairs(command._aliases) do local line = ("%s -n '__fish_use_subcommand' -xa '%s'"):format(prefix, alias) - if command._description then local description = fish_escape(get_short_description(command)) line = ("%s -d '%s'"):format(line, description) end - table.insert(lines, line) end + + if command._is_help_command then + local help_aliases = table.concat(command._aliases, " ") + + for _, cmd in ipairs(self._commands) do + local line = ("%s -n '__fish_seen_subcommand_from %s' -xa '%s'") + :format(prefix, help_aliases, table.concat(cmd._aliases, " ")) + table.insert(lines, line) + end + end end for _, option in ipairs(self._options) do @@ -1463,19 +1470,7 @@ end function Parser:get_fish_complete() local lines = {} local prefix = ("complete -c %s"):format(self._name) - - if self._help_command then - local help_aliases = table.concat(self._help_command._aliases, " ") - - for _, command in ipairs(self._commands) do - local line = ("%s -n '__fish_seen_subcommand_from %s' -xa '%s'") - :format(prefix, help_aliases, command._aliases[1]) - table.insert(lines, line) - end - end - self:_fish_complete_help(lines, prefix) - return table.concat(lines, "\n") .. "\n" end