2017-08-20 14:59:51 +05:00
|
|
|
# Flagger
|
|
|
|
|
2019-12-22 01:29:18 +05:00
|
|
|
[![GoDoc](https://godoc.org/go.dev.pztrn.name/flagger?status.svg)](https://godoc.org/go.dev.pztrn.name/flagger) [![Drone (self-hosted)](https://img.shields.io/drone/build/libraries/flagger?server=https%3A%2F%2Fci.dev.pztrn.name)](https://ci.dev.pztrn.name/libraries/flagger/) [![Discord](https://img.shields.io/discord/632359730089689128)](https://discord.gg/qHN6KsD) ![Keybase XLM](https://img.shields.io/keybase/xlm/pztrn) [![Go Report Card](https://goreportcard.com/badge/go.dev.pztrn.name/flagger)](https://goreportcard.com/report/go.dev.pztrn.name/flagger)
|
|
|
|
|
2017-08-20 14:59:51 +05:00
|
|
|
Flagger is an arbitrary CLI flags parser, like argparse in Python.
|
2017-08-20 15:06:37 +05:00
|
|
|
Flagger is able to parse boolean, integer and string flags.
|
2017-08-20 14:59:51 +05:00
|
|
|
|
2019-12-22 01:29:18 +05:00
|
|
|
## Installation
|
2017-08-20 14:59:51 +05:00
|
|
|
|
2019-12-22 01:29:18 +05:00
|
|
|
```bash
|
2019-10-15 21:20:49 +05:00
|
|
|
go get -u -v go.dev.pztrn.name/flagger
|
2017-08-20 14:59:51 +05:00
|
|
|
```
|
|
|
|
|
2019-12-22 01:29:18 +05:00
|
|
|
## Usage
|
2017-08-20 14:59:51 +05:00
|
|
|
|
2017-08-20 15:06:37 +05:00
|
|
|
Flagger requires logging interface to be passed on initialization.
|
|
|
|
See ``loggerinterface.go`` for required logging functions.
|
|
|
|
It is able to run with standart log package, in that case
|
|
|
|
initialize flagger like:
|
|
|
|
|
2019-12-22 01:29:18 +05:00
|
|
|
```go
|
2018-12-18 04:12:55 +05:00
|
|
|
flgr = flagger.New("My Super Program", flagger.LoggerInterface(log.New(os.Stdout, "testing logger: ", log.Lshortfile)))
|
2017-10-27 07:36:15 +05:00
|
|
|
flgr.Initialize()
|
2017-08-20 15:06:37 +05:00
|
|
|
```
|
|
|
|
|
|
|
|
Adding a flag is easy, just fill ``Flag`` structure and pass to ``AddFlag()`` call:
|
|
|
|
|
2019-12-22 01:29:18 +05:00
|
|
|
```go
|
2017-08-20 15:06:37 +05:00
|
|
|
flag_bool := Flag{
|
|
|
|
Name: "boolflag",
|
|
|
|
Description: "Boolean flag",
|
|
|
|
Type: "bool",
|
|
|
|
DefaultValue: true,
|
|
|
|
}
|
|
|
|
err := flgr.AddFlag(&flag_bool)
|
|
|
|
if err != nil {
|
|
|
|
...
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
After adding all neccessary flags you should issue ``Parse()`` call to get
|
|
|
|
them parsed:
|
|
|
|
|
2019-12-22 01:29:18 +05:00
|
|
|
```go
|
2017-08-20 15:06:37 +05:00
|
|
|
flgr.Parse()
|
|
|
|
```
|
|
|
|
|
|
|
|
After parsed they can be obtained everywhere you want, like:
|
|
|
|
|
2019-12-22 01:29:18 +05:00
|
|
|
```go
|
2017-08-20 15:06:37 +05:00
|
|
|
val, err := flgr.GetBoolValue("boolflag")
|
|
|
|
if err != nil {
|
|
|
|
...
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2019-12-22 01:29:18 +05:00
|
|
|
For more examples take a look at ``flagger_test.go`` file or [at GoDoc](https://godoc.org/go.dev.pztrn.name/flagger).
|