From 3c74bcdf2a7338e4ae16f5d0fe26baf00eb705e0 Mon Sep 17 00:00:00 2001 From: Meo597 <197331664+Meo597@users.noreply.github.com> Date: Mon, 12 Oct 2026 22:20:45 +0800 Subject: [PATCH] cmd: refine --- main/commands/all/base64/decode.go | 12 +++++------- main/commands/all/base64/encode.go | 8 ++++---- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/main/commands/all/base64/decode.go b/main/commands/all/base64/decode.go index 1d8b4eb69..a3981e1d7 100644 --- a/main/commands/all/base64/decode.go +++ b/main/commands/all/base64/decode.go @@ -11,10 +11,10 @@ import ( ) var cmdDecode = &base.Command{ - UsageLine: "{{.Exec}} base64 decode [-gzip] [value | - | stdin:] [output-file]", + UsageLine: "{{.Exec}} base64 decode [-gzip] [output-file]", Short: "Decode Base64", Long: fmt.Sprintf(` -Decode a Base64 value; omit value or pass "-" or "stdin:" to read stdin. +Decode a Base64 value, or pass "-" or "stdin:" to read stdin. Write to output-file, or stdout if omitted. Accepts %s, %s, or no prefix; gzip is enabled by %s or -gzip. @@ -32,19 +32,17 @@ func init() { } func executeDecode(cmd *base.Command, args []string) { - if len(args) > 2 { + if len(args) == 0 || len(args) > 2 { cmd.Usage() } - var value string - if len(args) == 0 || args[0] == "-" || args[0] == "stdin:" { + value := args[0] + if value == "-" || value == "stdin:" { input, err := io.ReadAll(os.Stdin) if err != nil { base.Fatalf("failed to read input: %s", err) } value = string(input) - } else { - value = args[0] } value = strings.TrimSpace(value) diff --git a/main/commands/all/base64/encode.go b/main/commands/all/base64/encode.go index 0340c6c43..cfa86fb68 100644 --- a/main/commands/all/base64/encode.go +++ b/main/commands/all/base64/encode.go @@ -10,11 +10,11 @@ import ( ) var cmdEncode = &base.Command{ - UsageLine: "{{.Exec}} base64 encode [-gzip] [input-file | - | stdin:]", + UsageLine: "{{.Exec}} base64 encode [-gzip] ", Short: "Encode as Base64", Long: fmt.Sprintf(` Encode input-file as Base64 with the %s prefix. -Omit input-file or pass "-" or "stdin:" to read stdin. +Pass "-" or "stdin:" to read stdin. Use -gzip to compress and output %s. Examples: @@ -31,13 +31,13 @@ func init() { } func executeEncode(cmd *base.Command, args []string) { - if len(args) > 1 { + if len(args) != 1 { cmd.Usage() } var data []byte var err error - if len(args) == 0 || args[0] == "-" || args[0] == "stdin:" { + if args[0] == "-" || args[0] == "stdin:" { data, err = io.ReadAll(os.Stdin) } else { data, err = os.ReadFile(args[0])