persistent storage for flags in go

Godoc Go Report Card Coverage

ingo is a simple Go library helping you to persist flags in a ini-like config file.

Features and limitations

  • Requires Go 1.5 or later
  • automatically creates config file, if it does not exist yet
  • option value priorities (from high to low): 0. flags given on the commandline 0. flags read from the config file 0. defaults given when flags are initialized
  • write defaults to config file, if they are not set there already
  • every flag in the config file has the flag usage prepended as a comment
  • shorthand flags pointing to the same variable as another flag will not be written to the config file to reduce noise. Both short and long version will still show up in the -h output.
  • only rewrite the config file, when it would change
  • old flags, which are not used anymore are not removed
  • when old flags are found, a warning is printed to stderr (see example below)
  • flags must not contain the runes : and = and not start with # as these runes are used as separators and comment prefix in the config file
  • no sections, namespaces or FlagSets other than the default one

Installation

go get -u github.com/schachmat/ingo

Usage example

Just setup your flags with defaults like you are used to do. Instead of flag.Parse() you have to call ingo.Parse(APPLICATION_NAME) and check if an error is returned. Thats all.

package main

import (
	"flag"
	"fmt"
	"log"
	"github.com/schachmat/ingo"
)

func main() {
	num := flag.Int("num", 3, "`NUMBER` of times to\n    \tdo a barrel roll")
	location := flag.String("location", "space", "`WHERE` to do the barrel roll")
	flag.StringVar(location, "l", "space", "`WHERE` to do the barrel roll (shorthand)")
	if err := ingo.Parse("keep_rollin"); err != nil {
		log.Fatal(err)
	}
	fmt.Println(*num, *location)
}

The (one newline, four spaces, one tab) separator will ensure that multi-line usage strings will be laid out correctly in the config file and in the -h help message. The code will create the following config file .keep_rollinrc (or any other location if specified in the environment variable KEEP_ROLLINRC):

# WHERE to do the barrel roll
location=space

# NUMBER of times to
# do a barrel roll
num=3

If you change num to 5 in the config file, it will be persistent on all future runs:

# WHERE to do the barrel roll
location=space

# NUMBER of times to
# do a barrel roll
num=5

If you add a new flag style to your programm, it will be added to the config file on the first run using the default value from the flag:

# WHERE to do the barrel roll
location=space

# NUMBER of times to
# do a barrel roll
num=5

# HOW to do the barrel roll
style=epic

If you remove both location flags from your programm, the config entry will be rewritten to this:

# NUMBER of times to
# do a barrel roll
num=5

# HOW to do the barrel roll
style=epic


# The following options are probably deprecated and not used currently!
location=space

Also when such old flags are found, a warning like the following is printed to stderr when running ingo.Parse:

!!!!!!!!!!
! WARNING: keep_rollin was probably updated,
! Check and update .keep_rollinrc as necessary
! and remove the last "deprecated" paragraph to disable this message!
!!!!!!!!!!

All config files will have a header like the following explaining the syntax:

# keep_rollin configuration
#
# This config has https://github.com/schachmat/ingo syntax.
# Empty lines or lines starting with # will be ignored.
# All other lines must look like "KEY=VALUE" (without the quotes).
# The VALUE must not be enclosed in quotes as well!

License - ISC

Copyright (c) 2016-2017, [email protected]

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Similar Resources

Library for setting values to structs' fields from env, flags, files or default tag

Configuration is a library for injecting values recursively into structs - a convenient way of setting up a configuration object. Available features:

Dec 7, 2022

getopt-like flags package for golang,

goopt A getopt-like processor of command-line flags. It works much like the "flag" package, only it processes arguments in a way that is compatible wi

Oct 5, 2022

Drop-in replacement for Go's flag package, implementing POSIX/GNU-style --flags.

Description pflag is a drop-in replacement for Go's flag package, implementing POSIX/GNU-style --flags. pflag is compatible with the GNU extensions to

Nov 25, 2022

Flags-first package for configuration

ff stands for flags-first, and provides an opinionated way to populate a flag.FlagSet with configuration data from the environment.

Dec 26, 2022

A rich tool for parsing flags and values in pure Golang

A rich tool for parsing flags and values in pure Golang. No additional library is required and you can use everywhere.

Jan 25, 2022

A kubectl plugin for finding decoded secret data with productive search flags.

kubectl-secret-data What is it? This is a kubectl plugin for finding decoded secret data. Since kubectl only outputs base64-encoded secrets, it makes

Dec 2, 2022

A kubectl plugin for finding decoded secret data with productive search flags.

kubectl-secret-data What is it? This is a kubectl plugin for finding decoded secret data. Since kubectl outputs base64-encoded secrets basically, it m

Dec 2, 2022

Expressive flags for Go

Expressive flags for Go Package xflags provides an alternative to Go's flag package for defining and parsing command line arguments with an emphasis o

Dec 8, 2022

Go socket with SO_REUSEPORT and SO_REUSEADDR flags

Go socket with SO_REUSEPORT and SO_REUSEADDR flags

This library helps go developers to open sockets with SO_REUSEPORT and SO_REUSEADDR flags. Why ? This flags will allow many processes to bind to the s

Dec 29, 2022

Generate markdown from go-flags

