cmd.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/chzyer/readline"
  5. "strings"
  6. )
  7. // 开发指令在控制台操作屏
  8. func lcCmdServer() {
  9. rl, err := readline.New("> ")
  10. if err != nil {
  11. fmt.Println("start cmd server Error:", err)
  12. return
  13. }
  14. defer rl.Close()
  15. for {
  16. var line []byte
  17. _, err := rl.Readline()
  18. if err != nil {
  19. fmt.Println("read line Error:", err)
  20. break
  21. }
  22. // 判断输入是否以"lc"作为前缀
  23. if strings.HasPrefix(string(line), "lc") {
  24. args := strings.Fields(string(line)) // 将输入按空格分割成多个字段
  25. if len(args) >= 2 {
  26. command := args[1]
  27. switch command {
  28. case "rm":
  29. handleRmCommand(args[2:])
  30. case "text":
  31. handleTextCommand(args[2:])
  32. default:
  33. fmt.Println("Unknown command:", command)
  34. }
  35. } else {
  36. fmt.Println("Missing command")
  37. }
  38. } else {
  39. fmt.Println("Invalid command")
  40. }
  41. }
  42. }
  43. func handleRmCommand(args []string) {
  44. // 处理rm指令
  45. for _, arg := range args {
  46. fmt.Println("Deleting:", arg)
  47. // 执行删除操作...
  48. }
  49. }
  50. func handleTextCommand(args []string) {
  51. // 处理text指令
  52. if len(args) > 0 {
  53. text := strings.Join(args, " ")
  54. fmt.Println("Text:", text)
  55. // 执行文本处理操作...
  56. } else {
  57. fmt.Println("Missing text argument")
  58. }
  59. }