More Related Content
Similar to 15分で作って配布するGolangコマンドラインツール (6)
15分で作って配布するGolangコマンドラインツール
- 10. app.Commands = []cli.Command{
{
Name: "search", // Sub command: `stopho search`
ShortName: "s", // Alias: `stopho s`
Usage: "Generate search URLs from stock photos web sites",
Action: command.CmdSearch,
Flags: []cli.Flag{ // Options
cli.BoolFlag{ // TRUE or FALSE
Name: "japan, j", // `stopho s --japan` or `stopho s -j`
Usage: "Enable Japan mode",
},
cli.StringFlag{ // `stopho s -s $SITE_NAME`
Name: "site, s",
Usage: "Display only sites matched `value`",
},
},
},
}
- 11. func CmdSearch(c *cli.Context) {
// Arguments
term := c.Args().Get(0)
// Bool Options
if c.Bool("japan") {
// japan mode
}
// String Options
fmt.Println(c.String("site"))
}
Function
- 12. // Set Writer
outStream = new(bytes.Buffer)
app.Writer = outStream
~~~~~~
// Use `fmt.Fprintf`
fmt.Fprintf(c.App.Writer, ...)
Testing
- 23. Enjoy your Go programming!
Please star my Stopho! ->
Please follow me! ->