소스 검색

重构命令行参数处理代码

将命令行参数处理相关的函数和数据结构进行了重构,移除了不必要的文件,并统一了变量命名风格。更新了部分函数的调用方式以适应新的结构。
SongZihuan 1 주 전
부모
커밋
e6aa14295f

+ 1 - 0
CHANGELOG.md

@@ -21,6 +21,7 @@
 
 - 优化了人类可读日志的输出格式。
 - 部分原生 `panic` 语句改写为 `logger.Panic` 日志记录。
+- 优化命令行参数读取。
 
 ## [0.2.0] - 2025-04-16
 

+ 4 - 4
src/commandlineargs/define_data_type.go

@@ -11,7 +11,7 @@ import (
 	"github.com/SongZihuan/BackendServerTemplate/src/utils/osutils"
 )
 
-type CommandLineArgsDataType struct {
+type commandLineArgsDataType struct {
 	flagReady  bool
 	flagSet    bool
 	flagParser bool
@@ -53,7 +53,7 @@ type CommandLineArgsDataType struct {
 }
 
 func initData() {
-	CommandLineArgsData = CommandLineArgsDataType{
+	commandLineArgsData = commandLineArgsDataType{
 		flagReady:  false,
 		flagSet:    false,
 		flagParser: false,
@@ -94,10 +94,10 @@ func initData() {
 		Usage: "",
 	}
 
-	CommandLineArgsData.ready()
+	commandLineArgsData.ready()
 }
 
-func (d *CommandLineArgsDataType) setFlag() {
+func (d *commandLineArgsDataType) setFlag() {
 	if d.isFlagSet() {
 		return
 	}

+ 38 - 17
src/commandlineargs/export.go

@@ -3,6 +3,7 @@ package commandlineargs
 import (
 	"fmt"
 	"io"
+	"os"
 )
 
 var StopRun = fmt.Errorf("stop run and exit with success")
@@ -36,45 +37,41 @@ func InitCommandLineArgsParser(output io.Writer) (err error) {
 	return nil
 }
 
-func IsReady() bool {
-	return CommandLineArgsData.isReady() && isReady
-}
-
 func helpInfoRun() error {
 	var stopFlag = false
 
-	if OutputVersion() {
-		_, _ = PrintOutputVersion()
+	if commandLineArgsData.OutputVersion() {
+		_, _ = commandLineArgsData.PrintOutputVersion()
 		stopFlag = true
 		return StopRun
 	}
 
-	if Version() {
-		_, _ = PrintVersion()
+	if commandLineArgsData.Version() {
+		_, _ = commandLineArgsData.PrintVersion()
 		stopFlag = true
 	}
 
-	if License() {
+	if commandLineArgsData.License() {
 		if stopFlag {
-			_, _ = PrintLF()
+			_, _ = commandLineArgsData.PrintLF()
 		}
-		_, _ = PrintLicense()
+		_, _ = commandLineArgsData.PrintLicense()
 		stopFlag = true
 	}
 
-	if Report() {
+	if commandLineArgsData.Report() {
 		if stopFlag {
-			_, _ = PrintLF()
+			_, _ = commandLineArgsData.PrintLF()
 		}
-		_, _ = PrintReport()
+		_, _ = commandLineArgsData.PrintReport()
 		stopFlag = true
 	}
 
-	if Help() {
+	if commandLineArgsData.Help() {
 		if stopFlag {
-			_, _ = PrintLF()
+			_, _ = commandLineArgsData.PrintLF()
 		}
-		_, _ = PrintUsage()
+		_, _ = commandLineArgsData.PrintUsage()
 		stopFlag = true
 	}
 
@@ -84,3 +81,27 @@ func helpInfoRun() error {
 
 	return nil
 }
+
+func IsReady() bool {
+	return commandLineArgsData.isReady() && isReady
+}
+
+func Name() string {
+	return commandLineArgsData.Name()
+}
+
+func ConfigFile() string {
+	return commandLineArgsData.ConfigFile()
+}
+
+func OutputConfigFile() string {
+	return commandLineArgsData.OutputConfig()
+}
+
+func SetOutput(writer io.Writer) {
+	if writer == nil {
+		writer = os.Stdout
+	}
+
+	commandLineArgsData.SetOutput(writer)
+}

+ 9 - 9
src/commandlineargs/export_data.go

@@ -6,9 +6,9 @@ package commandlineargs
 
 import "github.com/SongZihuan/BackendServerTemplate/src/logger"
 
-var CommandLineArgsData CommandLineArgsDataType
+var commandLineArgsData commandLineArgsDataType
 
-func (d *CommandLineArgsDataType) Name() string {
+func (d *commandLineArgsDataType) Name() string {
 	if !d.isReady() {
 		logger.Panic("flag not ready")
 	}
@@ -16,7 +16,7 @@ func (d *CommandLineArgsDataType) Name() string {
 	return d.NameData
 }
 
-func (d *CommandLineArgsDataType) Help() bool {
+func (d *commandLineArgsDataType) Help() bool {
 	if !d.isReady() {
 		logger.Panic("flag not ready")
 	}
@@ -24,26 +24,26 @@ func (d *CommandLineArgsDataType) Help() bool {
 	return d.HelpData
 }
 
-func (d *CommandLineArgsDataType) Version() bool {
+func (d *commandLineArgsDataType) Version() bool {
 	return getData(d, d.VersionData)
 }
 
-func (d *CommandLineArgsDataType) OutputVersion() bool {
+func (d *commandLineArgsDataType) OutputVersion() bool {
 	return getData(d, d.OutputVersionData)
 }
 
-func (d *CommandLineArgsDataType) License() bool {
+func (d *commandLineArgsDataType) License() bool {
 	return getData(d, d.LicenseData)
 }
 
-func (d *CommandLineArgsDataType) Report() bool {
+func (d *commandLineArgsDataType) Report() bool {
 	return getData(d, d.ReportData)
 }
 
-func (d *CommandLineArgsDataType) ConfigFile() string {
+func (d *commandLineArgsDataType) ConfigFile() string {
 	return getData(d, d.ConfigFileData)
 }
 
-func (d *CommandLineArgsDataType) OutputConfig() string {
+func (d *commandLineArgsDataType) OutputConfig() string {
 	return getData(d, d.ConfigOutputFileData)
 }

+ 1 - 1
src/commandlineargs/export_method.go

@@ -10,7 +10,7 @@ import (
 	"os"
 )
 
-func (d *CommandLineArgsDataType) SetOutput(writer io.Writer) {
+func (d *commandLineArgsDataType) SetOutput(writer io.Writer) {
 	if writer == nil {
 		writer = os.Stdout
 	}

+ 12 - 12
src/commandlineargs/export_printer.go

@@ -16,15 +16,15 @@ import (
 	"time"
 )
 
-func (d *CommandLineArgsDataType) FprintUsage(writer io.Writer) (int, error) {
+func (d *commandLineArgsDataType) FprintUsage(writer io.Writer) (int, error) {
 	return fmt.Fprintf(writer, "%s\n", d.Usage)
 }
 
-func (d *CommandLineArgsDataType) PrintUsage() (int, error) {
+func (d *commandLineArgsDataType) PrintUsage() (int, error) {
 	return d.FprintUsage(flag.CommandLine.Output())
 }
 
-func (d *CommandLineArgsDataType) FprintVersion(writer io.Writer) (int, error) {
+func (d *commandLineArgsDataType) FprintVersion(writer io.Writer) (int, error) {
 	res := new(strings.Builder)
 	res.WriteString(fmt.Sprintf("Version of %s: %s\n", osutils.GetArgs0Name(), global.Version))
 	res.WriteString(fmt.Sprintf("Build Date (UTC): %s\n", global.BuildTime.In(time.UTC).Format(time.DateTime)))
@@ -40,42 +40,42 @@ func (d *CommandLineArgsDataType) FprintVersion(writer io.Writer) (int, error) {
 	return fmt.Fprintf(writer, "%s\n", version)
 }
 
-func (d *CommandLineArgsDataType) PrintVersion() (int, error) {
+func (d *commandLineArgsDataType) PrintVersion() (int, error) {
 	return d.FprintVersion(flag.CommandLine.Output())
 }
 
-func (d *CommandLineArgsDataType) FprintOutputVersion(writer io.Writer) (int, error) {
+func (d *commandLineArgsDataType) FprintOutputVersion(writer io.Writer) (int, error) {
 	return fmt.Fprintf(writer, "%s", global.SemanticVersioning)
 }
 
-func (d *CommandLineArgsDataType) PrintOutputVersion() (int, error) {
+func (d *commandLineArgsDataType) PrintOutputVersion() (int, error) {
 	return d.FprintOutputVersion(flag.CommandLine.Output())
 }
 
-func (d *CommandLineArgsDataType) FprintLicense(writer io.Writer) (int, error) {
+func (d *commandLineArgsDataType) FprintLicense(writer io.Writer) (int, error) {
 	title := formatutils.FormatTextToWidth(fmt.Sprintf("License of %s:", osutils.GetArgs0Name()), formatutils.NormalConsoleWidth)
 	license := formatutils.FormatTextToWidth(global.License, formatutils.NormalConsoleWidth)
 	return fmt.Fprintf(writer, "%s\n%s\n", title, license)
 }
 
-func (d *CommandLineArgsDataType) PrintLicense() (int, error) {
+func (d *commandLineArgsDataType) PrintLicense() (int, error) {
 	return d.FprintLicense(flag.CommandLine.Output())
 }
 
-func (d *CommandLineArgsDataType) FprintReport(writer io.Writer) (int, error) {
+func (d *commandLineArgsDataType) FprintReport(writer io.Writer) (int, error) {
 	// 不需要title
 	report := formatutils.FormatTextToWidth(global.Report, formatutils.NormalConsoleWidth)
 	return fmt.Fprintf(writer, "%s\n", report)
 }
 
-func (d *CommandLineArgsDataType) PrintReport() (int, error) {
+func (d *commandLineArgsDataType) PrintReport() (int, error) {
 	return d.FprintReport(flag.CommandLine.Output())
 }
 
-func (d *CommandLineArgsDataType) FprintLF(writer io.Writer) (int, error) {
+func (d *commandLineArgsDataType) FprintLF(writer io.Writer) (int, error) {
 	return fmt.Fprintf(writer, "\n")
 }
 
-func (d *CommandLineArgsDataType) PrintLF() (int, error) {
+func (d *commandLineArgsDataType) PrintLF() (int, error) {
 	return d.FprintLF(flag.CommandLine.Output())
 }

+ 7 - 7
src/commandlineargs/internal_data_type_method.go

@@ -16,7 +16,7 @@ const OptionPrefix = "--"
 const OptionShortPrefix = "-"
 const UsagePrefixWidth = 10
 
-func (d *CommandLineArgsDataType) ready() {
+func (d *commandLineArgsDataType) ready() {
 	if d.isReady() {
 		return
 	}
@@ -27,7 +27,7 @@ func (d *CommandLineArgsDataType) ready() {
 	d.flagReady = true
 }
 
-func (d *CommandLineArgsDataType) writeUsage() {
+func (d *commandLineArgsDataType) writeUsage() {
 	if len(d.Usage) != 0 {
 		return
 	}
@@ -168,7 +168,7 @@ func (d *CommandLineArgsDataType) writeUsage() {
 	d.Usage = strings.TrimRight(result.String(), "\n")
 }
 
-func (d *CommandLineArgsDataType) parser() {
+func (d *commandLineArgsDataType) parser() {
 	if d.flagParser {
 		return
 	}
@@ -181,19 +181,19 @@ func (d *CommandLineArgsDataType) parser() {
 	d.flagParser = true
 }
 
-func (d *CommandLineArgsDataType) isReady() bool {
+func (d *commandLineArgsDataType) isReady() bool {
 	return d.isFlagSet() && d.isFlagParser() && d.flagReady
 }
 
-func (d *CommandLineArgsDataType) isFlagSet() bool {
+func (d *commandLineArgsDataType) isFlagSet() bool {
 	return d.flagSet
 }
 
-func (d *CommandLineArgsDataType) isFlagParser() bool {
+func (d *commandLineArgsDataType) isFlagParser() bool {
 	return d.flagParser
 }
 
-func getData[T any](d *CommandLineArgsDataType, data T) T { // 泛型函数无法作为 “方法” 只能作为函数
+func getData[T any](d *commandLineArgsDataType, data T) T { // 泛型函数无法作为 “方法” 只能作为函数
 	if !d.isReady() {
 		logger.Panic("flag not ready")
 	}

+ 0 - 70
src/commandlineargs/main.go

@@ -1,70 +0,0 @@
-package commandlineargs
-
-import (
-	"io"
-	"os"
-)
-
-func Name() string {
-	return CommandLineArgsData.NameData
-}
-
-func Help() bool {
-	return CommandLineArgsData.Help()
-}
-
-func PrintUsage() (int, error) {
-	return CommandLineArgsData.PrintUsage()
-}
-
-func PrintVersion() (int, error) {
-	return CommandLineArgsData.PrintVersion()
-}
-
-func PrintOutputVersion() (int, error) {
-	return CommandLineArgsData.PrintOutputVersion()
-}
-
-func PrintLicense() (int, error) {
-	return CommandLineArgsData.PrintLicense()
-}
-
-func PrintReport() (int, error) {
-	return CommandLineArgsData.PrintReport()
-}
-
-func PrintLF() (int, error) {
-	return CommandLineArgsData.PrintLF()
-}
-
-func Version() bool {
-	return CommandLineArgsData.Version()
-}
-
-func OutputVersion() bool {
-	return CommandLineArgsData.OutputVersion()
-}
-
-func License() bool {
-	return CommandLineArgsData.License()
-}
-
-func Report() bool {
-	return CommandLineArgsData.Report()
-}
-
-func ConfigFile() string {
-	return CommandLineArgsData.ConfigFile()
-}
-
-func OutputConfigFile() string {
-	return CommandLineArgsData.OutputConfig()
-}
-
-func SetOutput(writer io.Writer) {
-	if writer == nil {
-		writer = os.Stdout
-	}
-
-	CommandLineArgsData.SetOutput(writer)
-}