Readline programming


 
Thread Tools Search this Thread
Top Forums Programming Readline programming
# 1  
Old 05-10-2011
Readline programming

Hi,

I am new to using readline library to my application. Please I want to no how to write code for command line switches(options), i.e when i press tab the option of command as to change.
eg:
Code:
ls -a
ls -d

...so on ls as many options, here i want options to be completed using tab

Please any one could help on this really appreciated.
Thank you

Moderator's Comments:
Mod Comment Please use [code] and [/code] tags when posting code, data or logs etc. to preserve formatting and enhance readability, thanks.

Last edited by zaxxon; 05-10-2011 at 07:07 AM.. Reason: code tags
# 2  
Old 05-10-2011
Do you want to setup auto-completion of options?
# 3  
Old 05-10-2011
I think you've got something mixed up here.

readline is used if you want your application to present interactive input including line editing and history.

getopt from the GNU libc is used to parse command line options.

Command line completion as you describe it is provided by the shell, not the application. It's provided by the bash completion package for bash, and zsh is already shipped with that feature. These extensions are configured using shell commands and scripts.
This User Gave Thanks to pludi For This Post:
# 4  
Old 05-11-2011
Readline Probelm

Hi,

actually for command line auto completion i knew that using shell.....but we see interactive command line args completion can done by tab key using gnu readline library...up to this i knew but here i want command line switches completion using tab

---------- Post updated at 11:45 PM ---------- Previous update was at 11:29 PM ----------

here wht I want is:
Just type your command, a dash (-) and type tab. The shell will show you available switches.
For example, try:
root@ubuntu:~# ls -
and press tab - Surprise!
-a -c -b -d
-A -author -B -f .... so on options that available with ls command.
# 5  
Old 05-11-2011
mhmm, if you are using Bash you could achieve this by using only Bash's built-in `complete' and a couple of other commands. Or as suggested by pludi you could have a look at the bash autocompletion package or I could post my own function which does the same job and which I have written a few years ago. Below is just an example of how it works:
Code:
sidorenko@sidorenko>
0 ~>autocomplete_options ls

sidorenko@sidorenko>
0 ~>ls -  pressing tab
-1                                         -l
-a                                         -L
-A                                         -m
--author                                   -n
-b                                         -N
-B                                         -o
--block-size=                              -p
-c                                         -q
-C                                         -Q
--color                                    --quoting-style=
--color=                                   -r
-d                                         -R
-D                                         -s
--dereference-command-line-symlink-to-dir  -S
-f                                         --show-control-chars
-F                                         --si
--file-type                                --sort=
--format=                                  -t
--full-time                                -T
-g

Though when I was reading the help for the `complete' built-in, I came across the following line:
Quote:
>help complete
complete: complete [-abcdefgjksuv] [-pr] [-DE] [-o option] [-A action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [name ...]
Specify how arguments are to be completed by Readline.
I don't know what is meant here with Readline - maybe some C library or whatever. Do you mean this Readline?

---------- Post updated at 02:02 AM ---------- Previous update was at 01:53 AM ----------

On Linux I've indeed found such a function:
Code:
SYNOPSIS
       #include <stdio.h>
       #include <readline/readline.h>
       #include <readline/history.h>

       char *
       readline (const char *prompt);

This User Gave Thanks to sidorenko For This Post:
# 6  
Old 05-12-2011
Readline Programming

Hi,

Thank you so much for a usefull solution. Exactly this is what iam looking for but this is here for bash right? so now i want it using readline. Yes the same readline what i mean...which linux gnu readline library. Please can u post the function you have with you.which does same above action..which may help out from this issue.

Thanks again

---------- Post updated at 05:00 AM ---------- Previous update was at 04:52 AM ----------

And also you said you ahve your own fuction which is written few years back...........please could you provide me that fuction so that it may help me.

Last edited by vanid; 05-12-2011 at 07:19 AM..
# 7  
Old 05-12-2011
Again, the completion suggestions are done by the shell, not the application. Your application isn't even loaded at that point, and thus has no way of telling the shell what options are available. The readline library would only be of interest if you want that functionality inside your application (like GDB when used interactive), and even then you'd have to extend the functionality yourself.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Trouble compiling program using the readline library.

Hi: in the info page for readline library I read -- Function: void rl_variable_dumper (int readable) Print the readline variable names and their current values to `rl_outstream'. If READABLE is non-zero, the list is formatted in such a way that it can be made part of an... (1 Reply)
Discussion started by: stf92
1 Replies

