File tree 3 files changed +28
-6
lines changed
3 files changed +28
-6
lines changed Original file line number Diff line number Diff line change @@ -15,7 +15,16 @@ func main(){
15
15
// display welcome info.
16
16
shell.Println (" Sample Interactive Shell" )
17
17
18
- // register a function for "exit" command.
18
+ // register a function for "greet" command.
19
+ shell.Register (" greet" , func (cmd string , args []string ) (string , error ) {
20
+ name := " Stranger"
21
+ if len (args) > 0 {
22
+ name = strings.Join (args, " " )
23
+ }
24
+ return " Hello " +name, nil
25
+ })
26
+
27
+ // register a function for "exit" command.
19
28
shell.Register (" exit" , func (cmd string , args []string ) (string , error ) {
20
29
shell.Stop ()
21
30
return " bye!" , nil
@@ -28,11 +37,13 @@ func main(){
28
37
Execution
29
38
```
30
39
Sample Interactive Shell
40
+ >> greet Someone Somewhere
41
+ Hello Someone Somewhere
31
42
>> exit
32
43
bye!
33
44
```
34
45
35
- #### Let's do more .
46
+ #### Reading input .
36
47
``` go
37
48
// simulate an authentication
38
49
shell.Register (" login" , func (cmd string , args []string ) (string , error ) {
@@ -66,6 +77,8 @@ ishell is in active development and can still change significantly.
66
77
* Support multiline inputs.
67
78
* Handle ^C interrupts.
68
79
* Support coloured outputs.
80
+ * Command history.
81
+ * Tab completion.
69
82
* Testing, testing, testing.
70
83
71
84
### Contribution
Original file line number Diff line number Diff line change @@ -17,18 +17,27 @@ func main() {
17
17
shell .Println ("Do you want to do more ? y/n:" )
18
18
line , _ := shell .ReadLine ()
19
19
if strings .ToLower (line ) == "y" {
20
- doMore (shell )
20
+ doLogin (shell )
21
21
}
22
22
shell .Stop ()
23
23
return "bye!" , nil
24
24
})
25
25
26
+ // register a function for "greet" command.
27
+ shell .Register ("greet" , func (cmd string , args []string ) (string , error ) {
28
+ name := "Stranger"
29
+ if len (args ) > 0 {
30
+ name = strings .Join (args , " " )
31
+ }
32
+ return "Hello " + name , nil
33
+ })
34
+
26
35
// start shell
27
36
shell .Start ()
28
37
}
29
38
30
- func doMore (shell * ishell.Shell ) {
31
- shell .Stop ( )
39
+ func doLogin (shell * ishell.Shell ) {
40
+ shell .Println ( "Let's simulate login" )
32
41
33
42
// prompt for input
34
43
shell .Println ("Username:" )
Original file line number Diff line number Diff line change @@ -216,7 +216,7 @@ func (s *Shell) Print(val ...interface{}) {
216
216
fmt .Fprint (s .writer , val ... )
217
217
}
218
218
219
- // Register registers a function for a command.
219
+ // Register registers a function for command.
220
220
func (s * Shell ) Register (command string , function CmdFunc ) {
221
221
s .functions [command ] = function
222
222
}
You can’t perform that action at this time.
0 commit comments