Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix so "-v-" is not incorrectly parsed as "-v" and "--", #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion getopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (optionsDefinition Options) parseCommandLineImpl(args []string, environment
currentOption, _ := optionsDefinition.FindOption(opt)
key := currentOption.Key()

options[key], err = assignValue(currentOption.DefaultValue, "true")
options[key], err = assignValue(true, "true")

// make it look as if we have a normal option with a '-' prefix
buffer = "-" + buffer[2:]
Expand All @@ -134,6 +134,11 @@ func (optionsDefinition Options) parseCommandLineImpl(args []string, environment
opt, val, found = parseLongOpt(token)
}

if !found {
err = &GetOptError{InvalidOption, "invalid option '" + token + "'"}
break
}

currentOption, found := optionsDefinition.FindOption(opt)
key := currentOption.Key()

Expand Down
43 changes: 43 additions & 0 deletions getopt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,49 @@ func TestShortOptionsFlagsParsing(t *testing.T) {

}

func TestLongOptionsFlagsParsing(t *testing.T) {
options := Options{
"",
Definitions{
{"verbose|v", "verbose mode", Flag, ""},
{"debug", "very very verbose mode", Flag, ""},
},
}

os.Args = []string{"prog", "--verbose"}
if opts, _, _, e := options.ParseCommandLine(); e == nil {
if opts["verbose"].Bool != true {
t.Errorf("verbose flag was not set for --verbose")
}
if opts["debug"].Bool != false {
t.Errorf("debug flag was set for --verbose")
}
} else {
t.Errorf("error was set for --verbose")
}

os.Args = []string{"prog", "--error"}
if _, _, _, e := options.ParseCommandLine(); e == nil {
t.Errorf("error was not set for --error")
}

os.Args = []string{"prog", "-v-"} // bug?
if opts, _, _, e := options.ParseCommandLine(); e == nil {
t.Errorf("error was not set for -v-")
if opts["verbose"].Bool != true {
t.Errorf("verbose flag was not set for -v-")
}
if opts["debug"].Bool != false {
t.Errorf("debug flag was set for -v-")
}
}

os.Args = []string{"prog", "-v-debug"} // bug?
if _, _, _, e := options.ParseCommandLine(); e == nil {
t.Errorf("error was not set for -v-debug")
}
}

func TestShortOptionRequiredParsing(t *testing.T) {
options := Options{"", Definitions{{"method|m|MON_METHOD", "method: one of either 'heartbeat' or 'nagios'", Required, ""}}}

Expand Down
2 changes: 1 addition & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func checkOptionsDefinitionConsistency(optionsDefinition Options) (err *GetOptEr

func (options Options) FindOption(optionString string) (option Option, found bool) {
for _, cur := range options.Definitions {
if cur.ShortOpt() == optionString || cur.LongOpt() == optionString {
if (cur.HasShortOpt() && cur.ShortOpt() == optionString) || (cur.HasLongOpt() && cur.LongOpt() == optionString) {
option = cur
found = true
break
Expand Down