configflag

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package configflag provides a utility for registering config related command line options with the stdlib flag package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FlagStringSlice

type FlagStringSlice struct {
	Value []string
	// contains filtered or unexported fields
}

FlagStringSlice represents a slice of strings. When used as a flag variable, it allows for multiple string values. For example, it can be used like this:

var configFiles FlagStringSlice
flag.Var(&configFiles, "f", "configuration file(s)")

Then it can be invoked like this:

./app -f file1.yaml -f file2.yaml -f valueN.yaml

Finally, when the flags are parsed, the variable contains all the values.

Example

The FlagStringSlice allows for multiple values when used as a flag variable.

package main

import (
	"flag"
	"fmt"

	"github.com/m3db/m3/src/x/config/configflag"
)

func main() {
	var configFiles configflag.FlagStringSlice
	fs := flag.NewFlagSet("config", flag.PanicOnError)
	fs.Var(&configFiles, "f", "config files")
	noError(fs.Parse([]string{"-f", "file1.yaml", "-f", "file2.yaml", "-f", "file3.yaml"}))

	fmt.Println("Config files:", configFiles.Value)
}

func noError(err error) {
	if err != nil {
		panic(err.Error())
	}
}
Output:

Config files: [file1.yaml file2.yaml file3.yaml]

func (*FlagStringSlice) Set

func (i *FlagStringSlice) Set(value string) error

Set appends a string value to the slice.

func (*FlagStringSlice) String

func (i *FlagStringSlice) String() string

String() returns a string implementation of the slice.

type Options

type Options struct {
	// set by commandline flags
	// ConfigFiles (-f) is a list of config files to load
	ConfigFiles FlagStringSlice

	// ShouldDumpConfigAndExit (-d) causes MainLoad to print config to stdout,
	// and then exit.
	ShouldDumpConfigAndExit bool
	// contains filtered or unexported fields
}

Options represents the values of config command line flags

func (*Options) MainLoad

func (opts *Options) MainLoad(target interface{}, loadOpts config.Options) error

MainLoad is a convenience method, intended for use in main(), which handles all config commandline options. It:

  • Dumps config and exits if -d was passed.
  • Loads configuration otherwise.

Users who want a subset of this behavior should call individual methods.

func (*Options) Register

func (opts *Options) Register()

Register registers commandline options with the default flagset.

func (*Options) RegisterFlagSet

func (opts *Options) RegisterFlagSet(cmd *flag.FlagSet)

RegisterFlagSet registers commandline options with the given flagset.

Jump to

Keyboard shortcuts

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