From 37af41fbfeaf2e9830e25b658b8bed409fa6fae6 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Fri, 24 Oct 2025 20:21:35 -0700 Subject: [PATCH] Shell: Added a helper function `isHelpRequest()` (#7380) * Added a helper function `isHelpRequest()` * also handles combined short flags like -lh or -hl * Created handleHelpRequest() helper function encapsulates both: Checking for help flags Printing the help message * Limit to reasonable length (2-4 chars total) to avoid matching long options like -verbose --- weed/shell/command_fs_cat.go | 4 +++ weed/shell/command_fs_cd.go | 4 +++ weed/shell/command_fs_du.go | 4 +++ weed/shell/command_fs_ls.go | 4 +++ weed/shell/command_fs_meta_cat.go | 9 +++++-- weed/shell/command_fs_meta_notify.go | 4 +++ weed/shell/command_fs_mkdir.go | 9 +++++-- weed/shell/command_fs_mv.go | 4 +++ weed/shell/command_fs_pwd.go | 4 +++ weed/shell/command_fs_rm.go | 5 ++++ weed/shell/command_fs_tree.go | 4 +++ weed/shell/commands.go | 39 +++++++++++++++++++++++++--- 12 files changed, 87 insertions(+), 7 deletions(-) diff --git a/weed/shell/command_fs_cat.go b/weed/shell/command_fs_cat.go index facb126b8..99910d960 100644 --- a/weed/shell/command_fs_cat.go +++ b/weed/shell/command_fs_cat.go @@ -34,6 +34,10 @@ func (c *commandFsCat) HasTag(CommandTag) bool { func (c *commandFsCat) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + if handleHelpRequest(c, args, writer) { + return nil + } + path, err := commandEnv.parseUrl(findInputDirectory(args)) if err != nil { return err diff --git a/weed/shell/command_fs_cd.go b/weed/shell/command_fs_cd.go index 698865142..ef6cf6458 100644 --- a/weed/shell/command_fs_cd.go +++ b/weed/shell/command_fs_cd.go @@ -34,6 +34,10 @@ func (c *commandFsCd) HasTag(CommandTag) bool { func (c *commandFsCd) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + if handleHelpRequest(c, args, writer) { + return nil + } + path, err := commandEnv.parseUrl(findInputDirectory(args)) if err != nil { return err diff --git a/weed/shell/command_fs_du.go b/weed/shell/command_fs_du.go index 456f6bab6..b94869268 100644 --- a/weed/shell/command_fs_du.go +++ b/weed/shell/command_fs_du.go @@ -36,6 +36,10 @@ func (c *commandFsDu) HasTag(CommandTag) bool { func (c *commandFsDu) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + if handleHelpRequest(c, args, writer) { + return nil + } + path, err := commandEnv.parseUrl(findInputDirectory(args)) if err != nil { return err diff --git a/weed/shell/command_fs_ls.go b/weed/shell/command_fs_ls.go index 442702693..afa36ea3f 100644 --- a/weed/shell/command_fs_ls.go +++ b/weed/shell/command_fs_ls.go @@ -40,6 +40,10 @@ func (c *commandFsLs) HasTag(CommandTag) bool { func (c *commandFsLs) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + if handleHelpRequest(c, args, writer) { + return nil + } + var isLongFormat, showHidden bool for _, arg := range args { if !strings.HasPrefix(arg, "-") { diff --git a/weed/shell/command_fs_meta_cat.go b/weed/shell/command_fs_meta_cat.go index 2abb4d2b9..3e7eb0092 100644 --- a/weed/shell/command_fs_meta_cat.go +++ b/weed/shell/command_fs_meta_cat.go @@ -3,11 +3,12 @@ package shell import ( "context" "fmt" - "github.com/seaweedfs/seaweedfs/weed/filer" - "google.golang.org/protobuf/proto" "io" "sort" + "github.com/seaweedfs/seaweedfs/weed/filer" + "google.golang.org/protobuf/proto" + "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" "github.com/seaweedfs/seaweedfs/weed/util" ) @@ -37,6 +38,10 @@ func (c *commandFsMetaCat) HasTag(CommandTag) bool { func (c *commandFsMetaCat) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + if handleHelpRequest(c, args, writer) { + return nil + } + path, err := commandEnv.parseUrl(findInputDirectory(args)) if err != nil { return err diff --git a/weed/shell/command_fs_meta_notify.go b/weed/shell/command_fs_meta_notify.go index d7aca21d3..ea40b662d 100644 --- a/weed/shell/command_fs_meta_notify.go +++ b/weed/shell/command_fs_meta_notify.go @@ -36,6 +36,10 @@ func (c *commandFsMetaNotify) HasTag(CommandTag) bool { func (c *commandFsMetaNotify) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + if handleHelpRequest(c, args, writer) { + return nil + } + path, err := commandEnv.parseUrl(findInputDirectory(args)) if err != nil { return err diff --git a/weed/shell/command_fs_mkdir.go b/weed/shell/command_fs_mkdir.go index 9c33aa81c..49dc8a3f8 100644 --- a/weed/shell/command_fs_mkdir.go +++ b/weed/shell/command_fs_mkdir.go @@ -2,11 +2,12 @@ package shell import ( "context" - "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" - "github.com/seaweedfs/seaweedfs/weed/util" "io" "os" "time" + + "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" + "github.com/seaweedfs/seaweedfs/weed/util" ) func init() { @@ -33,6 +34,10 @@ func (c *commandFsMkdir) HasTag(CommandTag) bool { func (c *commandFsMkdir) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + if handleHelpRequest(c, args, writer) { + return nil + } + path, err := commandEnv.parseUrl(findInputDirectory(args)) if err != nil { return err diff --git a/weed/shell/command_fs_mv.go b/weed/shell/command_fs_mv.go index 2d44e4b58..8d6773513 100644 --- a/weed/shell/command_fs_mv.go +++ b/weed/shell/command_fs_mv.go @@ -40,6 +40,10 @@ func (c *commandFsMv) HasTag(CommandTag) bool { func (c *commandFsMv) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + if handleHelpRequest(c, args, writer) { + return nil + } + if len(args) != 2 { return fmt.Errorf("need to have 2 arguments") } diff --git a/weed/shell/command_fs_pwd.go b/weed/shell/command_fs_pwd.go index e74fb6c3d..65ce3fe7d 100644 --- a/weed/shell/command_fs_pwd.go +++ b/weed/shell/command_fs_pwd.go @@ -26,6 +26,10 @@ func (c *commandFsPwd) HasTag(CommandTag) bool { func (c *commandFsPwd) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + if handleHelpRequest(c, args, writer) { + return nil + } + fmt.Fprintf(writer, "%s\n", commandEnv.option.Directory) return nil diff --git a/weed/shell/command_fs_rm.go b/weed/shell/command_fs_rm.go index 2e3f19121..4f0848682 100644 --- a/weed/shell/command_fs_rm.go +++ b/weed/shell/command_fs_rm.go @@ -39,6 +39,11 @@ func (c *commandFsRm) HasTag(CommandTag) bool { } func (c *commandFsRm) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + + if handleHelpRequest(c, args, writer) { + return nil + } + isRecursive := false ignoreRecursiveError := false var entries []string diff --git a/weed/shell/command_fs_tree.go b/weed/shell/command_fs_tree.go index 628c95b30..e90572103 100644 --- a/weed/shell/command_fs_tree.go +++ b/weed/shell/command_fs_tree.go @@ -35,6 +35,10 @@ func (c *commandFsTree) HasTag(CommandTag) bool { func (c *commandFsTree) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) { + if handleHelpRequest(c, args, writer) { + return nil + } + path, err := commandEnv.parseUrl(findInputDirectory(args)) if err != nil { return err diff --git a/weed/shell/commands.go b/weed/shell/commands.go index 40be210a2..62dcfd7f8 100644 --- a/weed/shell/commands.go +++ b/weed/shell/commands.go @@ -3,13 +3,15 @@ package shell import ( "context" "fmt" - "github.com/seaweedfs/seaweedfs/weed/operation" - "github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb" - "github.com/seaweedfs/seaweedfs/weed/storage/needle_map" + "io" "net/url" "strconv" "strings" + "github.com/seaweedfs/seaweedfs/weed/operation" + "github.com/seaweedfs/seaweedfs/weed/pb/volume_server_pb" + "github.com/seaweedfs/seaweedfs/weed/storage/needle_map" + "google.golang.org/grpc" "github.com/seaweedfs/seaweedfs/weed/pb" @@ -147,6 +149,37 @@ func findInputDirectory(args []string) (input string) { return input } +// isHelpRequest checks if the args contain a help flag (-h, --help, or -help) +// It also handles combined short flags like -lh or -hl +func isHelpRequest(args []string) bool { + for _, arg := range args { + // Check for exact matches + if arg == "-h" || arg == "--help" || arg == "-help" { + return true + } + // Check for combined short flags (e.g., -lh, -hl, -rfh) + // Limit to reasonable length (2-4 chars total) to avoid matching long options like -verbose + if strings.HasPrefix(arg, "-") && !strings.HasPrefix(arg, "--") && len(arg) > 1 && len(arg) <= 4 { + for _, char := range arg[1:] { + if char == 'h' { + return true + } + } + } + } + return false +} + +// handleHelpRequest checks for help flags and prints the help message if requested. +// It returns true if the help message was printed, indicating the command should exit. +func handleHelpRequest(c command, args []string, writer io.Writer) bool { + if isHelpRequest(args) { + fmt.Fprintln(writer, c.Help()) + return true + } + return false +} + func readNeedleMeta(grpcDialOption grpc.DialOption, volumeServer pb.ServerAddress, volumeId uint32, needleValue needle_map.NeedleValue) (resp *volume_server_pb.ReadNeedleMetaResponse, err error) { err = operation.WithVolumeServerClient(false, volumeServer, grpcDialOption, func(client volume_server_pb.VolumeServerClient) error {