From 835b0fa4d372551c963d7c5cb064c7faa087ba81 Mon Sep 17 00:00:00 2001 From: sttk Date: Tue, 6 Feb 2024 00:42:33 +0900 Subject: [PATCH] fix: renamed .Default in OptCfg to .Defaults --- doc.go | 14 ++++---- example_invalid-option_test.go | 8 ++--- example_parse-for_test.go | 32 ++++++++--------- example_parse-with_test.go | 6 ++-- parse-for.go | 30 ++++++++-------- parse-for_test.go | 10 +++--- parse-with.go | 64 +++++++++++++++++----------------- parse-with_test.go | 12 +++---- parse.go | 27 +++++++------- 9 files changed, 102 insertions(+), 101 deletions(-) diff --git a/doc.go b/doc.go index 16def18..694a978 100644 --- a/doc.go +++ b/doc.go @@ -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. @@ -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 @@ -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:"", }, @@ -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{ @@ -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: "", // }, // OptCfg{ @@ -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: "", // }, // } diff --git a/example_invalid-option_test.go b/example_invalid-option_test.go index d96604c..e62dd60 100644 --- a/example_invalid-option_test.go +++ b/example_invalid-option_test.go @@ -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, }, } @@ -23,6 +23,6 @@ func ExampleInvalidOption() { fmt.Printf("option: %s\n", ee.GetOpt()) // Output: - // error type: cliargs.ConfigHasDefaultButHasNoArg + // error type: cliargs.ConfigHasDefaultsButHasNoArg // option: foo } diff --git a/example_parse-for_test.go b/example_parse-for_test.go index 10dd163..a588a46 100644 --- a/example_parse-for_test.go +++ b/example_parse-for_test.go @@ -28,14 +28,14 @@ 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) @@ -43,7 +43,7 @@ func ExampleParseFor() { 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) @@ -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 = // 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 = // options.FooBar = true @@ -98,14 +98,14 @@ 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() @@ -113,7 +113,7 @@ func ExampleMakeOptCfgsFor() { 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() @@ -121,7 +121,7 @@ func ExampleMakeOptCfgsFor() { 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() @@ -129,7 +129,7 @@ func ExampleMakeOptCfgsFor() { 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: @@ -140,14 +140,14 @@ 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 = // @@ -155,7 +155,7 @@ func ExampleMakeOptCfgsFor() { // 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 = // @@ -163,7 +163,7 @@ func ExampleMakeOptCfgsFor() { // 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 = // @@ -171,6 +171,6 @@ func ExampleMakeOptCfgsFor() { // optCfgs[4].Aliases = [] // optCfgs[4].HasArg = true // optCfgs[4].IsArray = true - // optCfgs[4].Default = [] + // optCfgs[4].Defaults = [] // optCfgs[4].Desc = } diff --git a/example_parse-with_test.go b/example_parse-with_test.go index 8b45020..3d62514 100644 --- a/example_parse-with_test.go +++ b/example_parse-with_test.go @@ -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: "*", diff --git a/parse-for.go b/parse-for.go index 128bc9c..0eb68d6 100644 --- a/parse-for.go +++ b/parse-for.go @@ -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. @@ -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{} @@ -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 @@ -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 @@ -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 @@ -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 { @@ -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. @@ -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, } } diff --git a/parse-for_test.go b/parse-for_test.go index be1d8bb..ddf1dbd 100644 --- a/parse-for_test.go +++ b/parse-for_test.go @@ -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) diff --git a/parse-with.go b/parse-with.go index cfa5ea6..b13e728 100644 --- a/parse-with.go +++ b/parse-with.go @@ -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. @@ -9,7 +9,7 @@ import ( "path" ) -// ConfigIsArrayButHasNoArg is an error which indicates that an option +// ConfigIsArrayButHasNoArg is the error which indicates that an option // configuration contradicts that the option must be an array // (.IsArray = true) but must have no option argument (.HasArg = false). type ConfigIsArrayButHasNoArg struct{ Option string } @@ -24,22 +24,22 @@ func (e ConfigIsArrayButHasNoArg) GetOpt() string { return e.Option } -// ConfigHasDefaultButHasNoArg is an error which indicates that an option +// ConfigHasDefaultsButHasNoArg is the error which indicates that an option // configuration contradicts that the option has default value -// (.Default != nil) but must have no option argument (.HasArg = false). -type ConfigHasDefaultButHasNoArg struct{ Option string } +// (.Defaults != nil) but must have no option argument (.HasArg = false). +type ConfigHasDefaultsButHasNoArg struct{ Option string } // Error is the method to retrieve the message of this error. -func (e ConfigHasDefaultButHasNoArg) Error() string { - return fmt.Sprintf("ConfigHasDefaultButHasNoArg{Option:%s}", e.Option) +func (e ConfigHasDefaultsButHasNoArg) Error() string { + return fmt.Sprintf("ConfigHasDefaultsButHasNoArg{Option:%s}", e.Option) } // GetOpt is the method to retrieve the option that caused this error. -func (e ConfigHasDefaultButHasNoArg) GetOpt() string { +func (e ConfigHasDefaultsButHasNoArg) GetOpt() string { return e.Option } -// UnconfiguredOption is an error which indicates that there is no +// UnconfiguredOption is the error which indicates that there is no // configuration about the input option. type UnconfiguredOption struct{ Option string } @@ -53,7 +53,7 @@ func (e UnconfiguredOption) GetOpt() string { return e.Option } -// OptionNeedsArg is an error which indicates that an option is input with +// OptionNeedsArg is the error which indicates that an option is input with // no option argument though its option configuration requires option // argument (.HasArg = true). type OptionNeedsArg struct{ Option string } @@ -68,7 +68,7 @@ func (e OptionNeedsArg) GetOpt() string { return e.Option } -// OptionTakesNoArg is an error which indicates that an option isinput with +// OptionTakesNoArg is the error which indicates that an option is input with // an option argument though its option configuration does not accept option // arguments (.HasArg = false). type OptionTakesNoArg struct{ Option string } @@ -83,7 +83,7 @@ func (e OptionTakesNoArg) GetOpt() string { return e.Option } -// OptionIsNotArray is an error which indicates that an option is input with +// OptionIsNotArray is the error which indicates that an option is input with // an option argument multiple times though its option configuration specifies // the option is not an array (.IsArray = false). type OptionIsNotArray struct{ Option string } @@ -100,9 +100,9 @@ func (e OptionIsNotArray) GetOpt() string { const anyOption = "*" -// OptCfg is a structure that represents an option configuration. +// OptCfg is the structure that represents an option configuration. // An option configuration consists of fields: Name, Aliases, HasArg, -// IsArray, Default, OnParsed, Desc, and ArgHelp. +// IsArray, Defaults, OnParsed, Desc, and ArgHelp. // // Name is the option name and Aliases are the another names. // Options given by those names in command line arguments are all registered to @@ -117,8 +117,8 @@ const anyOption = "*" // If both HasArg and IsArray are false, the option can take no option // argument. // -// Default is the field to specify the default value for when the option is not -// given in command line arguments. +// Defaults is the field to specify the default value for when the option is +// not given in command line arguments. // // OnParsed is the field for the event handler which is called when the option // has been parsed. @@ -135,13 +135,13 @@ type OptCfg struct { Aliases []string HasArg bool IsArray bool - Default []string + Defaults []string OnParsed *func([]string) error Desc string ArgHelp string } -// ParseWith is a function which parses command line arguments with option +// ParseWith is the function which parses command line arguments with option // configurations. // This function divides command line arguments to command arguments and // options, and an option consists of a name and option arguments. @@ -150,21 +150,21 @@ type OptCfg struct { // see the comment of the function. // // This function allows only options declared in option configurations. -// A option configuration has fields: Name, Aliases, HasArg, IsArray, and -// Default, ArgHelp. +// A option configuration has fields: Name, Aliases, HasArg, IsArray, Defaults, +// Desc and ArgHelp. // When an option matches Name or includes in Aliases in an option -// configuration, the option is registered in Args with the Name. -// If both HasParam and IsArray are true, the option can has one or multiple -// option parameters, and if HasParam is true and IsArray is false, the option -// can has only one option parameter, otherwise the option cannot have option -// parameter. -// If Default is specified and the option is not given in command line -// arguments, the value of Default is set to the option parameter. +// configuration, the option is registered into Cmd with the Name. +// If both HasArg and IsArray are true, the option can have one or multiple +// option arguments, and if HasArg is true and IsArray is false, the option +// can have only one option argument, otherwise the option cannot have option +// argument. +// If Defaults is specified and the option is not given in command line +// arguments, the value of Defaults is set to the option argument. // // If options not declared in option configurations are given in command line // arguments, this function basically returns UnconfiguredOption error. // If you want to allow other options, add an option configuration of which -// Name is "*" (but HasParam and IsArray of this configuration is ignored). +// Name is "*" (but HasArg and IsArray of this configuration is ignored). func ParseWith(osArgs []string, optCfgs []OptCfg) (Cmd, error) { var cmdName string if len(osArgs) > 0 { @@ -179,8 +179,8 @@ func ParseWith(osArgs []string, optCfgs []OptCfg) (Cmd, error) { err := ConfigIsArrayButHasNoArg{Option: cfg.Name} return Cmd{Name: cmdName, args: empty}, err } - if cfg.Default != nil { - err := ConfigHasDefaultButHasNoArg{Option: cfg.Name} + if cfg.Defaults != nil { + err := ConfigHasDefaultsButHasNoArg{Option: cfg.Name} return Cmd{Name: cmdName, args: empty}, err } } @@ -260,8 +260,8 @@ func ParseWith(osArgs []string, optCfgs []OptCfg) (Cmd, error) { for _, cfg := range optCfgs { arr, exists := opts[cfg.Name] - if !exists && cfg.Default != nil { - arr = cfg.Default + if !exists && cfg.Defaults != nil { + arr = cfg.Defaults opts[cfg.Name] = arr } if cfg.OnParsed != nil { diff --git a/parse-with_test.go b/parse-with_test.go index 79b43b0..e552ea4 100644 --- a/parse-with_test.go +++ b/parse-with_test.go @@ -884,8 +884,8 @@ func TestParseWith_oneCfgIsNotArrayButOptsAreMultiple(t *testing.T) { func TestParseWith_specifyDefault(t *testing.T) { osArgs := []string{"app"} optCfgs := []cliargs.OptCfg{ - cliargs.OptCfg{Name: "bar", HasArg: true, Default: []string{"A"}}, - cliargs.OptCfg{Name: "baz", HasArg: true, IsArray: true, Default: []string{"A"}}, + cliargs.OptCfg{Name: "bar", HasArg: true, Defaults: []string{"A"}}, + cliargs.OptCfg{Name: "baz", HasArg: true, IsArray: true, Defaults: []string{"A"}}, } cmd, err := cliargs.ParseWith(osArgs, optCfgs) @@ -904,16 +904,16 @@ func TestParseWith_specifyDefault(t *testing.T) { func TestParseWith_oneCfgHasNoArgButHasDefault(t *testing.T) { optCfgs := []cliargs.OptCfg{ - cliargs.OptCfg{Name: "foo-bar", HasArg: false, Default: []string{"A"}}, + cliargs.OptCfg{Name: "foo-bar", HasArg: false, Defaults: []string{"A"}}, } osArgs := []string{"app"} cmd, err := cliargs.ParseWith(osArgs, optCfgs) assert.NotNil(t, err) - assert.Equal(t, err.Error(), "ConfigHasDefaultButHasNoArg{Option:foo-bar}") + assert.Equal(t, err.Error(), "ConfigHasDefaultsButHasNoArg{Option:foo-bar}") switch e := err.(type) { - case cliargs.ConfigHasDefaultButHasNoArg: + case cliargs.ConfigHasDefaultsButHasNoArg: assert.Equal(t, e.Option, "foo-bar") default: assert.Fail(t, err.Error()) @@ -946,7 +946,7 @@ func TestParseWith_multipleArgs(t *testing.T) { HasArg: true, IsArray: true, }, - cliargs.OptCfg{Name: "corge", HasArg: true, Default: []string{"99"}}, + cliargs.OptCfg{Name: "corge", HasArg: true, Defaults: []string{"99"}}, cliargs.OptCfg{Name: "*"}, } diff --git a/parse.go b/parse.go index b076f0f..6ba7786 100644 --- a/parse.go +++ b/parse.go @@ -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. @@ -12,14 +12,14 @@ import ( "unicode" ) -// InvalidOption is an error interface which provides method declarations -// to retrieve the option that caused this error and an error message. +// InvalidOption is the error interface which provides method declarations +// to retrieve an option that caused this error and an error message. type InvalidOption interface { GetOpt() string Error() string } -// OptionHasInvalidChar is an error which indicates that an invalid character +// OptionHasInvalidChar is the error which indicates that an invalid character // is found in the option. type OptionHasInvalidChar struct{ Option string } @@ -51,25 +51,25 @@ var ( } ) -// Cmd is a structure which contains a command name, command arguments, and +// Cmd is the structure which contains a command name, command arguments, and // option arguments that are parsed from command line arguments without // configurations. -// And this provides methods to check if they are specified or to obtain them. +// And this provides methods to check if they are specified and to obtain them. type Cmd struct { Name string args []string opts map[string][]string } -// HasOpt is a method which checks if the option is specified in command line +// HasOpt is the method which checks if the option is specified in command line // arguments. func (cmd Cmd) HasOpt(name string) bool { _, exists := cmd.opts[name] return exists } -// OptArg is a method to get a option argument which is firstly specified -// with opt in command line arguments. +// OptArg is the method to get the first option argument of the specified named +// option. func (cmd Cmd) OptArg(name string) string { arr := cmd.opts[name] // If no entry, map returns a nil slice. @@ -82,19 +82,20 @@ func (cmd Cmd) OptArg(name string) string { } } -// OptArgs is a method to get option arguments which are all specified with -// name in command line arguments. +// OptArgs is the method to get the option arguments which are all specified +// with name in command line arguments. func (cmd Cmd) OptArgs(name string) []string { return cmd.opts[name] } -// Args is a method to get command arguments which are specified in command +// Args is the method to get command arguments which are specified in command // line arguments and are not associated with any options. func (cmd Cmd) Args() []string { return cmd.args } -// Parse is a function to parse command line arguments without configurations. +// Parse is the function to parse command line arguments without +// configurations. // This function divides command line arguments to command arguments, which // are not associated with any options, and options, of which each has a name // and option arguments.