跳转至

AUXO

With package app, you can build a friendly CLI program easily.

Quick start

package main

import (
    "github.com/cuigh/auxo/app"
)

func main() {
    app.Name = "test"
    app.Version = "0.1"
    app.Desc = "a test app for auxo"
    app.Action = func(ctx *app.Context) {
        println("Hello, world")
    }
    app.Start()
}

Register common flags

Package app/flag has 4 built-in common flags:

  • Help - Show help
  • Version - Show Print version information
  • Config - Set configuration directory
  • Profile - Set active profiles

These flags can be registered by Register method.

func main() {
    app.Name = "test"
    app.Action = func(ctx *app.Context) {
        println("Hello, world.")
    }
    app.Flags.Register(flag.All)
    app.Start()
}

You can register the flags only you cared about.

app.Flags.Register(flag.Help | flag.Version)

Add sub commands

You can add sub command to your program

// test.go
package main

import (
    "github.com/cuigh/auxo/app"
    "github.com/cuigh/auxo/app/flag"
)

func main() {
    app.Name = "test"
    app.Desc = "a test app for auxo"
    app.Flags.Register(flag.All)

    cmd := app.NewCommand("test", "a test sub command", func(ctx *app.Context) {
        println("Hello, world.")
    })
    // Command can register common flags too
    cmd.Flags.Register(flag.Help)
    app.AddCommand(cmd)

    app.Start()
}

Using go run to test it:

> go run test.go test -h
a test sub command

Options:

  -h, -help[=false]         Show help

Serve with graceful shutdown

Run and RunFunc methods in package app provider a way to run server command.

func main() {
    app.Name = "test"
    app.Desc = "a test app for auxo"
    app.Action = func(ctx *app.Context) {
        app.RunFunc(runner, closer)
    }
    app.Flags.Register(flag.All)
    app.Start()
}

func runner() error {
    fmt.Println("Hello, world")
    return nil
}

func closer(timeout time.Duration) {
    // time-consuming operations for cleaning
}