flagext

package module
v0.0.0-...-a8252d2 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2016 License: Apache-2.0 Imports: 7 Imported by: 0

README

go-flags-ext

Convenience helpers for https://github.com/jessevdk/go-flags

Package documentation: https://godoc.org/github.com/bmoyles/go-flags-ext

Documentation

Overview

Package flagext provides convenience helpers for https://github.com/jessevdk/go-flags

Index

Constants

View Source
const (
	// DefaultRWFileFlag is the default set of flags used for ReadWriteFile
	DefaultRWFileFlag int = os.O_CREATE | os.O_RDWR

	// DefaultRWFilePerm is the default FileMode used for ReadWriteFile
	DefaultRWFilePerm os.FileMode = 0600
)

Variables

This section is empty.

Functions

func ExpandUser

func ExpandUser(path string) string

ExpandUser receives a path as a string and attempts to expand a leading tilde or leading tilde + username to the actual user home directory in case it is not expanded by the shell first. If the home directory cannot be expanded, the path is returned as-is.

Example

If user foo does exist, ExpandUser("~foo/bar") should return "<path to foo home directory>/bar" If user foo does NOT exist, ExpandUser("~foo/bar") will return "~foo/bar" as-is

Types

type InputFile

type InputFile struct {
	// contains filtered or unexported fields
}

An InputFile, after parsing, contains an embedded *os.File that is either the user-supplied file from command line args, or, the default (if any) if the user did not pass in an explicit filename. The file is opened for reading.

func (*InputFile) Close

func (f *InputFile) Close() error

Close, when SetNopClose was called with true, or, SetDefaultFile was called with nopClose=true, makes the Close operation on the file a no-op similar to io/ioutil.NopCloser. If SetNopClose was not called or is false otherwise, the underlying file's close method is called.

func (*InputFile) DefaultToStdin

func (f *InputFile) DefaultToStdin()

DefaultToStdin is a convenience method that sets the default file to os.Stdin and nopClose to true allowing default input to be taken from stdin rather than a file.

func (*InputFile) InternalFile

func (f *InputFile) InternalFile() *os.File

InternalFile allows one to extract the actual *os.File should it be needed directly (eg to pass to functions that ask for an *os.File directly rather than an io interface). Using this directly is not generally recommended as it breaks features like NopClose which are handled in the wrapper and cannot be applied directly to *os.File.

func (*InputFile) MarshalFlag

func (f *InputFile) MarshalFlag() (string, error)

MarshalFlag satisfies the go-flags Marshaler interface and returns the file object's file name to serve as a string representation of the file.

func (*InputFile) NopClose

func (f *InputFile) NopClose() bool

NopClose returns the current value of nopClose.

func (*InputFile) SetDefaultFile

func (f *InputFile) SetDefaultFile(defaultFile *os.File, nopClose bool)

SetDefaultFile should be called on the empty struct passed to go-flags' parser to set a default file to be used if a user did not present a file name on the command line. If nopClose is true, the file's Close() method will be wrapped so it returns without actually closing the file similar to io/ioutil.NopCloser (handy for files like os.Stdin or os.Stdout that one may not want to close immediately).

func (*InputFile) SetNopClose

func (f *InputFile) SetNopClose(value bool)

SetNopClose should be called on the empty struct passed to go-flags' parser and causes user-provided files from the command line to have their Close() method overridden and avoid closing the file if Close() is called. This is independent of the default file which is only used if the user did not supply a file on the command line.

func (*InputFile) UnmarshalFlag

func (f *InputFile) UnmarshalFlag(value string) error

UnmarshalFlag satisfies the go-flags Unmarshaler interface and is called at parse time to convert the user-provided file name into a file opened for reading.

type MultiSourceString

type MultiSourceString struct {
	// contains filtered or unexported fields
}

MultiSourceString, when unmarshaled, whose contents vary depending on prefixes on the input value. Provided value file:<filename> will cause the contents of <filename> to be read as the actual value. Provided value env:<environment_variable> will cause the contents of the environment variable specified by <environment_variable> to be read as the actual value. Provided values with no prefix are returned as strings, unmanipulated.

func (*MultiSourceString) Bytes

func (s *MultiSourceString) Bytes() []byte

Bytes returns the internal string value as a byte slice after parsing.

func (*MultiSourceString) Complete

func (s *MultiSourceString) Complete(match string) []flags.Completion

Complete satisfies flags.Completer and returns potential matches based on current input. If file: or env: prefixes are detected, files or environment variables are completed. Non-prefixed input will return no completions.

func (*MultiSourceString) MarshalFlag

func (s *MultiSourceString) MarshalFlag() (string, error)

MarshalFlag is provided to satisfy the flags.Marshaler interface and simply returns the internal string value.

func (*MultiSourceString) String

func (s *MultiSourceString) String() string

String returns the internal string value after parsing.

func (*MultiSourceString) UnmarshalFlag

func (s *MultiSourceString) UnmarshalFlag(value string) error

UnmarshalFlag is provided to satisfy flags.Unmarshaler and takes the string value from go-flags parsing and, depending on prefix, sets the internal string value to the appropriate contents.

type OutputFile

type OutputFile struct {
	// contains filtered or unexported fields
}

An OutputFile, after parsing, contains an embedded *os.File that is either the user-supplied file from command line args, or, the default (if any) if the user did not pass in an explicit filename. The file is opened for writing using flags os.O_WRONLY|os.O_CREATE|os.O_TRUNC and FileMode 0600.

func (*OutputFile) Close

func (f *OutputFile) Close() error

Close, when SetNopClose was called with true, or, SetDefaultFile was called with nopClose=true, makes the Close operation on the file a no-op similar to io/ioutil.NopCloser. If SetNopClose was not called or is false otherwise, the underlying file's close method is called.

func (*OutputFile) DefaultToStdout

func (f *OutputFile) DefaultToStdout()

DefaultToStdout is a convenience method that sets the default file to os.Stdout and nopClose to true allowing default input to be taken from stdin rather than a file.

func (*OutputFile) InternalFile

func (f *OutputFile) InternalFile() *os.File

InternalFile allows one to extract the actual *os.File should it be needed directly (eg to pass to functions that ask for an *os.File directly rather than an io interface). Using this directly is not generally recommended as it breaks features like NopClose which are handled in the wrapper and cannot be applied directly to *os.File.

func (*OutputFile) MarshalFlag

func (f *OutputFile) MarshalFlag() (string, error)

MarshalFlag satisfies the go-flags Marshaler interface and returns the file object's file name to serve as a string representation of the file.

func (*OutputFile) NopClose

func (f *OutputFile) NopClose() bool

NopClose returns the current value of nopClose.

func (*OutputFile) SetDefaultFile

func (f *OutputFile) SetDefaultFile(defaultFile *os.File, nopClose bool)

SetDefaultFile should be called on the empty struct passed to go-flags' parser to set a default file to be used if a user did not present a file name on the command line. If nopClose is true, the file's Close() method will be wrapped so it returns without actually closing the file similar to io/ioutil.NopCloser (handy for files like os.Stdin or os.Stdout that one may not want to close immediately).

func (*OutputFile) SetNopClose

func (f *OutputFile) SetNopClose(value bool)

SetNopClose should be called on the empty struct passed to go-flags' parser and causes user-provided files from the command line to have their Close() method overridden and avoid closing the file if Close() is called. This is independent of the default file which is only used if the user did not supply a file on the command line.

func (*OutputFile) UnmarshalFlag

func (f *OutputFile) UnmarshalFlag(value string) error

UnmarshalFlag satisfies the go-flags Unmarshaler interface and is called at parse time to convert the user-provided file name into a file opened for writing.

type ReadWriteFile

type ReadWriteFile struct {
	// contains filtered or unexported fields
}

An ReadWriteFile, after parsing, contains an embedded *os.File that is either the user-supplied file from command line args, or, the default (if any) if the user did not pass in an explicit filename. The file is opened for both reading and writing with the default flag contained in DefaultRWFileFlag and default FileMode contained in DefaultRWFileMode.

func (*ReadWriteFile) Close

func (f *ReadWriteFile) Close() error

Close, when SetNopClose was called with true, or, SetDefaultFile was called with nopClose=true, makes the Close operation on the file a no-op similar to io/ioutil.NopCloser. If SetNopClose was not called or is false otherwise, the underlying file's close method is called.

func (*ReadWriteFile) Flag

func (f *ReadWriteFile) Flag() int

Flag returns the current default flag value.

func (*ReadWriteFile) InternalFile

func (f *ReadWriteFile) InternalFile() *os.File

InternalFile allows one to extract the actual *os.File should it be needed directly (eg to pass to functions that ask for an *os.File directly rather than an io interface). Using this directly is not generally recommended as it breaks features like NopClose which are handled in the wrapper and cannot be applied directly to *os.File.

func (*ReadWriteFile) MarshalFlag

func (f *ReadWriteFile) MarshalFlag() (string, error)

MarshalFlag satisfies the go-flags Marshaler interface and returns the file object's file name to serve as a string representation of the file.

func (*ReadWriteFile) NopClose

func (f *ReadWriteFile) NopClose() bool

NopClose returns the current value of nopClose.

func (*ReadWriteFile) Perm

func (f *ReadWriteFile) Perm() os.FileMode

Perm returns the current default perm value.

func (*ReadWriteFile) SetDefaultFile

func (f *ReadWriteFile) SetDefaultFile(defaultFile *os.File, nopClose bool)

SetDefaultFile should be called on the empty struct passed to go-flags' parser to set a default file to be used if a user did not present a file name on the command line. If nopClose is true, the file's Close() method will be wrapped so it returns without actually closing the file similar to io/ioutil.NopCloser (handy for files like os.Stdin or os.Stdout that one may not want to close immediately).

func (*ReadWriteFile) SetFlag

func (f *ReadWriteFile) SetFlag(value int)

SetFlag should be called on the empty struct passed to go-flags' parser and causes user-provided files on the command line to be opened with the provided flag value rather than DefaultRWFileFlag.

func (*ReadWriteFile) SetNopClose

func (f *ReadWriteFile) SetNopClose(value bool)

SetNopClose should be called on the empty struct passed to go-flags' parser and causes user-provided files from the command line to have their Close() method overridden and avoid closing the file if Close() is called. This is independent of the default file which is only used if the user did not supply a file on the command line.

func (*ReadWriteFile) SetPerm

func (f *ReadWriteFile) SetPerm(value os.FileMode)

SetPerm should be called on the empty struct passed to go-flags' parser and causes user-provided files on the command line to be opened with the provided FileMode rather than DefaultRWFilePerm.

func (*ReadWriteFile) UnmarshalFlag

func (f *ReadWriteFile) UnmarshalFlag(value string) error

UnmarshalFlag satisfies the go-flags Unmarshaler interface and is called at parse time to convert the user-provided file name into a file opened for reading and writing.

Jump to

Keyboard shortcuts

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