Skip to content

Instantly share code, notes, and snippets.

@alecthomas
Created October 15, 2018 01:59
Show Gist options
  • Save alecthomas/5e7fcd22c96ff96758626d04ef0d2909 to your computer and use it in GitHub Desktop.
Save alecthomas/5e7fcd22c96ff96758626d04ef0d2909 to your computer and use it in GitHub Desktop.
Participle
package main
import (
"fmt"
"github.com/alecthomas/participle"
"github.com/alecthomas/repr"
)
type CLI struct {
Entries []*Entry `{ @@ }`
}
type Entry struct {
Usage string ` "usage" ":" @String`
Commands []*Command `| "commands" ":" { @@ }`
Flags []*Flag `| "flags" ":" { @@ }`
}
type Command struct {
Long string `@Ident`
Comma string `","`
Short string `@Ident`
Description string `@String`
}
type Flag struct {
Long string `"-" "-" @Ident`
Comma string `","`
Short string `"-" "-" @Ident`
Description string `@String`
}
func main() {
parser, err := participle.Build(&CLI{})
if err != nil {
panic(err)
}
ast := &CLI{}
err = parser.ParseString(`
usage: "my usage"
commands:
long, short "description"
long2, short2 "description2"
flags:
--long, --short "description"
`, ast)
if err != nil {
fmt.Println(err)
}
repr.Println(ast)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment