Skip to content

Commit

Permalink
Merge pull request #94 from projectdiscovery/dev
Browse files Browse the repository at this point in the history
v0.1.3
  • Loading branch information
ehsandeep authored Nov 1, 2022
2 parents c123b8b + 30955f2 commit e837d49
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
31 changes: 31 additions & 0 deletions examples/basic/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"log"

"github.com/projectdiscovery/goflags"
)

type Options struct {
name string
Email goflags.StringSlice
Phone string
Address goflags.StringSlice
}

func main() {
testOptions := &Options{}
flagSet := goflags.NewFlagSet()
flagSet.CreateGroup("info", "Info",
flagSet.StringVarP(&testOptions.name, "name", "n", "", "name of the user"),
flagSet.StringSliceVarP(&testOptions.Email, "email", "e", nil, "email of the user", goflags.CommaSeparatedStringSliceOptions),
)
flagSet.CreateGroup("additional", "Additional",
flagSet.StringVarP(&testOptions.Phone, "phone", "ph", "", "phone of the user"),
flagSet.StringSliceVarP(&testOptions.Address, "address", "add", nil, "address of the user", goflags.StringSliceOptions),
)

if err := flagSet.Parse(); err != nil {
log.Fatal(err)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/projectdiscovery/goflags

go 1.17
go 1.18

require (
github.com/cnf/structhash v0.0.0-20201127153200-e1b16c1ebc08
Expand Down
28 changes: 26 additions & 2 deletions goflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -106,7 +105,7 @@ func (flagSet *FlagSet) Parse() error {
_ = os.MkdirAll(filepath.Dir(configFilePath), os.ModePerm)
if !fileutil.FileExists(configFilePath) {
configData := flagSet.generateDefaultConfig()
return ioutil.WriteFile(configFilePath, configData, os.ModePerm)
return os.WriteFile(configFilePath, configData, os.ModePerm)
}
_ = flagSet.MergeConfigFile(configFilePath) // try to read default config after parsing flags
return nil
Expand Down Expand Up @@ -530,6 +529,11 @@ func (flagSet *FlagSet) usageFunc() {
flagSet.displayGroupUsageFunc(newUniqueDeduper(), group, cliOutput, writer)
return
}
flag := flagSet.getFlagByName(strings.ToLower(os.Args[2]))
if flag != nil {
flagSet.displaySingleFlagUsageFunc(os.Args[2], flag, cliOutput, writer)
return
}
}

if len(flagSet.groups) > 0 {
Expand All @@ -548,6 +552,17 @@ func (flagSet *FlagSet) getGroupbyName(name string) groupData {
return groupData{}
}

func (flagSet *FlagSet) getFlagByName(name string) *FlagData {
var flagData *FlagData
flagSet.flagKeys.forEach(func(key string, data *FlagData) {
if strings.EqualFold(data.long, name) || strings.EqualFold(data.short, name) {
flagData = data
return
}
})
return flagData
}

// usageFuncInternal prints usage for command line flags
func (flagSet *FlagSet) usageFuncInternal(writer *tabwriter.Writer) {
uniqueDeduper := newUniqueDeduper()
Expand Down Expand Up @@ -614,6 +629,15 @@ func (flagSet *FlagSet) displayGroupUsageFunc(uniqueDeduper *uniqueDeduper, grou
return otherOptions
}

// displaySingleFlagUsageFunc displays usage for a single flag
func (flagSet *FlagSet) displaySingleFlagUsageFunc(name string, data *FlagData, cliOutput io.Writer, writer *tabwriter.Writer) {
if currentFlag := flagSet.CommandLine.Lookup(name); currentFlag != nil {
result := createUsageString(data, currentFlag)
fmt.Fprint(writer, result, "\n")
writer.Flush()
}
}

type uniqueDeduper struct {
hashes map[string]interface{}
}
Expand Down
7 changes: 3 additions & 4 deletions goflags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package goflags
import (
"bytes"
"flag"
"io/ioutil"
"os"
"reflect"
"strconv"
Expand Down Expand Up @@ -63,7 +62,7 @@ severity:
int-value: 543
bool-value: true
duration-value: 1h`
err := ioutil.WriteFile("test.yaml", []byte(configFileData), os.ModePerm)
err := os.WriteFile("test.yaml", []byte(configFileData), os.ModePerm)
require.Nil(t, err, "could not write temporary config")
defer os.Remove("test.yaml")

Expand Down Expand Up @@ -277,7 +276,7 @@ func TestParseFileCommaSeparatedStringSlice(t *testing.T) {
testFileData := `value1
Value2 "
value3`
err := ioutil.WriteFile(testFile, []byte(testFileData), os.ModePerm)
err := os.WriteFile(testFile, []byte(testFileData), os.ModePerm)
require.Nil(t, err, "could not write temporary values file")
defer os.Remove(testFile)

Expand Down Expand Up @@ -306,7 +305,7 @@ config-only:
- test
- test2
`
err := ioutil.WriteFile("test.yaml", []byte(configFileData), os.ModePerm)
err := os.WriteFile("test.yaml", []byte(configFileData), os.ModePerm)
require.Nil(t, err, "could not write temporary config")
defer os.Remove("test.yaml")

Expand Down

0 comments on commit e837d49

Please sign in to comment.