From 24261bbf420502500b38bdfdcd72820cd0855fbb Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Sun, 24 Sep 2023 13:07:40 +0300 Subject: [PATCH] [options] Improve usage examples --- CHANGELOG.md | 1 + options/example_test.go | 46 +++++++++++++++++++++++++++++++---------- options/options_test.go | 2 ++ 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72505e12..7b795f04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/options/example_test.go b/options/example_test.go index 673c633f..c92f0a48 100644 --- a/options/example_test.go +++ b/options/example_test.go @@ -161,7 +161,7 @@ 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) @@ -169,6 +169,20 @@ func ExampleGetF() { 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"}, @@ -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() @@ -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() diff --git a/options/options_test.go b/options/options_test.go index e1800237..86ea299f 100644 --- a/options/options_test.go +++ b/options/options_test.go @@ -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) @@ -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) {