Skip to content

Commit

Permalink
Fix bash_completion for subcommands
Browse files Browse the repository at this point in the history
Fixes: maros#72

**Improvements/Fixes:**

* Filter out duplicate entries from `_.*_COMMANDS="..."`
* Do not add subcommands to `_.*_COMMANDS="..."`
* add subcommands at the beginning of `_.*_macc_command` options list
* use `_.*_command_subcommand() { ... }` instead of `_.*_command subcommand() { ... }`
  • Loading branch information
M0ses committed Jul 15, 2024
1 parent 76adc06 commit 88c7f9f
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions lib/MooseX/App/Plugin/BashCompletion/Command.pm
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ sub bash_completion {
my %command_map;
my $app_meta = $app->meta;
my $commands = $app_meta->app_commands;
my @commands_to_complete = grep { $_ ne 'bash_completion' } sort keys %{$commands};
my $command_list = join (' ', @commands_to_complete);
my @filtered_cmds = grep { $_ ne 'bash_completion' } keys %{$commands};
my %unique_cmds = map { my @c = split ' ', $_; ($c[0], 1); } @filtered_cmds;
my @cmds2complete = sort(@filtered_cmds);
my $command_list = join (' ', sort(keys(%unique_cmds)));
my $package = __PACKAGE__;
my $prefix = $app_meta->app_base;

Expand All @@ -29,14 +31,17 @@ sub bash_completion {

$prefix =~ tr/./_/;

foreach my $command (@commands_to_complete) {
foreach my $command (@cmds2complete) {
my $command_class = $commands->{$command};
Class::Load::load_class($command_class);
#my @parameters = $app_meta->command_usage_attributes($command_class->meta,'parameter');
my @options = $app_meta->command_usage_attributes($command_class->meta,[qw(option proto)]);
my @sub_map = map { $_ =~ /^${command} (\w+)/; $1; } sort(@filtered_cmds);
my @sub_cmds = grep { defined $_ } @sub_map;
my @options = $app_meta->command_usage_attributes(
$command_class->meta,
[qw(option proto)],
);
$command_map{$command} = {
#parameters => [ map { $_->is_required } @parameters ],
options => [ map { $_->cmd_usage_name } @options ],
options => [@sub_cmds, sort(map { $_->cmd_usage_name } @options)],
};
}

Expand All @@ -60,9 +65,11 @@ _${prefix}_macc_help() {
EOT

foreach my $command (sort keys %command_map) {
$syntax .= "_${prefix}_macc_${command}() {\n _${prefix}_compreply \"";
my $fn_suffix = $command;
$fn_suffix =~ s/ /_/g;
$syntax .= "_${prefix}_macc_${fn_suffix}() {\n _${prefix}_compreply \"";
#$syntax .= join(" ", @{$data->{parameters}});
$syntax .= join(" ", sort @{$command_map{$command}->{options}});
$syntax .= join(" ", @{$command_map{$command}->{options}});
$syntax .= "\"\n}\n\n";
}

Expand Down

0 comments on commit 88c7f9f

Please sign in to comment.