Created
January 14, 2024 01:20
-
-
Save nasitra/f45a0b61427c89dfaf0121f6ef87c2d5 to your computer and use it in GitHub Desktop.
Handle command line input implemented with Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"github.com/mattn/go-shellwords" | |
"github.com/peterh/liner" | |
) | |
func handle(args []string) error { | |
/* TODO */ | |
fmt.Println(args) | |
return nil | |
} | |
func main() { | |
line := liner.NewLiner() | |
defer line.Close() | |
line.SetCtrlCAborts(true) | |
for { | |
prompt := "> " | |
l, err := line.Prompt(prompt) | |
if err != nil { | |
break | |
} | |
line.AppendHistory(l) | |
args, err := shellwords.Parse(l) | |
if err != nil { | |
fmt.Fprintln(os.Stderr, err) | |
continue | |
} | |
if len(args) == 0 { | |
continue | |
} | |
err = handle(args) | |
if err != nil { | |
fmt.Fprintln(os.Stderr, err) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment