flagfile

package module
v0.0.0-...-5d34715 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2022 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package flagfile allows to define program flags that fill other flags from files.

See github.com/dolmen-go/flagx/flagfile/flagjson as an implementation using JSON files.

Example
package main

import (
	"flag"
	"fmt"

	"github.com/dolmen-go/flagx/flagfile"
)

func main() {
	flags := flag.NewFlagSet("test", flag.PanicOnError) // Usually flag.CommandLine

	flags.Var(flagfile.File(flags, flagfile.Lines), "args", "args from file (one line = one argument)")
	b := flags.Bool("bool", false, "bool test")
	i := flags.Int("int", 0, "int test")
	s := flags.String("string", "", "string test")

	flags.Parse([]string{
		"-args", "testdata/args.txt",
	})

	fmt.Println(*b, *i, *s)

}
Output:

true 1 foo

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func File

func File(flagset FlagSet, loader Loader) flag.Value

File allows to define a command-line flag that gives a path to a structured file whose content will be expanded as command-line arguments and injected into flagset.

The structured data may be:

  • an array of strings given to [flagset.Parse]
  • a map where keys are argument names (without leading '-') and keys are values or arrays of values

This flag is reentrant, so the file may refer to (include) other files by reusing the same flag.

func Lines

func Lines(r io.Reader, fragment string) (interface{}, error)

Lines is a Loader that reads a text file and takes each line as an argument.

Types

type FlagSet

type FlagSet interface {
	Parse([]string) error
	Args() []string
	Set(name, value string) error
}

FlagSet is a subset of methods of *flag.FlagSet. flag.CommandLine is a well known global instance.

type Loader

type Loader func(r io.Reader, fragment string) (interface{}, error)

Loader loads structured data from r. The fragment may point to a partial of the content.

The output is expected to be:

  • a slice of strings
  • a map where keys are strings and values are either string or slice of strings

Directories

Path Synopsis
Package flagjson provides an implementation of guthub.com/dolmen-go/flagx/flagfile.Loader using JSON as file format.
Package flagjson provides an implementation of guthub.com/dolmen-go/flagx/flagfile.Loader using JSON as file format.

Jump to

Keyboard shortcuts

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