internal/build:support relocatable lib
This commit is contained in:
144
_demo/cargs/demo.go
Normal file
144
_demo/cargs/demo.go
Normal file
@@ -0,0 +1,144 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
_ "unsafe"
|
||||
|
||||
"github.com/goplus/lib/c"
|
||||
)
|
||||
|
||||
const LLGoPackage string = "link: $(pkg-config --libs cargs);"
|
||||
|
||||
type Option struct {
|
||||
Identifier c.Char
|
||||
AccessLetters *c.Char
|
||||
AccessName *c.Char
|
||||
ValueName *c.Char
|
||||
Description *c.Char
|
||||
}
|
||||
|
||||
type OptionContext struct {
|
||||
Options *Option
|
||||
OptionCount c.SizeT
|
||||
Argc c.Int
|
||||
Argv **c.Char
|
||||
Index c.Int
|
||||
InnerIndex c.Int
|
||||
ErrorIndex c.Int
|
||||
ErrorLetter c.Char
|
||||
ForcedEnd bool
|
||||
Identifier c.Char
|
||||
Value *c.Char
|
||||
}
|
||||
|
||||
// llgo:type C
|
||||
type Printer func(__llgo_arg_0 c.Pointer, __llgo_arg_1 *c.Char, __llgo_va_list ...interface{}) c.Int
|
||||
|
||||
// llgo:link (*OptionContext).OptionInit C.cag_option_init
|
||||
func (recv_ *OptionContext) OptionInit(options *Option, option_count c.SizeT, argc c.Int, argv **c.Char) {
|
||||
}
|
||||
|
||||
// llgo:link (*OptionContext).OptionFetch C.cag_option_fetch
|
||||
func (recv_ *OptionContext) OptionFetch() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// llgo:link (*OptionContext).OptionGetIdentifier C.cag_option_get_identifier
|
||||
func (recv_ *OptionContext) OptionGetIdentifier() c.Char {
|
||||
return 0
|
||||
}
|
||||
|
||||
// llgo:link (*OptionContext).OptionGetValue C.cag_option_get_value
|
||||
func (recv_ *OptionContext) OptionGetValue() *c.Char {
|
||||
return nil
|
||||
}
|
||||
|
||||
// llgo:link (*OptionContext).OptionGetIndex C.cag_option_get_index
|
||||
func (recv_ *OptionContext) OptionGetIndex() c.Int {
|
||||
return 0
|
||||
}
|
||||
|
||||
// llgo:link (*OptionContext).OptionGetErrorIndex C.cag_option_get_error_index
|
||||
func (recv_ *OptionContext) OptionGetErrorIndex() c.Int {
|
||||
return 0
|
||||
}
|
||||
|
||||
// llgo:link (*OptionContext).OptionGetErrorLetter C.cag_option_get_error_letter
|
||||
func (recv_ *OptionContext) OptionGetErrorLetter() c.Char {
|
||||
return 0
|
||||
}
|
||||
|
||||
// llgo:link (*OptionContext).OptionPrintError C.cag_option_print_error
|
||||
func (recv_ *OptionContext) OptionPrintError(destination *c.FILE) {
|
||||
}
|
||||
|
||||
// llgo:link (*OptionContext).OptionPrinterError C.cag_option_printer_error
|
||||
func (recv_ *OptionContext) OptionPrinterError(printer Printer, printer_ctx c.Pointer) {
|
||||
}
|
||||
|
||||
// llgo:link (*Option).OptionPrint C.cag_option_print
|
||||
func (recv_ *Option) OptionPrint(option_count c.SizeT, destination *c.FILE) {
|
||||
}
|
||||
|
||||
// llgo:link (*Option).OptionPrinter C.cag_option_printer
|
||||
func (recv_ *Option) OptionPrinter(option_count c.SizeT, printer Printer, printer_ctx c.Pointer) {
|
||||
}
|
||||
|
||||
// llgo:link (*OptionContext).OptionPrepare C.cag_option_prepare
|
||||
func (recv_ *OptionContext) OptionPrepare(options *Option, option_count c.SizeT, argc c.Int, argv **c.Char) {
|
||||
}
|
||||
|
||||
// llgo:link (*OptionContext).OptionGet C.cag_option_get
|
||||
func (recv_ *OptionContext) OptionGet() c.Char {
|
||||
return 0
|
||||
}
|
||||
|
||||
func main() {
|
||||
options := []Option{
|
||||
{
|
||||
Identifier: 'h',
|
||||
AccessLetters: c.Str("h"),
|
||||
AccessName: c.Str("help"),
|
||||
ValueName: nil,
|
||||
Description: c.Str("Show help information"),
|
||||
},
|
||||
{
|
||||
Identifier: 'v',
|
||||
AccessLetters: c.Str("v"),
|
||||
AccessName: c.Str("version"),
|
||||
ValueName: nil,
|
||||
Description: c.Str("Show version information"),
|
||||
},
|
||||
}
|
||||
|
||||
args := os.Args
|
||||
|
||||
// Convert Go string array to C-style argv
|
||||
argv := make([]*int8, len(args))
|
||||
for i, arg := range args {
|
||||
argv[i] = c.AllocaCStr(arg)
|
||||
}
|
||||
|
||||
// Initialize option context
|
||||
var context OptionContext
|
||||
context.OptionInit(&options[0], uintptr(len(options)), c.Int(len(args)), &argv[0])
|
||||
|
||||
// Process all options
|
||||
identifierFound := false
|
||||
for context.OptionFetch() {
|
||||
identifierFound = true
|
||||
identifier := context.OptionGetIdentifier()
|
||||
switch identifier {
|
||||
case 'h':
|
||||
fmt.Println("Help: This is a simple command-line parser demo")
|
||||
case 'v':
|
||||
fmt.Println("Version: 1.0.0")
|
||||
}
|
||||
}
|
||||
|
||||
// Default output if no identifier is found
|
||||
if !identifierFound {
|
||||
fmt.Println("Demo Command-line Tool\nIdentifier:\n\t-h: Help\n\t-v: Version")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user