clapconf

package module
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2022 License: BSD-3-Clause Imports: 10 Imported by: 0

README

Clapconf

logo

clapconf is a utility library (an extension) to claptrap to get (and store) parameters in configuration files.

The project home is here. For help, join #claptrap-lib:matrix.org with a (Matrix client).

Build statusAPI DocumentationGo Report Card

Please file issues in the claptrap todo, and post patches and questions to the claptrap list; please also include #clapconf somewhere in your subjects.

About

Clapconf will load configuration files from:

  • /etc/<progname>
  • then a file in XDG_CONFIG_HOME, $XDG_CONFIG_HOME/<progname>/config
  • then A supplied path to a file

It will save a configuration to:

  • A supplied path to a file, or
  • A file in XDG_CONFIG_HOME, $XDG_CONFIG_HOME/<progname>/config

Clapconf will never override command-line arguments. Config files are TOML. LoadConfig() is idempotent.

Why? You are using claptrap, and want to easily add config file support.

Dependencies

Unlike claptrap, clapconf does have dependencies. The TOML de/serialization, in particular, is a non-trivial issue. clapconf is little more than a link between claptrap and a TOML library (with a little magic for finding config files in common places). TOML was chosen because it's a human oriented format with a defined standard and wide usage and. JSON is machine-oriented, and YAML is prone to breakage due to depending on invisible, interchangeable, characters for syntax.

stretchr's testify is used, although that won't affect your binary size.

Usage

  1. Add ser1.net/clapconf to your imports
  2. Set up your claptrap interface
  3. Call clapconf.LoadConfig("") (or clapconf.Load(), a utility function)
  4. Parse your command line

Steps 3 and 4 are interchangeable.

package main
import "ser1.net/claptrap/v4"
import "ser1.net/clapconf"
func main() {
	myprog := claptrap.Command("myprog", "blah blah blah")
	myprog.Add("--flag", "a bool")
	myprog.Add("--int", "-i", 0, "an int")
	myprog.Add("named", "a positional argument")
  myprog.Parse(nil)        // Parse from os.Args
  clapconf.LoadConfig("")  // Start looking in XDG_CONFIG_HOME
  // The rest of your program here
}

As above, you may call clapconf.LoadConfig() after parsing. This way, you could (e.g.) get the config file name from your user through arguments. clapconf will not overwrite the user's command-line arguments.

As stated, LoadConfig() is idempotent: it can be called any number of times.

Under the hood

LoadConfig() is overriding the default values, so it's equivalent to changing the defaults specified in the command to defaults provided by user configs.

Bugs and limitations

Mandatory flags are always required from the user. Whether you consider this a bug or not depends heavily on how you interpret "required." Since clapconf functions by changing the defaults, mandatory args are left unset.

clapconf can not set multiple values from the config file, because claptrap does not support multi-value default values. This is a real issue that will probably require an API change in claptrap to fix.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load() error

func LoadConfig

func LoadConfig(configPath string) error

LoadConfig loads configuration files into the currently defined claptrap.RootCommand, overriding the default values.

LoadConfig loads the first config file it finds. It looks first in:

1. `configPath` 2. $XDG_CONFIG_HOME '/' claptrap.RootCommand.Name '/config' 3. '/etc/' claptrap.RootCommand.Name '/config'

If no config files are found, nil is returned

LoadConfig may be run before or after Parse().

The same CommandConfig validation rules are enforced with the exception of mandatory arguments. Unrecognized keys, variadic keys for non-variadic args, and types are validated.

func SaveConfig

func SaveConfig(configPath string) error

func SaveDefaults

func SaveDefaults(configPath string) error

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL