Skip to content

Commit

Permalink
fix: renamed .Default in OptCfg to .Defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
sttk committed Feb 5, 2024
1 parent 832f5bc commit 835b0fa
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 101 deletions.
14 changes: 7 additions & 7 deletions doc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2023 Takayuki Sato. All Rights Reserved.
// Copyright (C) 2023-2024 Takayuki Sato. All Rights Reserved.
// This program is free software under MIT License.
// See the file LICENSE in this distribution for more details.

Expand Down Expand Up @@ -50,14 +50,14 @@ This function takes an array of option configurations: []OptCfg as the second
argument, and divides command line arguments to options and command arguments
with this configurations.
An option configuration has fields: Name, Aliases, HasArg, IsArray, Default,
An option configuration has fields: Name, Aliases, HasArg, IsArray, Defaults,
Desc, and ArgHelp.
Name field is an option name and it is used as an argument of the functions:
Cmd#HasOpt, Cmd#OptArg, and Cmd#OptArgs.
Aliases field is an array of option aliases.
HasArg field indicates the option requires one or more values.
IsArray field indicates the option can have multiple values.
Default field is an array of string which is used as default one or more
Defaults field is an array of string which is used as default one or more
values if the option is not specified.
Desc field is a description of the option for help text.
ArgHelp field is a text which is output after option name and aliases as an
Expand All @@ -75,7 +75,7 @@ option value in help text.
Aliases:[]string{"z"},
HasArg:true,
IsArray: true,
Default: [9,8,7],
Defaults: [9,8,7],
Desc:"This is description of baz.",
ArgHelp:"<text>",
},
Expand Down Expand Up @@ -168,7 +168,7 @@ And optarg is what to specify a text for an option argument value in help text.
// Desc: "This is description of foo-bar.",
// HasArg: false,
// IsArray: false,
// Default: []string(nil),
// Defaults: []string(nil),
// ArgHelp: "",
// },
// OptCfg{
Expand All @@ -177,7 +177,7 @@ And optarg is what to specify a text for an option argument value in help text.
// Desc: "This is description of baz.",
// HasArg: true,
// IsArray: true,
// Default: []string{"9","8","7"},
// Defaults: []string{"9","8","7"},
// ArgHelp: "<num>",
// },
// OptCfg{
Expand All @@ -186,7 +186,7 @@ And optarg is what to specify a text for an option argument value in help text.
// Desc: "This is description of qux.",
// HasArg: false,
// IsArray: false,
// Default: []string(nil),
// Defaults: []string(nil),
// ArgHelp: "",
// },
// }
Expand Down
8 changes: 4 additions & 4 deletions example_invalid-option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
func ExampleInvalidOption() {
optCfgs := []cliargs.OptCfg{
cliargs.OptCfg{
Name: "foo",
Default: []string{"123"},
HasArg: false,
Name: "foo",
Defaults: []string{"123"},
HasArg: false,
},
}

Expand All @@ -23,6 +23,6 @@ func ExampleInvalidOption() {
fmt.Printf("option: %s\n", ee.GetOpt())

// Output:
// error type: cliargs.ConfigHasDefaultButHasNoArg
// error type: cliargs.ConfigHasDefaultsButHasNoArg
// option: foo
}
32 changes: 16 additions & 16 deletions example_parse-for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ func ExampleParseFor() {
fmt.Printf("optCfgs[0].Aliases = %v\n", optCfgs[0].Aliases)
fmt.Printf("optCfgs[0].HasArg = %v\n", optCfgs[0].HasArg)
fmt.Printf("optCfgs[0].IsArray = %v\n", optCfgs[0].IsArray)
fmt.Printf("optCfgs[0].Default = %v\n", optCfgs[0].Default)
fmt.Printf("optCfgs[0].Defaults = %v\n", optCfgs[0].Defaults)
fmt.Printf("optCfgs[0].Desc = %v\n", optCfgs[0].Desc)

fmt.Printf("optCfgs[1].Name = %v\n", optCfgs[1].Name)
fmt.Printf("optCfgs[1].Aliases = %v\n", optCfgs[1].Aliases)
fmt.Printf("optCfgs[1].HasArg = %v\n", optCfgs[1].HasArg)
fmt.Printf("optCfgs[1].IsArray = %v\n", optCfgs[1].IsArray)
fmt.Printf("optCfgs[1].Default = %v\n", optCfgs[1].Default)
fmt.Printf("optCfgs[1].Defaults = %v\n", optCfgs[1].Defaults)
fmt.Printf("optCfgs[1].Desc = %v\n", optCfgs[1].Desc)
fmt.Printf("optCfgs[1].ArgHelp = %v\n", optCfgs[1].ArgHelp)

fmt.Printf("optCfgs[2].Name = %v\n", optCfgs[2].Name)
fmt.Printf("optCfgs[2].Aliases = %v\n", optCfgs[2].Aliases)
fmt.Printf("optCfgs[2].HasArg = %v\n", optCfgs[2].HasArg)
fmt.Printf("optCfgs[2].IsArray = %v\n", optCfgs[2].IsArray)
fmt.Printf("optCfgs[2].Default = %v\n", optCfgs[2].Default)
fmt.Printf("optCfgs[2].Defaults = %v\n", optCfgs[2].Defaults)
fmt.Printf("optCfgs[2].Desc = %v\n", optCfgs[2].Desc)
fmt.Printf("optCfgs[2].ArgHelp = %v\n", optCfgs[2].ArgHelp)

Expand All @@ -59,20 +59,20 @@ func ExampleParseFor() {
// optCfgs[0].Aliases = [f]
// optCfgs[0].HasArg = false
// optCfgs[0].IsArray = false
// optCfgs[0].Default = []
// optCfgs[0].Defaults = []
// optCfgs[0].Desc = FooBar description.
// optCfgs[1].Name = baz
// optCfgs[1].Aliases = [b]
// optCfgs[1].HasArg = true
// optCfgs[1].IsArray = false
// optCfgs[1].Default = [99]
// optCfgs[1].Defaults = [99]
// optCfgs[1].Desc = Baz description.
// optCfgs[1].ArgHelp = <num>
// optCfgs[2].Name = qux
// optCfgs[2].Aliases = [q]
// optCfgs[2].HasArg = true
// optCfgs[2].IsArray = true
// optCfgs[2].Default = [A B C]
// optCfgs[2].Defaults = [A B C]
// optCfgs[2].Desc = Qux description.
// optCfgs[2].ArgHelp = <text>
// options.FooBar = true
Expand All @@ -98,38 +98,38 @@ func ExampleMakeOptCfgsFor() {
fmt.Printf("optCfgs[0].Aliases = %v\n", optCfgs[0].Aliases)
fmt.Printf("optCfgs[0].HasArg = %v\n", optCfgs[0].HasArg)
fmt.Printf("optCfgs[0].IsArray = %v\n", optCfgs[0].IsArray)
fmt.Printf("optCfgs[0].Default = %v\n", optCfgs[0].Default)
fmt.Printf("optCfgs[0].Defaults = %v\n", optCfgs[0].Defaults)
fmt.Printf("optCfgs[0].Desc = %v\n", optCfgs[0].Desc)
fmt.Println()
fmt.Printf("optCfgs[1].Name = %v\n", optCfgs[1].Name)
fmt.Printf("optCfgs[1].Aliases = %v\n", optCfgs[1].Aliases)
fmt.Printf("optCfgs[1].HasArg = %v\n", optCfgs[1].HasArg)
fmt.Printf("optCfgs[1].IsArray = %v\n", optCfgs[1].IsArray)
fmt.Printf("optCfgs[1].Default = %v\n", optCfgs[1].Default)
fmt.Printf("optCfgs[1].Defaults = %v\n", optCfgs[1].Defaults)
fmt.Printf("optCfgs[1].Desc = %v\n", optCfgs[1].Desc)
fmt.Printf("optCfgs[1].ArgHelp = %v\n", optCfgs[1].ArgHelp)
fmt.Println()
fmt.Printf("optCfgs[2].Name = %v\n", optCfgs[2].Name)
fmt.Printf("optCfgs[2].Aliases = %v\n", optCfgs[2].Aliases)
fmt.Printf("optCfgs[2].HasArg = %v\n", optCfgs[2].HasArg)
fmt.Printf("optCfgs[2].IsArray = %v\n", optCfgs[2].IsArray)
fmt.Printf("optCfgs[2].Default = %v\n", optCfgs[2].Default)
fmt.Printf("optCfgs[2].Defaults = %v\n", optCfgs[2].Defaults)
fmt.Printf("optCfgs[2].Desc = %v\n", optCfgs[2].Desc)
fmt.Printf("optCfgs[2].ArgHelp = %v\n", optCfgs[2].ArgHelp)
fmt.Println()
fmt.Printf("optCfgs[3].Name = %v\n", optCfgs[3].Name)
fmt.Printf("optCfgs[3].Aliases = %v\n", optCfgs[3].Aliases)
fmt.Printf("optCfgs[3].HasArg = %v\n", optCfgs[3].HasArg)
fmt.Printf("optCfgs[3].IsArray = %v\n", optCfgs[3].IsArray)
fmt.Printf("optCfgs[3].Default = %v\n", optCfgs[3].Default)
fmt.Printf("optCfgs[3].Defaults = %v\n", optCfgs[3].Defaults)
fmt.Printf("optCfgs[3].Desc = %v\n", optCfgs[3].Desc)
fmt.Printf("optCfgs[3].ArgHelp = %v\n", optCfgs[3].ArgHelp)
fmt.Println()
fmt.Printf("optCfgs[4].Name = %v\n", optCfgs[4].Name)
fmt.Printf("optCfgs[4].Aliases = %v\n", optCfgs[4].Aliases)
fmt.Printf("optCfgs[4].HasArg = %v\n", optCfgs[4].HasArg)
fmt.Printf("optCfgs[4].IsArray = %v\n", optCfgs[4].IsArray)
fmt.Printf("optCfgs[4].Default = %v\n", optCfgs[4].Default)
fmt.Printf("optCfgs[4].Defaults = %v\n", optCfgs[4].Defaults)
fmt.Printf("optCfgs[4].Desc = %v\n", optCfgs[4].Desc)

// Output:
Expand All @@ -140,37 +140,37 @@ func ExampleMakeOptCfgsFor() {
// optCfgs[0].Aliases = [f]
// optCfgs[0].HasArg = false
// optCfgs[0].IsArray = false
// optCfgs[0].Default = []
// optCfgs[0].Defaults = []
// optCfgs[0].Desc = FooBar description
//
// optCfgs[1].Name = baz
// optCfgs[1].Aliases = [b]
// optCfgs[1].HasArg = true
// optCfgs[1].IsArray = false
// optCfgs[1].Default = [99]
// optCfgs[1].Defaults = [99]
// optCfgs[1].Desc = Baz description
// optCfgs[1].ArgHelp = <number>
//
// optCfgs[2].Name = Qux
// optCfgs[2].Aliases = []
// optCfgs[2].HasArg = true
// optCfgs[2].IsArray = false
// optCfgs[2].Default = [XXX]
// optCfgs[2].Defaults = [XXX]
// optCfgs[2].Desc = Qux description
// optCfgs[2].ArgHelp = <string>
//
// optCfgs[3].Name = quux
// optCfgs[3].Aliases = []
// optCfgs[3].HasArg = true
// optCfgs[3].IsArray = true
// optCfgs[3].Default = [A B C]
// optCfgs[3].Defaults = [A B C]
// optCfgs[3].Desc = Quux description
// optCfgs[3].ArgHelp = <array elem>
//
// optCfgs[4].Name = Corge
// optCfgs[4].Aliases = []
// optCfgs[4].HasArg = true
// optCfgs[4].IsArray = true
// optCfgs[4].Default = []
// optCfgs[4].Defaults = []
// optCfgs[4].Desc =
}
6 changes: 3 additions & 3 deletions example_parse-with_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ func ExampleParseWith() {
IsArray: true,
},
cliargs.OptCfg{
Name: "corge",
HasArg: true,
Default: []string{"99"},
Name: "corge",
HasArg: true,
Defaults: []string{"99"},
},
cliargs.OptCfg{
Name: "*",
Expand Down
30 changes: 15 additions & 15 deletions parse-for.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2023 Takayuki Sato. All Rights Reserved.
// Copyright (C) 2023-2024 Takayuki Sato. All Rights Reserved.
// This program is free software under MIT License.
// See the file LICENSE in this distribution for more details.

Expand All @@ -12,7 +12,7 @@ import (
"strings"
)

// OptionStoreIsNotChangeable is an error which indicates that the second
// OptionStoreIsNotChangeable is the error which indicates that the second
// argument of ParseFor function, which is set options produced by parsing
// command line arguments, is not a pointer.
type OptionStoreIsNotChangeable struct{}
Expand All @@ -21,7 +21,7 @@ func (e OptionStoreIsNotChangeable) Error() string {
return "OptionStoreIsNotChangeable{}"
}

// FailToParseInt is an error reaason which indicates that an option
// FailToParseInt is the error reaason which indicates that an option
// argument in command line arguments should be an integer but is invalid.
type FailToParseInt struct {
Option string
Expand All @@ -41,7 +41,7 @@ func (e FailToParseInt) Unwrap() error {
return e.cause
}

// FailToParseUint is an error which indicates that an option argument in
// FailToParseUint is the error which indicates that an option argument in
// command line arguments should be an unsigned integer but is invalid.
type FailToParseUint struct {
Option string
Expand All @@ -61,7 +61,7 @@ func (e FailToParseUint) Unwrap() error {
return e.cause
}

// FailToParseFloat is an error which indicates that an option argument in
// FailToParseFloat is the error which indicates that an option argument in
// command line arguments should be a floating point number but is invalid.
type FailToParseFloat struct {
Option string
Expand All @@ -81,7 +81,7 @@ func (e FailToParseFloat) Unwrap() error {
return e.cause
}

// IllegalOptionType is an error which indicates that a type of a field of the
// IllegalOptionType is the error which indicates that a type of a field of the
// option store is neither a boolean, a number, a string, nor an array of
// numbers or strings.
type IllegalOptionType struct {
Expand All @@ -96,8 +96,8 @@ func (e IllegalOptionType) Error() string {
e.Option, e.Field, e.Type.String())
}

// ParseFor is a function to parse command line arguments and set their values
// to the option store which is the second argument of this function.
// ParseFor is the function to parse command line arguments and set their
// values to the option store which is the second argument of this function.
// This function divides command line arguments to command arguments and
// options, then stores the options to the option store, and returns the
// command arguments with the generated option configuratins.
Expand Down Expand Up @@ -238,13 +238,13 @@ func newOptCfg(fld reflect.StructField) OptCfg {
desc := fld.Tag.Get("optdesc")

return OptCfg{
Name: name,
Aliases: aliases,
HasArg: hasArg,
IsArray: isArray,
Default: defaults,
Desc: desc,
ArgHelp: optArg,
Name: name,
Aliases: aliases,
HasArg: hasArg,
IsArray: isArray,
Defaults: defaults,
Desc: desc,
ArgHelp: optArg,
}
}

Expand Down
10 changes: 5 additions & 5 deletions parse-for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1519,31 +1519,31 @@ func TestMakeOptCfgsFor_multipleOptsAndMultipleArgs(t *testing.T) {
assert.Equal(t, optCfgs[0].Aliases, []string{"f"})
assert.False(t, optCfgs[0].HasArg)
assert.False(t, optCfgs[0].IsArray)
assert.Nil(t, optCfgs[0].Default)
assert.Nil(t, optCfgs[0].Defaults)
assert.NotNil(t, optCfgs[0].OnParsed)
assert.Equal(t, optCfgs[1].Name, "baz")
assert.Equal(t, optCfgs[1].Aliases, []string{"b"})
assert.True(t, optCfgs[1].HasArg)
assert.False(t, optCfgs[1].IsArray)
assert.Equal(t, optCfgs[1].Default, []string{"99"})
assert.Equal(t, optCfgs[1].Defaults, []string{"99"})
assert.NotNil(t, optCfgs[1].OnParsed)
assert.Equal(t, optCfgs[2].Name, "Qux")
assert.Equal(t, optCfgs[2].Aliases, []string(nil))
assert.True(t, optCfgs[2].HasArg)
assert.False(t, optCfgs[2].IsArray)
assert.Equal(t, optCfgs[2].Default, []string{"XXX"})
assert.Equal(t, optCfgs[2].Defaults, []string{"XXX"})
assert.NotNil(t, optCfgs[2].OnParsed)
assert.Equal(t, optCfgs[3].Name, "quux")
assert.Equal(t, optCfgs[3].Aliases, []string{})
assert.True(t, optCfgs[3].HasArg)
assert.True(t, optCfgs[3].IsArray)
assert.Equal(t, optCfgs[3].Default, []string{"A", "B", "C"})
assert.Equal(t, optCfgs[3].Defaults, []string{"A", "B", "C"})
assert.NotNil(t, optCfgs[3].OnParsed)
assert.Equal(t, optCfgs[4].Name, "Corge")
assert.Equal(t, optCfgs[4].Aliases, []string(nil))
assert.True(t, optCfgs[4].HasArg)
assert.True(t, optCfgs[4].IsArray)
assert.Equal(t, optCfgs[4].Default, []string(nil))
assert.Equal(t, optCfgs[4].Defaults, []string(nil))
assert.NotNil(t, optCfgs[4].OnParsed)

cmd, err1 := cliargs.ParseWith(osArgs, optCfgs)
Expand Down

0 comments on commit 835b0fa

Please sign in to comment.