Skip to content

Commit

Permalink
gogensig:assign cfg path
Browse files Browse the repository at this point in the history
  • Loading branch information
luoliwoshang committed Dec 2, 2024
1 parent 13bd6c9 commit c3c5276
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 37 deletions.
23 changes: 4 additions & 19 deletions _xtool/llcppsigfetch/llcppsigfetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"io"
"os"
"strconv"
"strings"

"github.com/goplus/llcppg/_xtool/llcppsigfetch/parse"
Expand All @@ -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,
})

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion _xtool/llcppsymg/_cmptest/args_test/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
34 changes: 31 additions & 3 deletions _xtool/llcppsymg/args/args.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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++ {
Expand Down Expand Up @@ -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]
}
2 changes: 1 addition & 1 deletion _xtool/llcppsymg/llcppsymg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 25 additions & 12 deletions cmd/gogensig/gogensig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {

Check warning on line 61 in cmd/gogensig/gogensig.go

View check run for this annotation

qiniu-x / golangci-lint

cmd/gogensig/gogensig.go#L61

singleCaseSwitch: should rewrite switch statement to if statement (gocritic)
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()
Expand All @@ -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"),
},
})
Expand Down
2 changes: 1 addition & 1 deletion llcppg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c3c5276

Please sign in to comment.