2. UNIX for Dummies Questions & Answers

From iOS programming to Linux system programming

Hello. I like Linux and C programming language. Allways wanted to understand kernel and become a Linux system programmer. And I also like Objective-C and iOS. These two programming areas have relations: 1. Linux and iOS are UNIX-like systems, POSIX compliant. 2. It is useful to know C language... (2 Replies)
Discussion started by: Rockatansky
2 Replies

3. Shell Programming and Scripting

How do I get my shell back to normal readline?

I just ran an application that crashed... but before it did, it managed to set readline echo off, and probably a bunch of other settings. Is there any way I can just tell my shell to re-initialize? To get back to whatever state existed before my shell got messed up by this evil program? (2 Replies)
Discussion started by: jjinno
2 Replies

4. Shell Programming and Scripting

using readline with parameter

dear all, i have code shell like this but i want to using parameter for this shell how i can do that :ex ./sample.sh 100 500sample.sh START=${1} LAST=${2} for (( a=${START}; a<=${LAST}; a++ )) do { echo $a } donethx for your advice (5 Replies)
Discussion started by: zvtral
5 Replies

5. Programming

Error:readline() on closed filehandle Perl

Hi, i have run the below perl code and i am getting an error Error:readline() on closed filehandle OR at run.pl line 31. CODE: =========================================== open OR,$ARGV; while (<OR>) { # find the batch date next if length $_ < 3; # BLANK LINE # last if $. > 120; #... (3 Replies)
Discussion started by: pspriyanka
3 Replies

6. Shell Programming and Scripting

Readline Formatting

Hi All, I have a function that loops through an XML file line by line and spits it the content out to a new file (sometimes certain lines need changing). This all works fine, however the formatting of the original XML is not kept. for example:- <?xml version="1.0"?> <mysqldump>... (3 Replies)
Discussion started by: robfwauk
3 Replies

7. Programming

C Programming - Hardware Programming

Can someone help me on suggesting some ways to access the memory content in RAM directly from C/C++ source code. Please provide me any book name or any URL so that I can get an exhaustive knowledge over it. If possible please give me some tips on interacting with hardwares directly through... (3 Replies)
Discussion started by: nandumishra
3 Replies

8. Programming

Readline problems

I'm having problems with libreadline. When I write text longer than the current line, the text wraps back to the beginning of the line rather than to the next line. Also, when I use the arrow keys to edit something in that beginning part, it won't display at all (so I can edit only if I remember... (5 Replies)
Discussion started by: CRGreathouse
5 Replies

9. Shell Programming and Scripting

cat vs head vs readline get variable from txt file

I have a file with a single filename in it, which I want to assign to a BASH variable, so I've been trying: c=$(head -1 somefile) echo $c which outputs correctly, but them when I do ... somecommand $c it says it can't find the file, is that because it's grabbing the whole line, and... (5 Replies)
Discussion started by: unclecameron
5 Replies

10. UNIX for Dummies Questions & Answers

Carreer:Networking Programming in Unix (C programming Language)

Hello, I am trying to learn Networking Programming in C in unix enviorment. I want to know how good it is to become a network programmer. i am crazy about Network programming but i also want to opt for the best carreer options. Anybody experienced Network Programmer, please tell me is my... (5 Replies)
Discussion started by: vibhory2j
5 Replies
Login or Register to Ask a Question