llcppsigfetch:config the output

This commit is contained in:
luoliwoshang
2024-09-23 15:24:03 +08:00
parent dbaf12b043
commit 074090a0aa

View File

@@ -31,30 +31,37 @@ import (
)
func main() {
if len(os.Args) == 1 {
// run with default config file
runFromConfig()
return
}
if os.Args[1] == "--extract" {
cfgFile := ""
outputToFile := false
for i := 1; i < len(os.Args); i++ {
arg := os.Args[i]
if arg == "--extract" {
runExtract()
} else if os.Args[1] == "--help" || os.Args[1] == "-h" {
return
} else if arg == "--help" || arg == "-h" {
printUsage()
} else {
runFromConfig()
return
} else if strings.HasPrefix(arg, "-out=") {
outputToFile = parseBoolArg(arg, "out", false)
} else if cfgFile == "" && !strings.HasPrefix(arg, "-") {
cfgFile = arg
}
}
runFromConfig(cfgFile, outputToFile)
}
func printUsage() {
fmt.Println("Usage:")
fmt.Println(" llcppsigfetch [<config_file>]")
fmt.Println(" llcppsigfetch [<config_file>] [-out=<bool>]")
fmt.Println(" OR")
fmt.Println(" llcppsigfetch --extract <file> <temp> [args...]")
fmt.Println(" llcppsigfetch --extract <file> [-out=<bool>] [-temp=<bool>] [-cpp=<bool>] [args...]")
fmt.Println("")
fmt.Println("Options:")
fmt.Println(" [<config_file>]: Path to the configuration file (use '-' for stdin)")
fmt.Println(" If not provided, uses default 'llcppg.cfg'")
fmt.Println(" -out=<bool>: Optional. Set to 'true' to output results to a file,")
fmt.Println(" 'false' (default) to output to stdout")
fmt.Println(" This option can be used with both modes")
fmt.Println("")
fmt.Println(" --extract: Extract information from a single file")
fmt.Println(" <file>: Path to the file to process, or file content if -temp=true")
@@ -71,10 +78,9 @@ func printUsage() {
fmt.Println("Note: The two usage modes are mutually exclusive. Use either [<config_file>] OR --extract, not both.")
}
func runFromConfig() {
cfgFile := "llcppg.cfg"
if len(os.Args) > 1 {
cfgFile = os.Args[1]
func runFromConfig(cfgFile string, outputToFile bool) {
if cfgFile == "" {
cfgFile = "llcppg.cfg"
}
var data []byte
@@ -100,7 +106,7 @@ func runFromConfig() {
err = context.ProcessFiles(files)
check(err)
outputInfo(context)
outputInfo(context, outputToFile)
}
func runExtract() {
@@ -117,6 +123,7 @@ func runExtract() {
Temp: false,
}
outputToFile := false
for i := 3; i < len(os.Args); i++ {
arg := os.Args[i]
switch {
@@ -128,6 +135,10 @@ func runExtract() {
cfg.IsCpp = parseBoolArg(arg, "cpp", true)
os.Args = append(os.Args[:i], os.Args[i+1:]...)
i--
case strings.HasPrefix(arg, "-out="):
outputToFile = parseBoolArg(arg, "out", false)
os.Args = append(os.Args[:i], os.Args[i+1:]...)
i--
default:
cfg.Args = append(cfg.Args, arg)
}
@@ -139,7 +150,7 @@ func runExtract() {
check(err)
result := converter.MarshalOutputASTFiles()
cstr := result.Print()
c.Printf(cstr)
outputResult(cstr, outputToFile)
cjson.FreeCStr(cstr)
result.Delete()
converter.Dispose()
@@ -151,6 +162,20 @@ func check(err error) {
}
}
func outputResult(result *c.Char, outputToFile bool) {
if outputToFile {
outputFile := "llcppg.sigfetch.json"
err := os.WriteFile(outputFile, []byte(c.GoString(result)), 0644)
if err != nil {
fmt.Fprintf(os.Stderr, "Error writing to output file: %v\n", err)
os.Exit(1)
}
fmt.Printf("Results saved to %s\n", outputFile)
} else {
c.Printf(result)
}
}
func getHeaderFiles(cflags string, files []string) []string {
prefix := cflags
prefix = strings.TrimPrefix(prefix, "-I")
@@ -161,12 +186,12 @@ func getHeaderFiles(cflags string, files []string) []string {
return paths
}
func outputInfo(context *parse.Context) {
func outputInfo(context *parse.Context, outputToFile bool) {
info := context.Output()
str := info.Print()
defer cjson.FreeCStr(str)
defer info.Delete()
c.Printf(str)
outputResult(str, outputToFile)
}
func parseBoolArg(arg, name string, defaultValue bool) bool {