github.com/gotranspile/cxgo@v0.3.8-0.20240118201721-29871598a6a2/cmd/cxgo/file.go (about)

     1  package main
     2  
     3  import (
     4  	"errors"
     5  	"path/filepath"
     6  	"strings"
     7  
     8  	"github.com/spf13/cobra"
     9  
    10  	"github.com/gotranspile/cxgo"
    11  	"github.com/gotranspile/cxgo/libs"
    12  	"github.com/gotranspile/cxgo/types"
    13  )
    14  
    15  func init() {
    16  	cmdFile := &cobra.Command{
    17  		Use:   "file",
    18  		Short: "transpile a single C file to Go",
    19  	}
    20  	Root.AddCommand(cmdFile)
    21  
    22  	fOut := cmdFile.Flags().StringP("out", "o", "", "output file to write to")
    23  	fPkg := cmdFile.Flags().StringP("pkg", "p", "main", "package name for a Go file")
    24  	fExportFields := cmdFile.Flags().Bool("export-fields", false, "export struct fields")
    25  	fDoNotEdit := cmdFile.Flags().Bool("donotedit", false, "add DO NOT EDIT comment header")
    26  	cmdFile.RunE = func(cmd *cobra.Command, args []string) error {
    27  		if len(args) != 1 {
    28  			return errors.New("exactly one file must be specified")
    29  		}
    30  		in := args[0]
    31  		out := *fOut
    32  		if out == "" {
    33  			out = strings.TrimSuffix(in, filepath.Ext(in)) + ".go"
    34  		}
    35  		env := libs.NewEnv(types.Config{
    36  			UseGoInt: true,
    37  		})
    38  		fc := cxgo.Config{
    39  			Package:          *fPkg,
    40  			GoFile:           filepath.Base(out),
    41  			MaxDecls:         -1,
    42  			UnexportedFields: !*fExportFields,
    43  			DoNotEdit:        *fDoNotEdit,
    44  		}
    45  		return cxgo.Translate("", in, filepath.Dir(out), env, fc)
    46  	}
    47  }