Just write your function, and you'll have a CLI tool-no need to worry about flags, figuring out their names, or creating a config struct to manage them. Simply focus on the function, and let me take care of the rest.
- create your functions
func Curl(link string) (string, error) {
res, err := http.Get(link)
if err != nil {
return "", err
}
defer res.Body.Close()
data, err := io.ReadAll(res.Body)
if err != nil {
return "", err
}
return string(data), nil
}
func Cat(path string) (string, error) {
data, err := os.ReadFile(path)
if err != nil {
return "", err
}
fmt.Println(string(data))
return string(data), nil
}
- use the tool
import (
"fmt"
"io"
"net/http"
"os"
"github.com/nasoooor29/ExportIT"
)
func main() {
ExportIT.ExecCli(
"appName",
"short Desc",
"Long Description",
ExportIT.CliNamedParam(Curl),
ExportIT.CliNamedParam(Cat),
)
}
- you are done!!
❯ go build
❯ ./ExportIT
Long Description
Usage:
appName [command]
Available Commands:
Cat main.Cat
Curl main.Curl
completion Generate the autocompletion script for the specified shell
help Help about any command
Flags:
-h, --help help for appName
Use "appName [command] --help" for more information about a command.
❯
- currently it work only with simple data types - string - int - bool
- currently only work with named arguments
- can't pipe things into it
better shorthand naming- cli for init sample
- arg first uppercase rune means required cli arg
- add positional args function
- instead of pos args func [
FIRST LETTER for req
]whateverTheArg[P0S
] example "UrlP0S"