diff options
| author | Chris Lu <chrislusf@users.noreply.github.com> | 2025-10-24 20:21:35 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-24 20:21:35 -0700 |
| commit | 37af41fbfeaf2e9830e25b658b8bed409fa6fae6 (patch) | |
| tree | 4ac4d610f83ab3279072593d2d143e1c8a89ba28 /weed/shell/commands.go | |
| parent | 922bb171944190dc8a7c9a563cd95da39d9c2ebb (diff) | |
| download | seaweedfs-37af41fbfeaf2e9830e25b658b8bed409fa6fae6.tar.xz seaweedfs-37af41fbfeaf2e9830e25b658b8bed409fa6fae6.zip | |
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
Diffstat (limited to 'weed/shell/commands.go')
| -rw-r--r-- | weed/shell/commands.go | 39 |
1 files changed, 36 insertions, 3 deletions
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 { |
