Skip to content

Commit

Permalink
[options] Improve usage examples
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Sep 24, 2023
1 parent 27661e2 commit 24261bb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* `[options]` Added merge symbol customization
* `[options]` Added method `Split` for splitting string value of mergeble option
* `[options]` Improve usage examples

### 12.76.1

Expand Down
46 changes: 35 additions & 11 deletions options/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,28 @@ func ExampleGetB() {
func ExampleGetF() {
args, _ := Parse(Map{
"u:user": {Type: STRING, Value: "john"},
"l:lines": {Type: INT, Min: 1, Max: 100},
"r:ratio": {Type: FLOAT},
})

fmt.Printf("Arguments: %v\n", args)
fmt.Printf("User: %s\n", GetS("u:user"))
fmt.Printf("Ratio: %g\n", GetF("r:ratio"))
}

func ExampleSplit() {
args, _ := Parse(Map{
"u:user": {Mergeble: true},
"r:ratio": {Type: FLOAT},
})

// Use null-terminated string instead of default spaces for merging
MergeSymbol = "\x00"

fmt.Printf("Arguments: %v\n", args)
fmt.Printf("Users: %s\n", Split("u:user"))
fmt.Printf("Ratio: %g\n", GetF("r:ratio"))
}

func ExampleIs() {
Parse(Map{
"u:user": {Type: STRING, Value: "john"},
Expand Down Expand Up @@ -291,6 +305,26 @@ func ExampleOptions_GetB() {
// Force: true
}

func ExampleOptions_GetF() {
opts := NewOptions()

// Add options
opts.AddMap(Map{
"u:user": {Type: STRING, Value: "john"},
"r:ratio": {Type: FLOAT},
})

args, _ := opts.Parse([]string{"-u", "bob", "-r", "2.35", "file.txt"})

fmt.Printf("Arguments: %v\n", args)
fmt.Printf("User: %s\n", opts.GetS("u:user"))
fmt.Printf("Ratio: %g\n", opts.GetF("r:ratio"))
// Output:
// Arguments: [file.txt]
// User: bob
// Ratio: 2.35
}

func ExampleOptions_Split() {
opts := NewOptions()

Expand All @@ -315,16 +349,6 @@ func ExampleOptions_Split() {
// Ratio: 3.14
}

func ExampleOptions_GetF() {
opts := NewOptions()

// Add options
opts.AddMap(Map{
"u:user": {Type: STRING, Value: "john"},
"r:ratio": {Type: FLOAT},
})
}

func ExampleOptions_Has() {
opts := NewOptions()

Expand Down
2 changes: 2 additions & 0 deletions options/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (s *OptUtilSuite) TestGlobal(c *C) {
c.Assert(GetI("i:int"), Equals, 0)
c.Assert(GetF("f:float"), Equals, 0.0)
c.Assert(GetB("b:bool"), Equals, false)
c.Assert(Split("s:string"), IsNil)
c.Assert(Is("s:string", ""), Equals, false)
c.Assert(Has("s:string"), Equals, false)

Expand Down Expand Up @@ -110,6 +111,7 @@ func (s *OptUtilSuite) TestGlobal(c *C) {
c.Assert(GetB("b:bool"), Equals, true)
c.Assert(Is("s:string", "Test"), Equals, true)
c.Assert(Is("string1", "Test"), Equals, false)
c.Assert(Split("s:string"), DeepEquals, []string{"Test"})
}

func (s *OptUtilSuite) TestLimiters(c *C) {
Expand Down

0 comments on commit 24261bb

Please sign in to comment.