| 1 |
#Region "Microsoft.VisualBasic::581f46ffd31dd39cc2a9383be95c77ba, Microsoft.VisualBasic.Core\CommandLine\Interpreters\InteractiveConsole.vb"
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
#End Region
|
| 46 |
|
| 47 |
Imports System.IO
|
| 48 |
Imports Microsoft.VisualBasic.ApplicationServices.Terminal
|
| 49 |
Imports Microsoft.VisualBasic.Language
|
| 50 |
Imports Microsoft.VisualBasic.Language.UnixBash
|
| 51 |
Imports Microsoft.VisualBasic.Linq
|
| 52 |
Imports Microsoft.VisualBasic.Scripting.Expressions
|
| 53 |
|
| 54 |
Namespace CommandLine
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
```bash
|
| 60 |
# This command will makes your CLI program enter
|
| 61 |
# the interactive console mode.
|
| 62 |
#
|
| 63 |
# type quit() for exit.
|
| 64 |
#
|
| 65 |
App /i
|
| 66 |
```
|
| 67 |
</summary>
|
| 68 |
<remarks></remarks>
|
| 69 |
Public NotInheritable Class InteractiveConsole : Inherits Interpreter
|
| 70 |
|
| 71 |
Sub New(App As Type)
|
| 72 |
Call MyBase.New(App)
|
| 73 |
End Sub
|
| 74 |
|
| 75 |
Public Function RunApp() As Integer
|
| 76 |
Dim input As Value(Of String) = ""
|
| 77 |
Dim ps1 As PS1 = PS1.Fedora12
|
| 78 |
|
| 79 |
Call Console.Write(ps1.ToString)
|
| 80 |
Call Console.Write(" ")
|
| 81 |
|
| 82 |
Do While Not (input = Console.ReadLine).TextEquals("quit()")
|
| 83 |
With input.Value
|
| 84 |
If Not .StringEmpty Then
|
| 85 |
Call RunAppInternal(CLITools.TryParse(.ByRef))
|
| 86 |
End If
|
| 87 |
End With
|
| 88 |
|
| 89 |
Call Console.Write(ps1.ToString)
|
| 90 |
Call Console.Write(" ")
|
| 91 |
Loop
|
| 92 |
|
| 93 |
Return 0
|
| 94 |
End Function
|
| 95 |
|
| 96 |
|
| 97 |
Contains sevral build in command about file system operation and the program CLI interpreter commands
|
| 98 |
</summary>
|
| 99 |
<param name="cmd"></param>
|
| 100 |
Private Sub RunAppInternal(cmd As CommandLine)
|
| 101 |
Select Case cmd.Name.ToLower
|
| 102 |
|
| 103 |
Case "ls"
|
| 104 |
|
| 105 |
Dim directory$ = If(cmd.Tokens.ElementAtOrDefault(1), App.CurrentDirectory)
|
| 106 |
Dim directories = FileIO.FileSystem.GetDirectories(directory)
|
| 107 |
Dim files = FileIO.FileSystem.GetFiles(directory)
|
| 108 |
|
| 109 |
For Each dir As String In directories
|
| 110 |
Call Console.WriteLine("<directory> " & dir.Replace("\"c, "/"c).Split("/"c).Last)
|
| 111 |
Next
|
| 112 |
|
| 113 |
Dim table = files _
|
| 114 |
.Select(Function(path)
|
| 115 |
Return {
|
| 116 |
path.Replace("\"c, "/"c).Split("/"c).Last,
|
| 117 |
New FileInfo(path).Length & " Bytes"
|
| 118 |
}
|
| 119 |
End Function) _
|
| 120 |
.ToArray
|
| 121 |
|
| 122 |
Call PrintAsTable.Print(table, New StreamWriter(Console.OpenStandardOutput))
|
| 123 |
|
| 124 |
Case "cd"
|
| 125 |
|
| 126 |
App.CurrentDirectory = cmd.Tokens.ElementAtOrDefault(1)
|
| 127 |
|
| 128 |
Case "cat"
|
| 129 |
|
| 130 |
Call Console.WriteLine(cmd.Tokens.ElementAtOrDefault(1).ReadAllText)
|
| 131 |
|
| 132 |
Case "help"
|
| 133 |
|
| 134 |
Call MyBase.Execute(args:=New CommandLine With {.Name = "?"})
|
| 135 |
|
| 136 |
Case "/@set"
|
| 137 |
|
| 138 |
|
| 139 |
Dim var$ = cmd.Tokens.ElementAtOrDefault(1)
|
| 140 |
Dim value$ = cmd _
|
| 141 |
.Tokens _
|
| 142 |
.ElementAtOrDefault(2) _
|
| 143 |
.Interpolate(AddressOf App.GetVariable, escape:=False)
|
| 144 |
|
| 145 |
Call App.JoinVariable(var, value)
|
| 146 |
|
| 147 |
Case "/@get"
|
| 148 |
|
| 149 |
|
| 150 |
Call Console.WriteLine(App.GetVariable(cmd.Tokens.ElementAtOrDefault(1)))
|
| 151 |
|
| 152 |
Case Else
|
| 153 |
|
| 154 |
Call MyBase.Execute(args:=cmd)
|
| 155 |
|
| 156 |
End Select
|
| 157 |
End Sub
|
| 158 |
End Class
|
| 159 |
End Namespace
|