Skip to content

Commit

Permalink
update: changed APIs to print help (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
sttk committed May 10, 2023
1 parent c46e9f6 commit 229b81e
Show file tree
Hide file tree
Showing 7 changed files with 720 additions and 587 deletions.
20 changes: 10 additions & 10 deletions example_parse-for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ func ExampleParseFor() {
fmt.Printf("optCfgs[1].IsArray = %v\n", optCfgs[1].IsArray)
fmt.Printf("optCfgs[1].Default = %v\n", optCfgs[1].Default)
fmt.Printf("optCfgs[1].Desc = %v\n", optCfgs[1].Desc)
fmt.Printf("optCfgs[1].HelpArg = %v\n", optCfgs[1].HelpArg)
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].Desc = %v\n", optCfgs[2].Desc)
fmt.Printf("optCfgs[2].HelpArg = %v\n", optCfgs[2].HelpArg)
fmt.Printf("optCfgs[2].ArgHelp = %v\n", optCfgs[2].ArgHelp)

fmt.Printf("options.FooBar = %v\n", options.FooBar)
fmt.Printf("options.Baz = %v\n", options.Baz)
Expand All @@ -66,14 +66,14 @@ func ExampleParseFor() {
// optCfgs[1].IsArray = false
// optCfgs[1].Default = [99]
// optCfgs[1].Desc = Baz description.
// optCfgs[1].HelpArg = <num>
// 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].Desc = Qux description.
// optCfgs[2].HelpArg = <text>
// optCfgs[2].ArgHelp = <text>
// options.FooBar = true
// options.Baz = 12
// options.Qux = [D E]
Expand Down Expand Up @@ -106,23 +106,23 @@ func ExampleMakeOptCfgsFor() {
fmt.Printf("optCfgs[1].IsArray = %v\n", optCfgs[1].IsArray)
fmt.Printf("optCfgs[1].Default = %v\n", optCfgs[1].Default)
fmt.Printf("optCfgs[1].Desc = %v\n", optCfgs[1].Desc)
fmt.Printf("optCfgs[1].HelpArg = %v\n", optCfgs[1].HelpArg)
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].Desc = %v\n", optCfgs[2].Desc)
fmt.Printf("optCfgs[2].HelpArg = %v\n", optCfgs[2].HelpArg)
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].Desc = %v\n", optCfgs[3].Desc)
fmt.Printf("optCfgs[3].HelpArg = %v\n", optCfgs[3].HelpArg)
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)
Expand All @@ -148,23 +148,23 @@ func ExampleMakeOptCfgsFor() {
// optCfgs[1].IsArray = false
// optCfgs[1].Default = [99]
// optCfgs[1].Desc = Baz description
// optCfgs[1].HelpArg = <number>
// 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].Desc = Qux description
// optCfgs[2].HelpArg = <string>
// 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].Desc = Quux description
// optCfgs[3].HelpArg = <array elem>
// optCfgs[3].ArgHelp = <array elem>
//
// optCfgs[4].Name = Corge
// optCfgs[4].Aliases = []
Expand Down
60 changes: 31 additions & 29 deletions example_print-help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,48 @@ import (
"github.com/sttk-go/cliargs"
)

func ExamplePrintHelp() {
func ExampleHelp_Print() {
type MyOptions struct {
FooBar bool `optcfg:"foo-bar,f" optdesc:"FooBar is a flag.\nThis flag is foo bar."`
Baz int `optcfg:"baz,b=99" optdesc:"Baz is a integer."`
Qux string `optcfg:"=XXX" optdesc:"Qux is a string."`
Baz int `optcfg:"baz,b=99" optdesc:"Baz is a integer." optarg:"<num>"`
Qux string `optcfg:"=XXX" optdesc:"Qux is a string." optarg:"<text>"`
Quux []string `optcfg:"quux=[A,B,C]" optdesc:"Quux is a string array."`
}
options := MyOptions{}
optCfgs, _ := cliargs.MakeOptCfgsFor(&options)
wrapOpts := cliargs.WrapOpts{MarginLeft: 5, MarginRight: 2, Indent: 10}

usage := "This is the usage section."
err := cliargs.PrintHelp(usage, optCfgs, wrapOpts)
fmt.Printf("\nerr = %v\n", err)
help := cliargs.NewHelp(5, 2)
help.AddText("This is the usage section.")
help.AddOpts(optCfgs, 10, 1)

help.Print()

// Output:
// This is the usage section.
// --foo-bar, -f
// FooBar is a flag.
// This flag is foo bar.
// --baz, -b
// Baz is a integer.
// --Qux Qux is a string.
// --quux Quux is a string array.
//
// err = <nil>
// --foo-bar, -f
// FooBar is a flag.
// This flag is foo bar.
// --baz, -b <num>
// Baz is a integer.
// --Qux <text>
// Qux is a string.
// --quux Quux is a string array.
}

func ExampleMakeHelp() {
func ExampleHelp_Iter() {
type MyOptions struct {
FooBar bool `optcfg:"foo-bar,f" optdesc:"FooBar is a flag.\nThis flag is foo bar."`
Baz int `optcfg:"baz,b=99" optdesc:"Baz is a integer."`
Qux string `optcfg:"=XXX" optdesc:"Qux is a string."`
Baz int `optcfg:"baz,b=99" optdesc:"Baz is a integer." optarg:"<num>"`
Qux string `optcfg:"=XXX" optdesc:"Qux is a string." optarg:"<text>"`
Quux []string `optcfg:"quux=[A,B,C]" optdesc:"Quux is a string array."`
}
options := MyOptions{}
optCfgs, _ := cliargs.MakeOptCfgsFor(&options)
wrapOpts := cliargs.WrapOpts{MarginLeft: 5, MarginRight: 2}

usage := "This is the usage section."
iter, err := cliargs.MakeHelp(usage, optCfgs, wrapOpts)
fmt.Printf("\nerr = %v\n", err)
help := cliargs.NewHelp(5, 2)
help.AddText("This is the usage section.")
help.AddOpts(optCfgs, 10, 1)
iter := help.Iter()

for {
line, status := iter.Next()
Expand All @@ -57,11 +57,13 @@ func ExampleMakeHelp() {
}

// Output:
// err = <nil>
// This is the usage section.
// --foo-bar, -f FooBar is a flag.
// This flag is foo bar.
// --baz, -b Baz is a integer.
// --Qux Qux is a string.
// --quux Quux is a string array.
// --foo-bar, -f
// FooBar is a flag.
// This flag is foo bar.
// --baz, -b <num>
// Baz is a integer.
// --Qux <text>
// Qux is a string.
// --quux Quux is a string array.
}
6 changes: 3 additions & 3 deletions parse-for.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ func newOptCfg(fld reflect.StructField) OptCfg {
}
}

var helpArg string
var optArg string
if hasArg {
helpArg = fld.Tag.Get("optarg")
optArg = fld.Tag.Get("optarg")
}

desc := fld.Tag.Get("optdesc")
Expand All @@ -239,7 +239,7 @@ func newOptCfg(fld reflect.StructField) OptCfg {
IsArray: isArray,
Default: defaults,
Desc: desc,
HelpArg: helpArg,
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 @@ -1622,11 +1622,11 @@ func TestMakeOptCfgsFor_optionParam(t *testing.T) {

optCfgs, err0 := cliargs.MakeOptCfgsFor(&options)
assert.Nil(t, err0)
assert.Equal(t, optCfgs[0].HelpArg, "")
assert.Equal(t, optCfgs[1].HelpArg, "bbb")
assert.Equal(t, optCfgs[2].HelpArg, "ccc")
assert.Equal(t, optCfgs[3].HelpArg, "ddd (multiple)")
assert.Equal(t, optCfgs[4].HelpArg, "")
assert.Equal(t, optCfgs[0].ArgHelp, "")
assert.Equal(t, optCfgs[1].ArgHelp, "bbb")
assert.Equal(t, optCfgs[2].ArgHelp, "ccc")
assert.Equal(t, optCfgs[3].ArgHelp, "ddd (multiple)")
assert.Equal(t, optCfgs[4].ArgHelp, "")
}

func TestParseFor_optCfgHasUnsupportedType(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions parse-with.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const anyOption = "*"

// OptCfg is a structure that represents an option configuration.
// An option configuration consists of fields: Name, Aliases, HasArg,
// IsArray, Default, OnParsed, Desc, and HelpArg.
// IsArray, Default, 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
Expand All @@ -92,7 +92,7 @@ const anyOption = "*"
//
// Desc is the field to set the description of the option.
//
// HelpArg is a display at a argument position of this option in a help text.
// ArgHelp is a display at a argument position of this option in a help text.
// This string is for a display like: -o, --option <value>.
type OptCfg struct {
Name string
Expand All @@ -102,7 +102,7 @@ type OptCfg struct {
Default []string
OnParsed *func([]string) error
Desc string
HelpArg string
ArgHelp string
}

// ParseWith is a function which parses command line arguments with option
Expand All @@ -115,7 +115,7 @@ type OptCfg struct {
//
// This function allows only options declared in option configurations.
// A option configuration has fields: Name, Aliases, HasArg, IsArray, and
// Default, HelpArg.
// Default, 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
Expand Down
Loading

0 comments on commit 229b81e

Please sign in to comment.