Created
October 15, 2018 01:59
-
-
Save alecthomas/5e7fcd22c96ff96758626d04ef0d2909 to your computer and use it in GitHub Desktop.
Participle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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