-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add flagx.Advanced variable and first advanced flag -httpx.tcp-network (
#123) * Add flagx.Advanced flag set * Add httpx advanced flag to select tcp network
- Loading branch information
1 parent
3abc23e
commit fac1167
Showing
3 changed files
with
83 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package flagx | ||
|
||
import "flag" | ||
|
||
var ( | ||
// Advanced is a *flag.FlagSet for advanced flags. Packages should add flags | ||
// to Advanced when those flags should NOT be included in the default | ||
// flag.CommandLine flag set. Advanced flags may be enabled by calling | ||
// EnableAdvancedFlags _before_ calling flag.Parse(). | ||
Advanced = flag.NewFlagSet("advanced", flag.ExitOnError) | ||
) | ||
|
||
// EnableAdvancedFlags adds all flags registered with the Advanced flag set to | ||
// the default flag.CommandLine flag set. EnableAdvancedFlags should be called | ||
// before flag.Parse(). | ||
func EnableAdvancedFlags() { | ||
Advanced.VisitAll(func(f *flag.Flag) { | ||
flag.Var(f.Value, f.Name, f.Usage) | ||
}) | ||
} |
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,47 @@ | ||
package flagx | ||
|
||
import ( | ||
"flag" | ||
"testing" | ||
|
||
"github.com/go-test/deep" | ||
) | ||
|
||
func TestEnableAdvancedFlags(t *testing.T) { | ||
t.Run("example", func(t *testing.T) { | ||
// Add advanced flags. | ||
e := Enum{ | ||
Options: []string{"a", "b"}, | ||
Value: "b", | ||
} | ||
s := "advanced" | ||
Advanced.Var(&e, "advanced-enum-flag", "advanced-usage") | ||
Advanced.StringVar(&s, "advanced-string-flag", "", "advanced-usage") | ||
|
||
// Add default flags. | ||
// Reset the default command line flag to simplilfy checking tests. | ||
flag.CommandLine = flag.NewFlagSet("default", flag.ContinueOnError) | ||
s2 := "default" | ||
flag.StringVar(&s2, "default-flag", "", "default-usage") | ||
|
||
// Add advanced flags to default set. | ||
EnableAdvancedFlags() | ||
|
||
found := map[string]bool{} | ||
expected := map[string]bool{ | ||
"advanced-enum-flag": true, | ||
"advanced-string-flag": true, | ||
"default-flag": true, | ||
} | ||
// Verify that they are present in the default set now. | ||
flag.CommandLine.VisitAll( | ||
func(f *flag.Flag) { | ||
found[f.Name] = true | ||
t.Logf("Found: %q", f.Name) | ||
}, | ||
) | ||
if diff := deep.Equal(found, expected); diff != nil { | ||
t.Errorf("EnableAdvancedFlags() found the wrong advanced flags: %#v", diff) | ||
} | ||
}) | ||
} |
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