Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Tab completion on the command line

by Anonymous Monk
on Apr 23, 2004 at 14:27 UTC ( [id://347633]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a script that takes an argument from the command line. There are only about 20 or so possible values for this argument and I would like to make it so that the argument will tab complete. Is this possible?

Replies are listed 'Best First'.
Re: Tab completion on the command line
by halley (Prior) on Apr 23, 2004 at 14:30 UTC
    It depends on your platform, and what shell you're running. If you're writing your own input routines, then this *may* be Perl-related, but otherwise, this has nothing to do with Perl.

    Update: expanded.

    If you're on Windows, your shell is CMD.EXE and it does support command-line completion, but (1) only for filenames, and (2) only if you enable it in the registry.

    If you're on Unix/Linux, your shell may have completion support. Bash uses Tab, Csh uses Esc Esc. Some versions of these take completion heuristic rules in a separate configuration file.

    If you ARE writing your own input routine, you'll need to read from the console in an unbuffered way, so that you can see each character typed. When they tab, you decide what else should be simulated as typed. If they tab again, you may have to erase things from the screen with backspaces before typing a replacement. It's painful to write something like this yourself, and it often raises platform-specific bugs. There's no "standard" way of doing key-by-key input in Perl's basic module library.

    --
    [ e d @ h a l l e y . c c ]

Re: Tab completion on the command line
by gjb (Vicar) on Apr 23, 2004 at 14:34 UTC

    You could have a look at Bash programmable Completion, but note that this is platform and shell dependent. Incidently, it has nothing to do with Perl.

    Hope this helps, -gjb-

Re: Tab completion on the command line
by eserte (Deacon) on Apr 23, 2004 at 15:05 UTC
    Yes, with shells like tcsh or zsh.
Re: Tab completion on the command line
by Sandy (Curate) on Apr 24, 2004 at 00:35 UTC
    It ain't complete (only finds complete words, not a list of incomplete words), but I did test it a bit.

    It autocompletes on qw(one two three four five six seven).

    It's entirely perl. I tested it on Solaris, and although I didn't test it on windows, it should work (i re-used code that i had tested on windows).

    [~] perl -e 'use GetWord;$a=Get_Word();print"$a\n";' Enter command > abc your command was abc [~] perl -e 'use GetWord;$a=Get_Word();print"$a\n";' Enter command > o<tab> ->auto generated->one [~] perl -e 'use GetWord;$a=Get_Word();print"$a\n";' Enter command > tw<tab> ->auto generated->two
    This is GetWord code (partly taken from the Get_Password CPAN module, and partly from the camel book).

    I'm going to use readmore tags, but i'm never sure if it works, so if they don't, i apologize in advance.

Re: Tab completion on the command line
by Theo (Priest) on Apr 26, 2004 at 04:19 UTC
    Don't confuse shell command-line completion with your needs. The shell knows nothing of your 20 or so values. The completion must be done within your script, as halley and Sandy have mentioned.

    -Theo-
    (so many nodes and so little time ... )

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://347633]
Approved by sgifford
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-25 23:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found