From c3c5276c9a38bf76fb6a61a116f3ff338747b320 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Mon, 2 Dec 2024 14:56:24 +0800 Subject: [PATCH] gogensig:assign cfg path --- _xtool/llcppsigfetch/llcppsigfetch.go | 23 +++---------- _xtool/llcppsymg/_cmptest/args_test/args.go | 2 +- _xtool/llcppsymg/args/args.go | 34 +++++++++++++++++-- _xtool/llcppsymg/llcppsymg.go | 2 +- cmd/gogensig/gogensig.go | 37 ++++++++++++++------- llcppg.go | 2 +- 6 files changed, 63 insertions(+), 37 deletions(-) diff --git a/_xtool/llcppsigfetch/llcppsigfetch.go b/_xtool/llcppsigfetch/llcppsigfetch.go index afbc67b..d6bf6e4 100644 --- a/_xtool/llcppsigfetch/llcppsigfetch.go +++ b/_xtool/llcppsigfetch/llcppsigfetch.go @@ -20,7 +20,6 @@ import ( "fmt" "io" "os" - "strconv" "strings" "github.com/goplus/llcppg/_xtool/llcppsigfetch/parse" @@ -33,7 +32,7 @@ import ( ) func main() { - ags, remainArgs := args.ParseArgs(os.Args[1:], map[string]bool{ + ags, remainArgs := args.ParseArgs(os.Args[1:], args.LLCPPG_CFG, map[string]bool{ "--extract": true, }) @@ -66,11 +65,11 @@ func main() { os.Exit(1) } case strings.HasPrefix(arg, "-out="): - out = parseBoolArg(arg, "out", false) + out = args.BoolArg(arg, false) case strings.HasPrefix(arg, "-temp="): - isTemp = parseBoolArg(arg, "temp", false) + isTemp = args.BoolArg(arg, false) case strings.HasPrefix(arg, "-cpp="): - isCpp = parseBoolArg(arg, "cpp", true) + isCpp = args.BoolArg(arg, true) default: otherArgs = append(otherArgs, arg) } @@ -223,17 +222,3 @@ func outputInfo(context *parse.Context, outputToFile bool) { defer info.Delete() outputResult(str, outputToFile) } - -func parseBoolArg(arg, name string, defaultValue bool) bool { - parts := strings.SplitN(arg, "=", 2) - if len(parts) != 2 { - fmt.Fprintf(os.Stderr, "Warning: Invalid -%s= argument, defaulting to %v\n", name, defaultValue) - return defaultValue - } - value, err := strconv.ParseBool(parts[1]) - if err != nil { - fmt.Fprintf(os.Stderr, "Warning: Invalid -%s= value '%s', defaulting to %v\n", name, parts[1], defaultValue) - return defaultValue - } - return value -} diff --git a/_xtool/llcppsymg/_cmptest/args_test/args.go b/_xtool/llcppsymg/_cmptest/args_test/args.go index 74b9494..e8a15f5 100644 --- a/_xtool/llcppsymg/_cmptest/args_test/args.go +++ b/_xtool/llcppsymg/_cmptest/args_test/args.go @@ -51,7 +51,7 @@ func TestParseArgs() { fmt.Printf("Test case: %s\n", tc.name) fmt.Printf("Input: %v\n", tc.input) - result, filteredArgs := args.ParseArgs(tc.input, swflags) + result, filteredArgs := args.ParseArgs(tc.input, args.LLCPPG_CFG, swflags) fmt.Printf("Help: %v\n", result.Help) fmt.Printf("Verbose: %v\n", result.Verbose) diff --git a/_xtool/llcppsymg/args/args.go b/_xtool/llcppsymg/args/args.go index 76d1eaa..02bc425 100644 --- a/_xtool/llcppsymg/args/args.go +++ b/_xtool/llcppsymg/args/args.go @@ -1,6 +1,14 @@ package args -import "strings" +import ( + "strconv" + "strings" +) + +const LLCPPG_CFG = "llcppg.cfg" +const LLCPPG_SYMB = "llcppg.symb.json" +const LLCPPG_SIGFETCH = "llcppg.sigfetch.json" +const LLCPPG_PUB = "llcppg.pub" type Args struct { Help bool @@ -9,7 +17,7 @@ type Args struct { CfgFile string } -func ParseArgs(args []string, swflags map[string]bool) (*Args, []string) { +func ParseArgs(args []string, defaultCfgFile string, swflags map[string]bool) (*Args, []string) { result := &Args{} filteredArgs := []string{} for i := 0; i < len(args); i++ { @@ -45,7 +53,27 @@ func ParseArgs(args []string, swflags map[string]bool) (*Args, []string) { } } if result.CfgFile == "" { - result.CfgFile = "llcppg.cfg" + result.CfgFile = defaultCfgFile } return result, filteredArgs } + +func BoolArg(arg string, defaultValue bool) bool { + parts := strings.SplitN(arg, "=", 2) + if len(parts) != 2 { + return defaultValue + } + value, err := strconv.ParseBool(parts[1]) + if err != nil { + return defaultValue + } + return value +} + +func StringArg(arg string, defaultValue string) string { + parts := strings.SplitN(arg, "=", 2) + if len(parts) != 2 { + return defaultValue + } + return parts[1] +} diff --git a/_xtool/llcppsymg/llcppsymg.go b/_xtool/llcppsymg/llcppsymg.go index c1050bc..5fb57a7 100644 --- a/_xtool/llcppsymg/llcppsymg.go +++ b/_xtool/llcppsymg/llcppsymg.go @@ -31,7 +31,7 @@ import ( func main() { symbFile := "llcppg.symb.json" - ags, _ := args.ParseArgs(os.Args[1:], nil) + ags, _ := args.ParseArgs(os.Args[1:], args.LLCPPG_CFG, nil) var data []byte var err error diff --git a/cmd/gogensig/gogensig.go b/cmd/gogensig/gogensig.go index 2b2f154..3a565b1 100644 --- a/cmd/gogensig/gogensig.go +++ b/cmd/gogensig/gogensig.go @@ -17,10 +17,13 @@ package main import ( + "fmt" "io" "os" "path/filepath" + "strings" + "github.com/goplus/llcppg/_xtool/llcppsymg/args" "github.com/goplus/llcppg/cmd/gogensig/config" "github.com/goplus/llcppg/cmd/gogensig/convert" "github.com/goplus/llcppg/cmd/gogensig/convert/basic" @@ -36,25 +39,35 @@ func runGoCmds(wd, pkg string) { } func main() { - var data []byte - var err error - if len(os.Args) <= 1 { - os.Exit(1) - } + ags, remainArgs := args.ParseArgs(os.Args[1:], args.LLCPPG_SIGFETCH, nil) - sigfetchFile := "llcppg.sigfetch.json" - if len(os.Args) > 1 { - sigfetchFile = os.Args[1] + if ags.Help { + fmt.Fprintln(os.Stderr, "Usage: gogensig [sigfetch-file]") + return } - if sigfetchFile == "-" { + var data []byte + var err error + if ags.UseStdin { data, err = io.ReadAll(os.Stdin) } else { - data, err = os.ReadFile(sigfetchFile) + data, err = os.ReadFile(ags.CfgFile) } check(err) - conf, err := config.GetCppgCfgFromPath("./llcppg.cfg") + var cfg string + for i := 0; i < len(remainArgs); i++ { + arg := remainArgs[i] + switch { + case strings.HasPrefix(arg, "-cfg="): + cfg = args.StringArg(arg, args.LLCPPG_CFG) + } + } + if cfg == "" { + cfg = args.LLCPPG_CFG + } + + conf, err := config.GetCppgCfgFromPath(cfg) check(err) wd, err := os.Getwd() @@ -65,8 +78,8 @@ func main() { p, _, err := basic.ConvertProcesser(&basic.Config{ AstConvertConfig: convert.AstConvertConfig{ PkgName: conf.Name, + CfgFile: filepath.Join(wd, cfg), SymbFile: filepath.Join(wd, "llcppg.symb.json"), - CfgFile: filepath.Join(wd, "llcppg.cfg"), PubFile: filepath.Join(wd, "llcppg.pub"), }, }) diff --git a/llcppg.go b/llcppg.go index 57bcfcc..a8e2181 100644 --- a/llcppg.go +++ b/llcppg.go @@ -65,7 +65,7 @@ func gogensig(in io.Reader) error { } func main() { - ags, _ := args.ParseArgs(os.Args[1:], nil) + ags, _ := args.ParseArgs(os.Args[1:], args.LLCPPG_CFG, nil) if ags.Help { fmt.Fprintln(os.Stderr, "Usage: llcppg [config-file]") return