Skip to content

Call custom functions with strings. Useful for remote function call or call from stdin.

License

Notifications You must be signed in to change notification settings

No3371/go-strcmd

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-strcmd

The reflection-based package allows users to call methods with automatically parsed parameters, with strings.

The most common usecase is interactive cli that execute methods according to input from stdin.

func testInt(i1 int, i2 int) error {
	fmt.Print(i1 + i2)
    return nil
}

func Call () {
    strcmd := strcmd.NewStrCmd()
    strcmd.Call(testInt, []string {"123", "456"})
    strcmd.SplitAndCall(testInt, "123 456")
}

func NamedCall () {
    strcmd := strcmd.NewStrCmd()
    functions := map[string]any{
        "testInt": testInt,
        "testStr": func (arg string) error {
            _, err := fmt.Print(arg)
            return err
        },
    }
    strcmd.CallNamed("testInt 123 456", functions)
    strcmd.CallNamed("testStr '2 whitespaces here'", functions)
    strcmd.CallNamed("testStr \"2 whitespaces here\"", functions)
    strcmd.CallNamed("testStr '\\\''", functions)
}

Parsers

By default, most of the basic types are supported as parameters.

You can also add custom parser by strcmd.SetParser.

type testStruct struct {
    a int
}

func addParser () {
    strcmd := strcmd.NewStrCmd()
    strcmd.SetParser(reflect.TypeOf(testStruct{}).Name(),  func(from string) (any, error) {
        parsed, err := strconv.ParseInt(from, 10, 64)
        if err != nil {
            return testStruct { a: int(parsed) }, nil
        } else {
            return int(parsed), nil
        }
    })
}

About

Call custom functions with strings. Useful for remote function call or call from stdin.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages