Loading dclish/dclish.go +43 −14 Original line number Diff line number Diff line Loading @@ -11,17 +11,16 @@ type ActionFunc func(*Command) error // Flag is a flag for a command. type Flag struct { Name string Value string Default string Description string } // Flags is the list of flags. type Flags []*Flag type Flags map[string]*Flag // Command contains the definition of a command, it's flags and subcommands. type Command struct { Command string Flags Flags Args []string Commands []*Command Loading @@ -30,24 +29,54 @@ type Command struct { } // Commands is the full list of commands. type Commands []*Command type Commands map[string]*Command // ParseAndRun parses a command line and runs the command. func (c Commands) ParseAndRun(line string) error { // TODO: this doesn't handle a DCL command line completely. words := strings.Fields(line) fmt.Printf("TODO ParseAndRun need to parse flags: %s\n", words) cmd := strings.ToUpper(words[0]) for i := range c { if c[i].Command == cmd { if c[i].Action == nil { fmt.Printf("Command not implemented:\n%s\n", c[i].Description) cmd, ok := c[strings.ToUpper(words[0])] if !ok { fmt.Printf("ERROR: Unknown command '%s'\n", words[0]) return nil } err := c[i].Action(c[i]) return err if cmd.Action == nil { fmt.Printf("ERROR: Command not implemented:\n%s\n", cmd.Description) return nil } for flg := range cmd.Flags { cmd.Flags[flg].Value = cmd.Flags[flg].Default } fmt.Printf("ERROR: Unknown command '%s'\n", cmd) return nil cmd.Args = []string{} if len(words) == 1 { return cmd.Action(cmd) } for i := range words[1:] { if strings.HasPrefix(words[i], "/") { flag, val, assigned := strings.Cut(words[i], "=") if assigned { wordup := strings.ToUpper(flag) flg, ok := cmd.Flags[wordup] if !ok { fmt.Printf("ERROR: Flag '%s' not recognised.", words[i]) } flg.Value = val } else { // TODO: handle toggle flag. wordup := strings.ToUpper(words[i]) value := "true" if strings.HasPrefix(wordup, "/NO") { wordup = strings.Replace(wordup, "/NO", "/", 1) value = "false" } flg, ok := cmd.Flags[wordup] if !ok { fmt.Printf("ERROR: Flag '%s' not recognised.", words[i]) } flg.Value = value } } } return cmd.Action(cmd) } repl/help.go 0 → 100644 +11 −0 Original line number Diff line number Diff line // Package repl implements the main event loop. package repl import ( "git.lyda.ie/kevin/bulletin/dclish" ) // ActionHelp handles the `HELP` command. func ActionHelp(_ *dclish.Command) error { return nil } Loading
dclish/dclish.go +43 −14 Original line number Diff line number Diff line Loading @@ -11,17 +11,16 @@ type ActionFunc func(*Command) error // Flag is a flag for a command. type Flag struct { Name string Value string Default string Description string } // Flags is the list of flags. type Flags []*Flag type Flags map[string]*Flag // Command contains the definition of a command, it's flags and subcommands. type Command struct { Command string Flags Flags Args []string Commands []*Command Loading @@ -30,24 +29,54 @@ type Command struct { } // Commands is the full list of commands. type Commands []*Command type Commands map[string]*Command // ParseAndRun parses a command line and runs the command. func (c Commands) ParseAndRun(line string) error { // TODO: this doesn't handle a DCL command line completely. words := strings.Fields(line) fmt.Printf("TODO ParseAndRun need to parse flags: %s\n", words) cmd := strings.ToUpper(words[0]) for i := range c { if c[i].Command == cmd { if c[i].Action == nil { fmt.Printf("Command not implemented:\n%s\n", c[i].Description) cmd, ok := c[strings.ToUpper(words[0])] if !ok { fmt.Printf("ERROR: Unknown command '%s'\n", words[0]) return nil } err := c[i].Action(c[i]) return err if cmd.Action == nil { fmt.Printf("ERROR: Command not implemented:\n%s\n", cmd.Description) return nil } for flg := range cmd.Flags { cmd.Flags[flg].Value = cmd.Flags[flg].Default } fmt.Printf("ERROR: Unknown command '%s'\n", cmd) return nil cmd.Args = []string{} if len(words) == 1 { return cmd.Action(cmd) } for i := range words[1:] { if strings.HasPrefix(words[i], "/") { flag, val, assigned := strings.Cut(words[i], "=") if assigned { wordup := strings.ToUpper(flag) flg, ok := cmd.Flags[wordup] if !ok { fmt.Printf("ERROR: Flag '%s' not recognised.", words[i]) } flg.Value = val } else { // TODO: handle toggle flag. wordup := strings.ToUpper(words[i]) value := "true" if strings.HasPrefix(wordup, "/NO") { wordup = strings.Replace(wordup, "/NO", "/", 1) value = "false" } flg, ok := cmd.Flags[wordup] if !ok { fmt.Printf("ERROR: Flag '%s' not recognised.", words[i]) } flg.Value = value } } } return cmd.Action(cmd) }
repl/help.go 0 → 100644 +11 −0 Original line number Diff line number Diff line // Package repl implements the main event loop. package repl import ( "git.lyda.ie/kevin/bulletin/dclish" ) // ActionHelp handles the `HELP` command. func ActionHelp(_ *dclish.Command) error { return nil }