Skip to content

Commit

Permalink
run gofmt in root
Browse files Browse the repository at this point in the history
  • Loading branch information
rickar committed Mar 19, 2022
1 parent f72b88c commit b142bfa
Show file tree
Hide file tree
Showing 11 changed files with 1,528 additions and 1,528 deletions.
104 changes: 52 additions & 52 deletions arguments.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
// (c) 2022 Rick Arnold. Licensed under the BSD license (see LICENSE).

package props

import (
"os"
"strings"
)

// Arguments reads properties from the command line arguments.
// Property arguments are expected to have a common prefix and use key=value
// format. Other arguments are ignored.
//
// For example, the command:
// cmd -a -1 -z --prop.1=a --prop.2=b --prop.3 --log=debug
// with a prefix of '--prop.' would have properties "1"="a", "2"="b", and
// "3"="".
type Arguments struct {
// Prefix provides the common prefix to use when looking for property
// arguments. If not set, the default of '--' will be used.
Prefix string
}

// Ensure that Arguments implements PropertyGetter
var _ PropertyGetter = &Arguments{}

// Get retrieves the value of a property from the command line arguments. If
// the property does not exist, an empty string will be returned. The bool
// return value indicates whether the property was found.
func (a *Arguments) Get(key string) (string, bool) {
prefix := a.Prefix
if prefix == "" {
prefix = "--"
}
prefix = prefix + key + "="
for _, val := range os.Args {
if strings.HasPrefix(val, prefix) {
return val[len(prefix):], true
}
}
return "", false
}

// GetDefault retrieves the value of a property from the command line arguments.
// If the property does not exist, then the default value will be returned.
func (e *Arguments) GetDefault(key, defVal string) string {
v, ok := e.Get(key)
if !ok {
return defVal
}
return v
}
// (c) 2022 Rick Arnold. Licensed under the BSD license (see LICENSE).

package props

import (
"os"
"strings"
)

// Arguments reads properties from the command line arguments.
// Property arguments are expected to have a common prefix and use key=value
// format. Other arguments are ignored.
//
// For example, the command:
// cmd -a -1 -z --prop.1=a --prop.2=b --prop.3 --log=debug
// with a prefix of '--prop.' would have properties "1"="a", "2"="b", and
// "3"="".
type Arguments struct {
// Prefix provides the common prefix to use when looking for property
// arguments. If not set, the default of '--' will be used.
Prefix string
}

// Ensure that Arguments implements PropertyGetter
var _ PropertyGetter = &Arguments{}

// Get retrieves the value of a property from the command line arguments. If
// the property does not exist, an empty string will be returned. The bool
// return value indicates whether the property was found.
func (a *Arguments) Get(key string) (string, bool) {
prefix := a.Prefix
if prefix == "" {
prefix = "--"
}
prefix = prefix + key + "="
for _, val := range os.Args {
if strings.HasPrefix(val, prefix) {
return val[len(prefix):], true
}
}
return "", false
}

// GetDefault retrieves the value of a property from the command line arguments.
// If the property does not exist, then the default value will be returned.
func (e *Arguments) GetDefault(key, defVal string) string {
v, ok := e.Get(key)
if !ok {
return defVal
}
return v
}
120 changes: 60 additions & 60 deletions arguments_test.go
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
// (c) 2022 Rick Arnold. Licensed under the BSD license (see LICENSE).

package props

import (
"os"
"testing"
)

func TestArgumentsGet(t *testing.T) {
a := &Arguments{}
os.Args = make([]string, 0)
os.Args = append(os.Args, "prog")
os.Args = append(os.Args, "--props.test.val1=abc")
os.Args = append(os.Args, "--props.test.val2=")

val, ok := a.Get("props.test.val1")
if !ok || val != "abc" {
t.Errorf("want: true, 'abc'; got: %t, '%s'", ok, val)
}

val, ok = a.Get("props.test.val2")
if !ok || val != "" {
t.Errorf("want: true, ''; got: %t, '%s'", ok, val)
}

val, ok = a.Get("props.test.val3")
if ok || val != "" {
t.Errorf("want: false, ''; got %t, '%s'", ok, val)
}

a.Prefix = "--props."
val, ok = a.Get("test.val1")
if !ok || val != "abc" {
t.Errorf("want: true, 'abc'; got: %t, '%s'", ok, val)
}
}

