Readline interface: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Added Perl example)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 43:
puts("Yes...?");
}
}</lang>
 
=={{header|C sharp|C#}}==
{{trans|D}}
<lang csharp>using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
 
namespace ReadlineInterface {
class Program {
static LinkedList<string> histArr = new LinkedList<string>();
 
static void AppendHistory([CallerMemberName] string name = "unknown") {
histArr.AddLast(name);
}
 
static void Hist() {
if (histArr.Count == 0) {
Console.WriteLine("No history");
}
else {
foreach (string cmd in histArr) {
Console.WriteLine(" - {0}", cmd);
}
}
AppendHistory();
}
 
static void Hello() {
Console.WriteLine("Hello World!");
AppendHistory();
}
 
static void Help() {
Console.WriteLine("Available commands:");
Console.WriteLine(" hello");
Console.WriteLine(" hist");
Console.WriteLine(" exit");
Console.WriteLine(" help");
AppendHistory();
}
 
static void Main(string[] args) {
Dictionary<string, Action> cmdDict = new Dictionary<string, Action>();
cmdDict.Add("help", Help);
cmdDict.Add("hist", Hist);
cmdDict.Add("hello", Hello);
 
Console.WriteLine("Enter a command, type help for a listing.");
while (true) {
Console.Write(">");
string line = Console.ReadLine();
if (line=="exit") {
break;
}
 
Action action;
if (cmdDict.TryGetValue(line, out action)) {
action.Invoke();
} else {
Help();
}
}
}
}
}</lang>
 
Line 109 ⟶ 174:
 
return 0;
}</lang>
 
=={{header|C#|C sharp}}==
{{trans|D}}
<lang csharp>using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
 
namespace ReadlineInterface {
class Program {
static LinkedList<string> histArr = new LinkedList<string>();
 
static void AppendHistory([CallerMemberName] string name = "unknown") {
histArr.AddLast(name);
}
 
static void Hist() {
if (histArr.Count == 0) {
Console.WriteLine("No history");
}
else {
foreach (string cmd in histArr) {
Console.WriteLine(" - {0}", cmd);
}
}
AppendHistory();
}
 
static void Hello() {
Console.WriteLine("Hello World!");
AppendHistory();
}
 
static void Help() {
Console.WriteLine("Available commands:");
Console.WriteLine(" hello");
Console.WriteLine(" hist");
Console.WriteLine(" exit");
Console.WriteLine(" help");
AppendHistory();
}
 
static void Main(string[] args) {
Dictionary<string, Action> cmdDict = new Dictionary<string, Action>();
cmdDict.Add("help", Help);
cmdDict.Add("hist", Hist);
cmdDict.Add("hello", Hello);
 
Console.WriteLine("Enter a command, type help for a listing.");
while (true) {
Console.Write(">");
string line = Console.ReadLine();
if (line=="exit") {
break;
}
 
Action action;
if (cmdDict.TryGetValue(line, out action)) {
action.Invoke();
} else {
Help();
}
}
}
}
}</lang>
 
Line 759:
return undef;
}</lang>
 
=={{header|Perl 6}}==
 
Perl 6 has a built in REPL that can be initiated by running the perl6 executable without any parameters. It is fairly basic on its own but here are bindings available in the Perl 6 ecosystem for [https://tiswww.case.edu/php/chet/readline/rltop.html GNU Readline] and/or [https://github.com/antirez/linenoise Linenoise] either of which will automatically provide command history, tab completion and more advance command line editing capability if installed. They are not included in the Perl 6 distribution directly. There are incompatible licensing requirements and providing hooks for third party tools allows for more customization options.
 
Linenoise is generally the preferred option unless you really want the emacs compatible command line editing key bindings. Readline is arguably more powerful but is somewhat fiddly to set up.
 
If you are not inclined to install Readline ''or'' Linenoise, the REPL also works fairly well with 3rd party tools like rlwrap.
 
=={{header|Phix}}==
Line 909 ⟶ 901:
=={{header|Racket}}==
Racket's has a readline interface which is not used by default due to its license. This includes the usual readline-style editing, and tab-completion for Racket bindings. It is possible to use it as a library, or as a REPL convenience (both uses described [http://docs.racket-lang.org/readline/ in the documentation]) -- but it is better to use [http://docs.racket-lang.org/xrepl/ xrepl] which provides an enhanced command-line REPL and includes the readline interaction.
 
=={{header|Raku}}==
(formerly Perl 6)
 
Perl 6 has a built in REPL that can be initiated by running the perl6 executable without any parameters. It is fairly basic on its own but here are bindings available in the Perl 6 ecosystem for [https://tiswww.case.edu/php/chet/readline/rltop.html GNU Readline] and/or [https://github.com/antirez/linenoise Linenoise] either of which will automatically provide command history, tab completion and more advance command line editing capability if installed. They are not included in the Perl 6 distribution directly. There are incompatible licensing requirements and providing hooks for third party tools allows for more customization options.
 
Linenoise is generally the preferred option unless you really want the emacs compatible command line editing key bindings. Readline is arguably more powerful but is somewhat fiddly to set up.
 
If you are not inclined to install Readline ''or'' Linenoise, the REPL also works fairly well with 3rd party tools like rlwrap.
 
=={{header|REXX}}==
10,333

edits