Anandan
Anandan

Reputation:

How do I do tab completion in Perl's Term::Shell?

I am using the Term::Shell package in Perl for implementing a CLI tool. I am not able to do the tab completion of a command part with that.

comp_CMD() - which is a API provided by this Term::Shell, is to achieve the tab completion. This is not helping me. Does anyone know how to make this work??

Sample Code:

#!/usr/bin/env perl
package Sample;
use base qw(Term::Shell);

sub prompt_str { "Sample\>" };

sub comp_show
{
    my $o = shift;
    my $word = shift;
    $o->completions($word, [qw(all work and no play is no fun at)]);
}

sub run_show
{
        print "run show\n";
}

package main;
Sample->new->cmdloop;

This is a run of the program:

Sample>show[TAB] 

The above command doesnt give the expected output.. it just gives me a tab.

Upvotes: 4

Views: 2632

Answers (3)

Anandan
Anandan

Reputation:

first of all, i am not getting the "add comment" button.. so i am posting as an answer.

I tried this way:

sample> sh"TAB"

sample>show w"TAB"

Nothing worked.

Does it have anything to do with environment? or something else?

Upvotes: 0

ccheneson
ccheneson

Reputation: 49410

Make sure you have Term::ReadLine::Gnu or Term::ReadLine::Perl installed.

Upvotes: 6

Inshallah
Inshallah

Reputation: 4814

Your sample works for me. Both "show" and its arguments get completed.

After you type "show", there is nothing more to complete, it's already a full command. To get the first argument to complete, you have to at least provide its first letter; so typing <TAB> immediately after show, can only get you to the place where you have to type the first letter of the argument you want to complete. And if you hit <TAB> twice in a row, you will see what completions are available.

The only thing I found odd is that, if there is only a single possible argument to complete, it doesn't automatically get completed. You still have to provide the first letter. It's a little odd that, but perhaps just an oversight by the implementor.

Upvotes: 2

Related Questions