+ return $cmd if exists $def->{$cmd}; # command is already complete
+
+ my @expanded = grep { /^\Q$cmd\E/ } keys %$def;
+ return $expanded[0] if scalar(@expanded) == 1; # enforce exact match
+
+ return undef;
+};
+
+my $get_commands = sub {
+ my $def = shift // die "no command definition passed!";
+ return [ grep { !(ref($def->{$_}) eq 'HASH' && defined($def->{$_}->{alias})) } sort keys %$def ];
+};
+
+my $complete_command_names = sub { $get_commands->($cmddef) };
+
+# traverses the command definition using the $argv array, resolving one level
+# of aliases.
+# Returns the matching (sub) command and its definition, and argument array for
+# this (sub) command and a hash where we marked which (sub) commands got
+# expanded (e.g. st => status) while traversing
+sub resolve_cmd {
+ my ($argv, $is_alias) = @_;
+
+ my ($def, $cmd) = ($cmddef, $argv);
+ my $cmdstr = $exename;
+
+ if (ref($argv) eq 'ARRAY') {
+ my $expanded_last_arg;
+ my $last_arg_id = scalar(@$argv) - 1;
+
+ for my $i (0..$last_arg_id) {
+ $cmd = $expand_command_name->($def, $argv->[$i]);
+ if (defined($cmd)) {
+ # If the argument was expanded (or was already complete) and it
+ # is the final argument, tell our caller about it:
+ $expanded_last_arg = $cmd if $i == $last_arg_id;
+ } else {
+ # Otherwise continue with the unexpanded version of it.
+ $cmd = $argv->[$i];
+ }
+ $cmdstr .= " $cmd";
+ $def = $def->{$cmd};
+ last if !defined($def);
+
+ if (ref($def) eq 'ARRAY') {
+ # could expand to a real command, rest of $argv are its arguments
+ my $cmd_args = [ @$argv[$i+1..$last_arg_id] ];
+ return ($cmd, $def, $cmd_args, $expanded_last_arg, $cmdstr);
+ }
+
+ if (defined($def->{alias})) {
+ die "alias loop detected for '$cmd'" if $is_alias; # avoids cycles
+ # replace aliased (sub)command with the expanded aliased command
+ splice @$argv, $i, 1, split(/ +/, $def->{alias});
+ return resolve_cmd($argv, 1);