Skip to content

Commit

Permalink
update!: modified OptCfg fields to separate store key and option name.
Browse files Browse the repository at this point in the history
  • Loading branch information
sttk committed Mar 17, 2024
1 parent 6b2652e commit 2f34206
Show file tree
Hide file tree
Showing 10 changed files with 667 additions and 464 deletions.
62 changes: 32 additions & 30 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,51 +49,53 @@ 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, Defaults,
Desc, and ArgHelp.
An option configuration has fields: StoreKey, Names, HasArg, IsArray, Defaults,
Desc, and ArgInHelp.
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.
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
ArgInHelp field is a text which is output after option name and aliases as an
option value in help text.
// osArgs := []string{"app", "--foo-bar", "hoge", "--baz", "1", "-z=2", "-x" "fuga"}
optCfgs := []cliargs.OptCfg{
cliargs.OptCfg{
Name:"foo-bar",
Desc:"This is description of foo-bar.",
StoreKey: "FooBar",
Names: []string{"foo-bar"},
Desc: "This is description of foo-bar.",
},
cliargs.OptCfg{
Name:"baz",
Aliases:[]string{"z"},
Names: []string{"baz", "z"},
HasArg:true,
IsArray: true,
Defaults: [9,8,7],
Desc:"This is description of baz.",
ArgHelp:"<text>",
},
cliargs.OptCfg{
Name:"*",
Names: []string{"*"},
Desc: "(Any options are accepted)",
},
}
cmd, err := cliargs.ParseWith(optCfgs)
cmd.Name // app
cmd.Args() // [hoge fuga]
cmd.HasOpt("foo-bar") // true
cmd.HasOpt("FooBar") // true
cmd.HasOpt("baz") // true
cmd.HasOpt("x") // true, due to "*" config
cmd.OptArg("foo-bar") // true
cmd.OptArg("FooBar") // true
cmd.OptArg("baz") // 1
cmd.OptArg("x") // true
cmd.OptArgs("foo-bar") // []
cmd.OptArgs("FooBar") // []
cmd.OptArgs("baz") // [1 2]
cmd.OptArgs("x") // []
Expand Down Expand Up @@ -121,7 +123,7 @@ This function creates a Cmd instance and also an array of OptCfg which is
transformed from these struct tags and is used to parse command line arguments.
The struct tags used in a option store struct are optcfg, optdesc, and optarg.
optcfg is what to specify option configurations other than Desc and AtgHelp.
optcfg is what to specify option configurations other than Desc and AtgInHelp.
The format of optcfg is as follows:
`optcfg:"name"` // only name
Expand All @@ -146,47 +148,47 @@ And optarg is what to specify a text for an option argument value in help text.
cmd, optCfgs, err := cliargs.ParseFor(osArgs, &options)
cmd.Name // app
cmd.Args() // [hoge fuga]
cmd.HasOpt("foo-bar") // true
cmd.HasOpt("baz") // true
cmd.HasOpt("x") // true
cmd.OptArg("foo-bar") // true
cmd.OptArg("baz") // 1
cmd.OptArg("x") // true
cmd.OptArgs("foo-bar") // []
cmd.OptArgs("baz") // [1 2]
cmd.OptArgs("x") // []
cmd.HasOpt("FooBar") // true
cmd.HasOpt("Baz") // true
cmd.HasOpt("Qux") // true
cmd.OptArg("FooBar") // true
cmd.OptArg("Baz") // 1
cmd.OptArg("Qux") // true
cmd.OptArgs("FooBar") // []
cmd.OptArgs("Baz") // [1 2]
cmd.OptArgs("Qux") // []
options.FooBar // true
options.Baz // [1 2]
options.Qux // true
optCfgs // []OptCfg{
// OptCfg{
// Name: "foo-bar",
// Aliases: []string{},
// StoreKey: "FooBar",
// Names: []string{"foo-bar"},
// Desc: "This is description of foo-bar.",
// HasArg: false,
// IsArray: false,
// Defaults: []string(nil),
// ArgHelp: "",
// ArgInHelp: "",
// },
// OptCfg{
// Name: "baz",
// Aliases: []string{"z"},
// StoreKey: "Baz",
// Aliases: []string{"baz", "z"},
// Desc: "This is description of baz.",
// HasArg: true,
// IsArray: true,
// Defaults: []string{"9","8","7"},
// ArgHelp: "<num>",
// ArgInHelp: "<num>",
// },
// OptCfg{
// Name: "qux",
// Aliases: []string{"x"},
// StoreKey: "Qux",
// Aliases: []string{"qux", "x"},
// Desc: "This is description of qux.",
// HasArg: false,
// IsArray: false,
// Defaults: []string(nil),
// ArgHelp: "",
// ArgInHelp: "",
// },
// }
Expand Down
2 changes: 1 addition & 1 deletion example_invalid-option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func ExampleInvalidOption() {
optCfgs := []cliargs.OptCfg{
cliargs.OptCfg{
Name: "foo",
Names: []string{"foo"},
Defaults: []string{"123"},
HasArg: false,
},
Expand Down
84 changes: 42 additions & 42 deletions example_parse-for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,28 @@ func ExampleParseFor() {
fmt.Printf("cmd.Name = %v\n", cmd.Name)
fmt.Printf("cmd.Args() = %v\n", cmd.Args())

fmt.Printf("optCfgs[0].Name = %v\n", optCfgs[0].Name)
fmt.Printf("optCfgs[0].Aliases = %v\n", optCfgs[0].Aliases)
fmt.Printf("optCfgs[0].StoreKey = %v\n", optCfgs[0].StoreKey)
fmt.Printf("optCfgs[0].Names = %v\n", optCfgs[0].Names)
fmt.Printf("optCfgs[0].HasArg = %v\n", optCfgs[0].HasArg)
fmt.Printf("optCfgs[0].IsArray = %v\n", optCfgs[0].IsArray)
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].StoreKey = %v\n", optCfgs[1].StoreKey)
fmt.Printf("optCfgs[1].Names = %v\n", optCfgs[1].Names)
fmt.Printf("optCfgs[1].HasArg = %v\n", optCfgs[1].HasArg)
fmt.Printf("optCfgs[1].IsArray = %v\n", optCfgs[1].IsArray)
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[1].ArgInHelp = %v\n", optCfgs[1].ArgInHelp)

fmt.Printf("optCfgs[2].Name = %v\n", optCfgs[2].Name)
fmt.Printf("optCfgs[2].Aliases = %v\n", optCfgs[2].Aliases)
fmt.Printf("optCfgs[2].StoreKey = %v\n", optCfgs[2].StoreKey)
fmt.Printf("optCfgs[2].Names = %v\n", optCfgs[2].Names)
fmt.Printf("optCfgs[2].HasArg = %v\n", optCfgs[2].HasArg)
fmt.Printf("optCfgs[2].IsArray = %v\n", optCfgs[2].IsArray)
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.Printf("optCfgs[2].ArgInHelp = %v\n", optCfgs[2].ArgInHelp)

fmt.Printf("options.FooBar = %v\n", options.FooBar)
fmt.Printf("options.Baz = %v\n", options.Baz)
Expand All @@ -55,26 +55,26 @@ func ExampleParseFor() {
// err = <nil>
// cmd.Name = app
// cmd.Args() = [c1 c2]
// optCfgs[0].Name = foo-bar
// optCfgs[0].Aliases = [f]
// optCfgs[0].StoreKey = FooBar
// optCfgs[0].Names = [foo-bar f]
// optCfgs[0].HasArg = false
// optCfgs[0].IsArray = false
// optCfgs[0].Defaults = []
// optCfgs[0].Desc = FooBar description.
// optCfgs[1].Name = baz
// optCfgs[1].Aliases = [b]
// optCfgs[1].StoreKey = Baz
// optCfgs[1].Names = [baz b]
// optCfgs[1].HasArg = true
// optCfgs[1].IsArray = false
// optCfgs[1].Defaults = [99]
// optCfgs[1].Desc = Baz description.
// optCfgs[1].ArgHelp = <num>
// optCfgs[2].Name = qux
// optCfgs[2].Aliases = [q]
// optCfgs[1].ArgInHelp = <num>
// optCfgs[2].StoreKey = Qux
// optCfgs[2].Names = [qux q]
// optCfgs[2].HasArg = true
// optCfgs[2].IsArray = true
// optCfgs[2].Defaults = [A B C]
// optCfgs[2].Desc = Qux description.
// optCfgs[2].ArgHelp = <text>
// optCfgs[2].ArgInHelp = <text>
// options.FooBar = true
// options.Baz = 12
// options.Qux = [D E]
Expand All @@ -94,39 +94,39 @@ func ExampleMakeOptCfgsFor() {
fmt.Printf("err = %v\n", err)
fmt.Printf("len(optCfgs) = %v\n", len(optCfgs))
fmt.Println()
fmt.Printf("optCfgs[0].Name = %v\n", optCfgs[0].Name)
fmt.Printf("optCfgs[0].Aliases = %v\n", optCfgs[0].Aliases)
fmt.Printf("optCfgs[0].StoreKey = %v\n", optCfgs[0].StoreKey)
fmt.Printf("optCfgs[0].Names = %v\n", optCfgs[0].Names)
fmt.Printf("optCfgs[0].HasArg = %v\n", optCfgs[0].HasArg)
fmt.Printf("optCfgs[0].IsArray = %v\n", optCfgs[0].IsArray)
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].StoreKey = %v\n", optCfgs[1].StoreKey)
fmt.Printf("optCfgs[1].Names = %v\n", optCfgs[1].Names)
fmt.Printf("optCfgs[1].HasArg = %v\n", optCfgs[1].HasArg)
fmt.Printf("optCfgs[1].IsArray = %v\n", optCfgs[1].IsArray)
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[1].ArgInHelp = %v\n", optCfgs[1].ArgInHelp)
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].StoreKey = %v\n", optCfgs[2].StoreKey)
fmt.Printf("optCfgs[2].Names = %v\n", optCfgs[2].Names)
fmt.Printf("optCfgs[2].HasArg = %v\n", optCfgs[2].HasArg)
fmt.Printf("optCfgs[2].IsArray = %v\n", optCfgs[2].IsArray)
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.Printf("optCfgs[2].ArgInHelp = %v\n", optCfgs[2].ArgInHelp)
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].StoreKey = %v\n", optCfgs[3].StoreKey)
fmt.Printf("optCfgs[3].Names = %v\n", optCfgs[3].Names)
fmt.Printf("optCfgs[3].HasArg = %v\n", optCfgs[3].HasArg)
fmt.Printf("optCfgs[3].IsArray = %v\n", optCfgs[3].IsArray)
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.Printf("optCfgs[3].ArgInHelp = %v\n", optCfgs[3].ArgInHelp)
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].StoreKey = %v\n", optCfgs[4].StoreKey)
fmt.Printf("optCfgs[4].Names = %v\n", optCfgs[4].Names)
fmt.Printf("optCfgs[4].HasArg = %v\n", optCfgs[4].HasArg)
fmt.Printf("optCfgs[4].IsArray = %v\n", optCfgs[4].IsArray)
fmt.Printf("optCfgs[4].Defaults = %v\n", optCfgs[4].Defaults)
Expand All @@ -136,39 +136,39 @@ func ExampleMakeOptCfgsFor() {
// err = <nil>
// len(optCfgs) = 5
//
// optCfgs[0].Name = foo-bar
// optCfgs[0].Aliases = [f]
// optCfgs[0].StoreKey = FooBar
// optCfgs[0].Names = [foo-bar f]
// optCfgs[0].HasArg = false
// optCfgs[0].IsArray = false
// optCfgs[0].Defaults = []
// optCfgs[0].Desc = FooBar description
//
// optCfgs[1].Name = baz
// optCfgs[1].Aliases = [b]
// optCfgs[1].StoreKey = Baz
// optCfgs[1].Names = [baz b]
// optCfgs[1].HasArg = true
// optCfgs[1].IsArray = false
// optCfgs[1].Defaults = [99]
// optCfgs[1].Desc = Baz description
// optCfgs[1].ArgHelp = <number>
// optCfgs[1].ArgInHelp = <number>
//
// optCfgs[2].Name = Qux
// optCfgs[2].Aliases = []
// optCfgs[2].StoreKey = Qux
// optCfgs[2].Names = [Qux]
// optCfgs[2].HasArg = true
// optCfgs[2].IsArray = false
// optCfgs[2].Defaults = [XXX]
// optCfgs[2].Desc = Qux description
// optCfgs[2].ArgHelp = <string>
// optCfgs[2].ArgInHelp = <string>
//
// optCfgs[3].Name = quux
// optCfgs[3].Aliases = []
// optCfgs[3].StoreKey = Quux
// optCfgs[3].Names = [quux]
// optCfgs[3].HasArg = true
// optCfgs[3].IsArray = true
// optCfgs[3].Defaults = [A B C]
// optCfgs[3].Desc = Quux description
// optCfgs[3].ArgHelp = <array elem>
// optCfgs[3].ArgInHelp = <array elem>
//
// optCfgs[4].Name = Corge
// optCfgs[4].Aliases = []
// optCfgs[4].StoreKey = Corge
// optCfgs[4].Names = [Corge]
// optCfgs[4].HasArg = true
// optCfgs[4].IsArray = true
// optCfgs[4].Defaults = []
Expand Down
26 changes: 13 additions & 13 deletions example_parse-with_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,47 @@ func ExampleParseWith() {
}
optCfgs := []cliargs.OptCfg{
cliargs.OptCfg{
Name: "foo-bar",
Names: []string{"foo-bar"},
},
cliargs.OptCfg{
Name: "baz",
Aliases: []string{"z"},
HasArg: true,
IsArray: true,
StoreKey: "Bazoo",
Names: []string{"baz", "z"},
HasArg: true,
IsArray: true,
},
cliargs.OptCfg{
Name: "corge",
Names: []string{"corge"},
HasArg: true,
Defaults: []string{"99"},
},
cliargs.OptCfg{
Name: "*",
StoreKey: "*",
},
}

cmd, err := cliargs.ParseWith(osArgs, optCfgs)
fmt.Printf("err = %v\n", err)
fmt.Printf("cmd.Name = %v\n", cmd.Name)
fmt.Printf("cmd.HasOpt(\"foo-bar\") = %v\n", cmd.HasOpt("foo-bar"))
fmt.Printf("cmd.HasOpt(\"baz\") = %v\n", cmd.HasOpt("baz"))
fmt.Printf("cmd.HasOpt(\"Bazoo\") = %v\n", cmd.HasOpt("Bazoo"))
fmt.Printf("cmd.HasOpt(\"X\") = %v\n", cmd.HasOpt("X"))
fmt.Printf("cmd.HasOpt(\"corge\") = %v\n", cmd.HasOpt("corge"))
fmt.Printf("cmd.OptArg(\"baz\") = %v\n", cmd.OptArg("baz"))
fmt.Printf("cmd.OptArg(\"Bazoo\") = %v\n", cmd.OptArg("Bazoo"))
fmt.Printf("cmd.OptArg(\"corge\") = %v\n", cmd.OptArg("corge"))
fmt.Printf("cmd.OptArgs(\"baz\") = %v\n", cmd.OptArgs("baz"))
fmt.Printf("cmd.OptArgs(\"Bazoo\") = %v\n", cmd.OptArgs("Bazoo"))
fmt.Printf("cmd.OptArgs(\"corge\") = %v\n", cmd.OptArgs("corge"))
fmt.Printf("cmd.Args() = %v\n", cmd.Args())

// Output:
// err = <nil>
// cmd.Name = app
// cmd.HasOpt("foo-bar") = true
// cmd.HasOpt("baz") = true
// cmd.HasOpt("Bazoo") = true
// cmd.HasOpt("X") = true
// cmd.HasOpt("corge") = true
// cmd.OptArg("baz") = 1
// cmd.OptArg("Bazoo") = 1
// cmd.OptArg("corge") = 99
// cmd.OptArgs("baz") = [1 2]
// cmd.OptArgs("Bazoo") = [1 2]
// cmd.OptArgs("corge") = [99]
// cmd.Args() = [qux quux]
}
Loading

0 comments on commit 2f34206

Please sign in to comment.