diff --git a/chore/_xtool/llcppsigfetch/llcppsigfetch.go b/chore/_xtool/llcppsigfetch/llcppsigfetch.go index 6f9dcd37..898cb9fc 100644 --- a/chore/_xtool/llcppsigfetch/llcppsigfetch.go +++ b/chore/_xtool/llcppsigfetch/llcppsigfetch.go @@ -30,6 +30,43 @@ import ( ) func main() { + if len(os.Args) == 1 { + // run with default config file + runFromConfig() + return + } + + if os.Args[1] == "--extract" { + runExtract() + } else if os.Args[1] == "--help" || os.Args[1] == "-h" { + printUsage() + } else { + runFromConfig() + } +} + +func printUsage() { + fmt.Println("Usage:") + fmt.Println(" llcppsigfetch []") + fmt.Println(" OR") + fmt.Println(" llcppsigfetch --extract [args...]") + fmt.Println("") + fmt.Println("Options:") + fmt.Println(" []: Path to the configuration file (use '-' for stdin)") + fmt.Println(" If not provided, uses default 'llcppg.cfg'") + fmt.Println("") + fmt.Println(" --extract: Extract information from a single file") + fmt.Println(" : When is false: the path to the file to process") + fmt.Println(" When is true: the content of the file to process") + fmt.Println(" : 'true' if contains file content, 'false' if it's a file path") + fmt.Println(" [args]: Optional additional arguments (default: -x c++ -std=c++11)") + fmt.Println("") + fmt.Println(" --help, -h: Show this help message") + fmt.Println("") + fmt.Println("Note: The two usage modes are mutually exclusive. Use either [] OR --extract, not both.") +} + +func runFromConfig() { cfgFile := "llcppg.cfg" if len(os.Args) > 1 { cfgFile = os.Args[1] @@ -61,6 +98,36 @@ func main() { outputInfo(context) } +func runExtract() { + if len(os.Args) < 4 { + printUsage() + os.Exit(1) + } + + cfg := &parse.Config{ + File: os.Args[2], + Temp: os.Args[3] == "true", + Args: os.Args[4:], + } + if !cfg.Temp { + absPath, err := filepath.Abs(cfg.File) + check(err) + cfg.File = absPath + println(cfg.File) + } + + converter, err := parse.NewConverter(cfg) + check(err) + _, err = converter.Convert() + check(err) + result := converter.MarshalASTFiles() + cstr := result.Print() + c.Printf(cstr) + cjson.FreeCStr(cstr) + result.Delete() + converter.Dispose() +} + func check(err error) { if err != nil { panic(err) diff --git a/chore/_xtool/llcppsigfetch/parse/cvt.go b/chore/_xtool/llcppsigfetch/parse/cvt.go index c047e5bf..3fc04865 100644 --- a/chore/_xtool/llcppsigfetch/parse/cvt.go +++ b/chore/_xtool/llcppsigfetch/parse/cvt.go @@ -72,7 +72,7 @@ func NewConverter(config *Config) (*Converter, error) { } func CreateTranslationUnit(config *Config) (*clang.Index, *clang.TranslationUnit, error) { - if config.Args == nil { + if config.Args == nil || len(config.Args) == 0 { config.Args = []string{"-x", "c++", "-std=c++11"} }