aboutsummaryrefslogtreecommitdiff
path: root/README.md
blob: 503794d7548f37b97c4ae8b2df6b6645aa369df8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# app

[![Go Reference](https://pkg.go.dev/badge/github.com/neonxp/app.svg)](https://pkg.go.dev/github.com/neonxp/app)

Simple cli applications runner.

## Usage

Commands must implements `app.Command` interface:

```go
type exampleCommand struct {
	test string
}

func (c *exampleCommand) Name() string {
	return "example"
}

func (c *exampleCommand) Usage() string {
	return "just example command"
}

func (c *exampleCommand) Flags() *flag.FlagSet {
	fs := flag.NewFlagSet(c.Name(), flag.ExitOnError)
	fs.StringVar(&c.test, "test", "value", "test flag")
	return fs
}

func (c *exampleCommand) Run(args []string) error {
	fmt.Printf("Runned %s command with flag -test=%s and args: %v", c.Name(), c.test, args)
	return nil
}
```

Main file example:

```go
package main

import "github.com/neonxp/app"

func main() {
    app := app.New([]app.Command{
        exampleCommand{}, // <- Register command
    })
	if err := app.Start(os.Args); err != nil { // <- Run!
		fmt.Println(err)
		os.Exit(1)
	}
}
```

That's all!

[Full example](/example/example.go)