argh/cmd/argh-example/main.go

47 lines
677 B
Go
Raw Normal View History

package main
import (
"encoding/json"
"fmt"
"log"
"os"
2022-07-10 21:55:47 +00:00
"git.meatballhat.com/x/argh"
2022-05-29 23:16:13 +00:00
"github.com/davecgh/go-spew/spew"
)
func main() {
2022-05-29 23:16:13 +00:00
asJSON := os.Getenv("ARGH_OUTPUT_JSON") == "enabled"
2022-05-14 00:58:55 +00:00
log.SetFlags(0)
2022-06-06 23:06:50 +00:00
pCfg := argh.NewParserConfig()
2022-06-07 13:47:12 +00:00
pCfg.Prog = &argh.CommandConfig{
2022-06-06 23:06:50 +00:00
NValue: argh.OneOrMoreValue,
ValueNames: []string{"val"},
2022-06-06 23:06:50 +00:00
Flags: &argh.Flags{
Automatic: true,
2022-06-04 21:38:24 +00:00
},
2022-06-06 23:06:50 +00:00
}
pt, err := argh.ParseArgs(os.Args, pCfg)
if err != nil {
log.Fatal(err)
}
2022-05-29 23:16:13 +00:00
ast := argh.NewQuerier(pt.Nodes).AST()
if asJSON {
b, err := json.MarshalIndent(ast, "", " ")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
return
}
2022-05-29 23:16:13 +00:00
spew.Dump(ast)
}