forked from spf13/pflag
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add multiline wrapping support (spf13#155)
* Add multiline wrapping support With reference to golang/go#20799, pflag now will respect newline in usage string and do wrap accordingly. Also add test cases for testing * Break at \n only if \n pos<wrap
- Loading branch information
Showing
2 changed files
with
83 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package pflag | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"testing" | ||
) | ||
|
||
const expectedOutput = ` --long-form Some description | ||
--long-form2 Some description | ||
with multiline | ||
-s, --long-name Some description | ||
-t, --long-name2 Some description with | ||
multiline | ||
` | ||
|
||
func setUpPFlagSet(buf io.Writer) *FlagSet { | ||
f := NewFlagSet("test", ExitOnError) | ||
f.Bool("long-form", false, "Some description") | ||
f.Bool("long-form2", false, "Some description\n with multiline") | ||
f.BoolP("long-name", "s", false, "Some description") | ||
f.BoolP("long-name2", "t", false, "Some description with\n multiline") | ||
f.SetOutput(buf) | ||
return f | ||
} | ||
|
||
func TestPrintUsage(t *testing.T) { | ||
buf := bytes.Buffer{} | ||
f := setUpPFlagSet(&buf) | ||
f.PrintDefaults() | ||
res := buf.String() | ||
if res != expectedOutput { | ||
t.Errorf("Expected \n%s \nActual \n%s", expectedOutput, res) | ||
} | ||
} | ||
|
||
func setUpPFlagSet2(buf io.Writer) *FlagSet { | ||
f := NewFlagSet("test", ExitOnError) | ||
f.Bool("long-form", false, "Some description") | ||
f.Bool("long-form2", false, "Some description\n with multiline") | ||
f.BoolP("long-name", "s", false, "Some description") | ||
f.BoolP("long-name2", "t", false, "Some description with\n multiline") | ||
f.StringP("some-very-long-arg", "l", "test", "Some very long description having break the limit") | ||
f.StringP("other-very-long-arg", "o", "long-default-value", "Some very long description having break the limit") | ||
f.String("some-very-long-arg2", "very long default value", "Some very long description\nwith line break\nmultiple") | ||
f.SetOutput(buf) | ||
return f | ||
} | ||
|
||
const expectedOutput2 = ` --long-form Some description | ||
--long-form2 Some description | ||
with multiline | ||
-s, --long-name Some description | ||
-t, --long-name2 Some description with | ||
multiline | ||
-o, --other-very-long-arg string Some very long description having | ||
break the limit (default | ||
"long-default-value") | ||
-l, --some-very-long-arg string Some very long description having | ||
break the limit (default "test") | ||
--some-very-long-arg2 string Some very long description | ||
with line break | ||
multiple (default "very long default | ||
value") | ||
` | ||
|
||
func TestPrintUsage_2(t *testing.T) { | ||
buf := bytes.Buffer{} | ||
f := setUpPFlagSet2(&buf) | ||
res := f.FlagUsagesWrapped(80) | ||
if res != expectedOutput2 { | ||
t.Errorf("Expected \n%q \nActual \n%q", expectedOutput2, res) | ||
} | ||
} |