Skip to content

Commit f0144ce

Browse files
committed
Updated examples
1 parent ee350cd commit f0144ce

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

README.md

+15-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@ func main(){
1515
// display welcome info.
1616
shell.Println("Sample Interactive Shell")
1717

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.
1928
shell.Register("exit", func(cmd string, args []string) (string, error) {
2029
shell.Stop()
2130
return "bye!", nil
@@ -28,11 +37,13 @@ func main(){
2837
Execution
2938
```
3039
Sample Interactive Shell
40+
>> greet Someone Somewhere
41+
Hello Someone Somewhere
3142
>> exit
3243
bye!
3344
```
3445

35-
#### Let's do more.
46+
#### Reading input.
3647
```go
3748
// simulate an authentication
3849
shell.Register("login", func(cmd string, args []string) (string, error) {
@@ -66,6 +77,8 @@ ishell is in active development and can still change significantly.
6677
* Support multiline inputs.
6778
* Handle ^C interrupts.
6879
* Support coloured outputs.
80+
* Command history.
81+
* Tab completion.
6982
* Testing, testing, testing.
7083

7184
### Contribution

example/main.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,27 @@ func main() {
1717
shell.Println("Do you want to do more ? y/n:")
1818
line, _ := shell.ReadLine()
1919
if strings.ToLower(line) == "y" {
20-
doMore(shell)
20+
doLogin(shell)
2121
}
2222
shell.Stop()
2323
return "bye!", nil
2424
})
2525

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+
2635
// start shell
2736
shell.Start()
2837
}
2938

30-
func doMore(shell *ishell.Shell) {
31-
shell.Stop()
39+
func doLogin(shell *ishell.Shell) {
40+
shell.Println("Let's simulate login")
3241

3342
// prompt for input
3443
shell.Println("Username:")

ishell.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func (s *Shell) Print(val ...interface{}) {
216216
fmt.Fprint(s.writer, val...)
217217
}
218218

219-
// Register registers a function for a command.
219+
// Register registers a function for command.
220220
func (s *Shell) Register(command string, function CmdFunc) {
221221
s.functions[command] = function
222222
}

0 commit comments

Comments
 (0)