func TestArgumentsGetDefault(t *testing.T) {
a := &Arguments{Prefix: "--props."}
os.Args = make([]string, 0)
os.Args = append(os.Args, "prog")
os.Args = append(os.Args, "--props.test.val1=abc")
os.Args = append(os.Args, "--props.test.val2=")

val := a.GetDefault("test.val1", "zzz")
if val != "abc" {
t.Errorf("want: 'abc'; got: '%s'", val)
}

val = a.GetDefault("test.val2", "def")
if val != "" {
t.Errorf("want: ''; got: '%s'", val)
}

val = a.GetDefault("test.val3", "ghi")
if val != "ghi" {
t.Errorf("want: 'ghi'; got: '%s'", val)
}
}
// (c) 2022 Rick Arnold. Licensed under the BSD license (see LICENSE).

package props

import (
"os"
"testing"
)

func TestArgumentsGet(t *testing.T) {
a := &Arguments{}
os.Args = make([]string, 0)
os.Args = append(os.Args, "prog")
os.Args = append(os.Args, "--props.test.val1=abc")
os.Args = append(os.Args, "--props.test.val2=")

val, ok := a.Get("props.test.val1")
if !ok || val != "abc" {
t.Errorf("want: true, 'abc'; got: %t, '%s'", ok, val)
}

val, ok = a.Get("props.test.val2")
if !ok || val != "" {
t.Errorf("want: true, ''; got: %t, '%s'", ok, val)
}

val, ok = a.Get("props.test.val3")
if ok || val != "" {
t.Errorf("want: false, ''; got %t, '%s'", ok, val)
}

a.Prefix = "--props."
val, ok = a.Get("test.val1")
if !ok || val != "abc" {
t.Errorf("want: true, 'abc'; got: %t, '%s'", ok, val)
}
}

func TestArgumentsGetDefault(t *testing.T) {
a := &Arguments{Prefix: "--props."}
os.Args = make([]string, 0)
os.Args = append(os.Args, "prog")
os.Args = append(os.Args, "--props.test.val1=abc")
os.Args = append(os.Args, "--props.test.val2=")

val := a.GetDefault("test.val1", "zzz")
if val != "abc" {
t.Errorf("want: 'abc'; got: '%s'", val)
}

val = a.GetDefault("test.val2", "def")
if val != "" {
t.Errorf("want: ''; got: '%s'", val)
}

val = a.GetDefault("test.val3", "ghi")
if val != "ghi" {
t.Errorf("want: 'ghi'; got: '%s'", val)
}
}
82 changes: 41 additions & 41 deletions combined.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
// (c) 2022 Rick Arnold. Licensed under the BSD license (see LICENSE).

package props

// Combined provides property value lookups across multiple sources.
type Combined struct {
// The property sources to use for lookup in priority order. The first
// source to have a value for a property will be used.
Sources []PropertyGetter
}

// Ensure that Combined implements PropertyGetter
var _ PropertyGetter = &Combined{}

// Get retrieves the value of a property from the source list. If the source
// list is empty or none of the sources has the property, an empty string will
// be returned. The bool return value indicates whether the property was found.
func (c *Combined) Get(key string) (string, bool) {
if c.Sources == nil {
return "", false
}
for _, l := range c.Sources {
val, ok := l.Get(key)
if ok {
return val, true
}
}
return "", false
}

// GetDefault retrieves the value of a property from the source list. If the
// source list is empty or none of the sources has the property, then the
// default value will be returned.
func (c *Combined) GetDefault(key string, defVal string) string {
val, ok := c.Get(key)
if ok {
return val
} else {
return defVal
}
}
// (c) 2022 Rick Arnold. Licensed under the BSD license (see LICENSE).

package props

// Combined provides property value lookups across multiple sources.
type Combined struct {
// The property sources to use for lookup in priority order. The first
// source to have a value for a property will be used.
Sources []PropertyGetter
}

// Ensure that Combined implements PropertyGetter
var _ PropertyGetter = &Combined{}

// Get retrieves the value of a property from the source list. If the source
// list is empty or none of the sources has the property, an empty string will
// be returned. The bool return value indicates whether the property was found.
func (c *Combined) Get(key string) (string, bool) {
if c.Sources == nil {
return "", false
}
for _, l := range c.Sources {
val, ok := l.Get(key)
if ok {
return val, true
}
}
return "", false
}

// GetDefault retrieves the value of a property from the source list. If the
// source list is empty or none of the sources has the property, then the
// default value will be returned.
func (c *Combined) GetDefault(key string, defVal string) string {
val, ok := c.Get(key)
if ok {
return val
} else {
return defVal
}
}
Loading

0 comments on commit b142bfa

Please sign in to comment.