RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(cli): list only accepted commands in \help (#21503)
Lim Jooyoung45ffe5c1073b
diff --git a/internal/cli/cli.go b/internal/cli/cli.goindex 7e06ebefcb..a07b43eb2c 100644--- a/internal/cli/cli.go+++ b/internal/cli/cli.go@@ -800,20 +800,14 @@ func (c *CLI) handleMetaCommand(commandCount int, cmd *Command) error {return nil}-func (c *CLI) printHelp() {- help := `+const helpText = `RAGFlow CLI Help================Meta Commands:- \admin - Switch to ADMIN mode (port 9381)- \user - Switch to USER mode (port 9380)- \host [ip] - Show or set server host (default: 127.0.0.1)- \port [num] - Show or set server port (default: 9380 for user, 9381 for admin)- \status - Show current connection status\? or \h - Show this help+ \pwd - Show current working directory\q or \quit - Exit CLI- \c or \clear - Clear screenCommands (User Mode):LOGIN USER 'email'; - Login as user@@ -824,24 +818,25 @@ Commands (User Mode):LIST DATASETS; - List user datasetsLIST AGENTS; - List user agentsLIST CHATS; - List user chats- LIST MODEL PROVIDERS; - List model providers+ LIST PROVIDERS; - List model providersLIST DEFAULT MODELS; - List default models- LIST TOKENS; - List API tokens- LIST PROVIDERS; - List available LLM providers- CREATE TOKEN; - Create new API token+ LIST KEYS; - List API keys+ LIST AVAILABLE PROVIDERS; - List available LLM providers+ CREATE KEY; - Create new API keyADD PROVIDER 'name'; - Create a provider without API key- ADD PROVIDER 'name' 'api_key'; - Create a provider with API key- DROP TOKEN 'token_value'; - Delete an API token+ ADD PROVIDER 'name' INSTANCE 'instance' KEY 'api_key'; - Create a provider instance with API key+ DROP KEY 'api_key'; - Delete an API keyDELETE PROVIDER 'name'; - Delete a provider- SET TOKEN 'token_value'; - Set and validate API token- SHOW TOKEN; - Show current API token+ SET KEY 'api_key'; - Set and validate API key+ SHOW KEY; - Show current API keySHOW PROVIDER 'name'; - Show provider details- SHOW CURRENT MODEL; - Show current model settings- UNSET TOKEN; - Remove current API token- ALTER PROVIDER 'name' NAME 'new_name'; - Rename a provider- USE MODEL 'provider/instance/model'; - Set current model for chat- CHAT 'message'; - Chat using current model- CHAT 'provider/instance/model' 'message'; - Chat with specified model+ SHOW CURRENT; - Show current model settings+ RESET KEY; - Remove current API key+ ALTER PROVIDER 'name' INSTANCE 'instance' NAME 'new_name';+ - Rename a provider instance+ USE MODEL 'model@instance@provider'; - Set current model for chat+ CHAT MESSAGE 'message'; - Chat using current model+ CHAT WITH 'model@instance@provider' MESSAGE 'message'; - Chat with specified modelOPENAI_CHAT 'chat_id' 'message' [options] ; - OpenAI-compatible chat(run openai_chat -h for detailed options)CHAT COMPLETIONS 'question' [options] ; - Chat completions via /api/v1/chat/completions@@ -875,15 +870,18 @@ Filesystem Commands (no quotes):Note: cat datasets or cat datasets/kb1 will errorExamples:- ragflow-cli -f rf.yml "LIST USERS" # SQL mode (with quotes)+ ragflow-cli -f rf.yml "LIST DATASETS" # SQL mode (with quotes)ragflow-cli -f rf.yml ls datasets # Filesystem mode (no quotes)ragflow-cli -f rf.yml ls files # List files in rootragflow-cli -f rf.yml cat datasets # Error: datasets is a directoryragflow-cli -f rf.yml ls files/myfolder # List folder contentsFor more information, see documentation.+`- fmt.Println(help)++func (c *CLI) printHelp() {+ fmt.Print(helpText)}// Cleanup performs cleanup before exitdiff --git a/internal/cli/cli_test.go b/internal/cli/cli_test.goindex 1af4a07122..c10f003025 100644--- a/internal/cli/cli_test.go+++ b/internal/cli/cli_test.go@@ -19,6 +19,11 @@ package cliimport ("errors""fmt"+ "go/ast"+ "go/parser"+ "go/token"+ "strconv"+ "strings""testing")@@ -82,3 +87,106 @@ func TestSanitizeCLIError_StripsSingleQuotedUserInput(t *testing.T) {})}}++// Every meta-command and user-mode statement listed by \help must be accepted+// as written, without dropping any of its quoted arguments, so users can copy+// commands from the help.+func TestHelpListsAcceptedCommands(t *testing.T) {+ userCommands := registeredUserCommands(t)+ section := ""+ for _, line := range strings.Split(helpText, "\n") {+ if line != "" && !strings.HasPrefix(line, " ") {+ section = line+ continue+ }+ entry := strings.TrimSpace(line)+ switch section {+ case "Meta Commands:":+ names, _, ok := strings.Cut(entry, " - ")+ if !ok {+ continue+ }+ for _, name := range strings.Split(names, " or ") {+ name = strings.TrimSpace(name)+ cmd, err := NewParser(name).Parse(APIMode)+ if err != nil || cmd == nil || cmd.Type != "meta" {+ t.Errorf("%s: parsed as %v, err = %v", name, cmd, err)+ continue+ }+ c := &CLI{running: true}+ if err := c.handleMetaCommand(1, cmd); err != nil {+ t.Errorf("%s: %v", name, err)+ }+ }+ case "Commands (User Mode):":+ stmt, _, ok := strings.Cut(entry, ";")+ if !ok {+ continue+ }+ stmt = strings.ReplaceAll(stmt, " [options]", "") + ";"+ p := NewParser(stmt)+ cmd, err := p.Parse(APIMode)+ if err != nil {+ t.Errorf("%s: %v", stmt, err)+ continue+ }+ if cmd == nil || !userCommands[cmd.Type] {+ t.Errorf("%s: no ExecuteUserCommand handler for %v", stmt, cmd)+ }+ for ; p.curToken.Type != TokenEOF; p.nextToken() {+ if p.curToken.Type == TokenQuotedString ||+ (strings.HasPrefix(stmt, "SHOW CURRENT") && isKeyword(p.curToken.Type)) {+ t.Errorf("%s: argument %q is ignored", stmt, p.curToken.Value)+ break+ }+ }+ }+ }+}++// Read the dispatcher's cases rather than duplicating its command list or+// invoking handlers that can contact a server or change CLI state.+func registeredUserCommands(t *testing.T) map[string]bool {+ t.Helper()+ file, err := parser.ParseFile(token.NewFileSet(), "cli_http.go", nil, 0)+ if err != nil {+ t.Fatal(err)+ }+ for _, decl := range file.Decls {+ fn, ok := decl.(*ast.FuncDecl)+ if !ok || fn.Recv == nil || fn.Name.Name != "ExecuteUserCommand" {+ continue+ }+ for _, stmt := range fn.Body.List {+ dispatch, ok := stmt.(*ast.SwitchStmt)+ if !ok {+ continue+ }+ selector, ok := dispatch.Tag.(*ast.SelectorExpr)+ if !ok || selector.Sel.Name != "Type" {+ continue+ }+ command, ok := selector.X.(*ast.Ident)+ if !ok || command.Name != "cmd" {+ continue+ }+ commands := make(map[string]bool)+ for _, stmt := range dispatch.Body.List {+ for _, expr := range stmt.(*ast.CaseClause).List {+ literal, ok := expr.(*ast.BasicLit)+ if !ok || literal.Kind != token.STRING {+ t.Fatalf("unexpected user command case: %T", expr)+ }+ name, err := strconv.Unquote(literal.Value)+ if err != nil {+ t.Fatal(err)+ }+ commands[name] = true+ }+ }+ return commands+ }+ }+ t.Fatal("ExecuteUserCommand cmd.Type switch not found")+ return nil+}