Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lunixbochs/argjoy
Golang method invocation with arg codecs and optional args
https://github.com/lunixbochs/argjoy
Last synced: 1 day ago
JSON representation
Golang method invocation with arg codecs and optional args
- Host: GitHub
- URL: https://github.com/lunixbochs/argjoy
- Owner: lunixbochs
- License: mit
- Created: 2015-12-26T20:30:13.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-05-07T15:37:39.000Z (over 7 years ago)
- Last Synced: 2024-10-27T16:27:51.123Z (about 2 months ago)
- Language: Go
- Homepage:
- Size: 12.7 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# argjoy
[![Build Status](https://travis-ci.org/lunixbochs/argjoy.svg?branch=master)](https://travis-ci.org/lunixbochs/argjoy)
[![GoDoc](https://godoc.org/github.com/lunixbochs/argjoy?status.svg)](http://godoc.org/github.com/lunixbochs/argjoy)Golang module allowing you to call a method using callbacks to translate arguments. Also allows optional arguments.
Extremely basic example:
package main
import (
"fmt"
"github.com/lunixbochs/argjoy"
)// optC is just a variable name. The opt prefix is not required.
func test(a, b, optC int) int {
return a + b + optC
}func main() {
aj := argjoy.NewArgjoy(argjoy.StrToInt)
// Enables optional arguments, where unpassed arguments are zeroed.
aj.Optional = true// The following is effectively: out := test(1, 2, 0)
out, err := aj.Call(test, "1", "2")
if err != nil {
panic(err)
}
// out is []interface{} so you need to do a type assert
fmt.Println(out[0].(int))
}Custom argument decoder example (use [`argjoy.StrToInt`](https://github.com/lunixbochs/argjoy/blob/master/codecs.go#L9) for a more robust version of this):
func strToInt(arg interface{}, vals []interface{}) (err error) {
if v, ok := val[0].(string); ok {
if a, ok := arg.(*int); ok {
*a, err = strconv.Atoi(v)
return
}
}
return argjoy.NoMatch
}## Why?
Reduces duplicate decoding logic. Repeated decoding like `v, err := strconv.Atoi(); if err != nil` is completely eliminated.
Type-safe command-line parsing. Flag, Cobra, etc only reduce to a list of strings, so the rest of the decoding was up to you.
Syscall argument decoding via metaprogramming (used in [Usercorn](https://github.com/lunixbochs/usercorn)).