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
This commit is contained in:
Chris Lu
2025-10-24 20:21:35 -07:00
committed by GitHub
parent 922bb17194
commit 37af41fbfe
12 changed files with 87 additions and 7 deletions

View File

@@ -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 {