goflags-markdown -- generate markdown from a go-flags parser TODO Commands/sub-commands/etc Custom formatting Usage package main import ( "os" fla

May 22, 2022

Prompts users to enter values for required flags in Cobra CLI applications

Cobra Flag Prompt Cobra Flag Prompt prompts users to enter values for required flags. It is an extension of Cobra, and requires that you use Cobra to

Nov 13, 2021

A go1.18 wrapper to provide simple generics based API for defining command line flags.

gflag A go1.18 wrapper to provide simple generics based API for defining command line flags. Example package main import ( "flag" "fmt" "time" "

Dec 20, 2021

Golang flags parser with zero dependency

flags Golang flags parser with zero dependency. Usage See simple.go for basic usage. Concept flags gives a simple way to get flag's value from argumen

Jan 16, 2022

Go-flags wireframing demo

go-flags-demo redo - global option redo Redo global option via automatic code-gen TOC go-flags-demo - global option redo Synopsis Usage Development Hi

Jan 22, 2022

archy is an static binary to determine current kernel and machine architecture, with backwards compatible flags to uname, and offers alternative output format of Go runtime (i.e. GOOS, GOARCH).

archy archy is an simple binary to determine current kernel and machine architecture, which wraps uname and alternatively can read from Go runtime std

Mar 18, 2022

TurtleDex is a decentralized cloud storage platform that radically alters the landscape of cloud storage

TurtleDex is a decentralized cloud storage platform that radically alters the landscape of cloud storage. By leveraging smart contracts, client-side encryption, and sophisticated redundancy (via Reed-Solomon codes), TurtleDex allows users to safely store their data with hosts that they do not know or trust.

May 29, 2021

TurtleDex is a decentralized cloud storage platform that radically alters the landscape of cloud storage.

TurtleDex is a decentralized cloud storage platform that radically alters the landscape of cloud storage. By leveraging smart contracts, client-side e

Feb 17, 2021
Comments
  • Add a fallback way to determine the home path.

    Add a fallback way to determine the home path.

    On linux/386 systems Parse (in wego) fails with something like: "[...] 2016/04/15 12:13:04 Error parsing config: user: Current not implemented on linux/386 You can set the environment variable WEGORC to point to your config file as a workaround [...]"

    Before restorting to WEGORC we should try the HOME env variable first.

  • undefined: flag.UnquoteUsage

    undefined: flag.UnquoteUsage

    I get the following error when installing:

    $ go get -u github.com/schachmat/ingo
    # github.com/schachmat/ingo
    src/godeps/src/github.com/schachmat/ingo/in.go:134: undefined: flag.UnquoteUsage
    

    How can I come around this? :)

  • undefined while installation

    undefined while installation

    When installing the module, it throws an error:

    $ go get -u github.com/schachmat/ingo # github.com/schachmat/ingo ./in.go:92: undefined: flag.UnquoteUsage

    Unfortunately I'm not into go at all, I just wanted to use the wego weather forecast. It works great on my mac with osx 10.11 but on centos7 it doesn't install because of the above error.

    Please bear with me if this a OSI-Layer-8 ;)

    If you need further information about my environment, feel free to ask.

  • ingo error

    ingo error

    Hello, in jessie/sid (ubuntu) :

    go get -u github.com/schachmat/ingo

    github.com/schachmat/ingo

    go/src/github.com/schachmat/ingo/in.go:92:20: error: reference to undefined identifier ‘flag.UnquoteUsage’ _, usage := flag.UnquoteUsage(f) ^

Related tags
Flags-first package for configuration

ff stands for flags-first, and provides an opinionated way to populate a flag.FlagSet with configuration data from the environment.

Dec 26, 2022
The Container Storage Interface (CSI) Driver for Fortress Block Storage This driver allows you to use Fortress Block Storage with your container orchestrator

fortress-csi The Container Storage Interface (CSI) Driver for Fortress Block Storage This driver allows you to use Fortress Block Storage with your co

Jan 23, 2022
Application written in Go which polls Time-series data at specific intervals and saves to persistent storage

TPoller Server Overview The purpose of this application is to poll time-series data per time interval from any (telemetry) application running a gRPC

Feb 10, 2022
Dynamically provisioning persistent local storage with Kubernetes

Local Path Provisioner Overview Local Path Provisioner provides a way for the Kubernetes users to utilize the local storage in each node. Based on the

Jan 4, 2023
A Go implementation of an in-memory bloom filter, with support for boltdb and badgerdb as optional data persistent storage.

Sprout A bloom filter is a probabilistic data structure that is used to determine if an element is present in a set. Bloom filters are fast and space

Jul 4, 2022
Automatically sets up command line flags based on struct fields and tags.
Automatically sets up command line flags based on struct fields and tags.

Commandeer Commandeer sets up command line flags based on struct fields and tags. Do you... like to develop Go apps as libraries with tiny main packag

Dec 1, 2022
Idiomatic Go input parsing with subcommands, positional values, and flags at any position. No required project or package layout and no external dependencies.
Idiomatic Go input parsing with subcommands, positional values, and flags at any position. No required project or package layout and no external dependencies.

Sensible and fast command-line flag parsing with excellent support for subcommands and positional values. Flags can be at any position. Flaggy has no

Jan 1, 2023
Drop-in replacement for Go's flag package, implementing POSIX/GNU-style --flags.

Description pflag is a drop-in replacement for Go's flag package, implementing POSIX/GNU-style --flags. pflag is compatible with the GNU extensions to

Dec 30, 2022
Generate flags by parsing structures

Flags based on structures. The sflags package uses structs, reflection and struct field tags to allow you specify command line options. It supports di

Nov 24, 2022