Skip to content

SeaweedFS is a distributed storage system for object storage (S3), file systems, Iceberg table buckets, and Lance vector buckets, designed to handle billions of files with O(1) disk access and effortless horizontal scaling.

Shell: Added a helper function `isHelpRequest()` (#7380)

Chris Lu37af41fbfeaf
diff --git a/weed/shell/command_fs_cat.go b/weed/shell/command_fs_cat.go
index facb126b8c..99910d960c 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 6988651425..ef6cf64583 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 456f6bab63..b94869268e 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 442702693b..afa36ea3fe 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 2abb4d2b96..3e7eb00926 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 d7aca21d3b..ea40b662d0 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 9c33aa81ca..49dc8a3f8f 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 2d44e4b586..8d67735134 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 e74fb6c3dc..65ce3fe7d6 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 2e3f191216..4f0848682f 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 628c95b302..e905721032 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 40be210a29..62dcfd7f8c 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 {