~mendelmaleh/opt

1ea2994d8c5d81fe8be7aa6386191ed98a649484 — Mendel E 5 months ago c1802d2
Catch case where optarg has no arg
2 files changed, 18 insertions(+), 1 deletions(-)

M opt.go
M opt_test.go
M opt.go => opt.go +7 -1
@@ 56,12 56,18 @@ func (opts Opts) Parse(argv []string, _usage string) (args []string, err error) 
				continue
			}

			// if opt expects an arg:
			// if opt is not the end of this arg, use
			// the rest of it, otherwise use the next arg.
			// if there is no next arg, set the arg to an empty string.
			if j+1 < len(arg) {
				opts[opt] = string(arg[j+1:])
				break
			} else {
			} else if i+1 < len(argv) {
				i++
				opts[opt] = argv[i]
			} else {
				opts[opt] = ""
			}
		}
	}

M opt_test.go => opt_test.go +11 -0
@@ 61,3 61,14 @@ func TestOptsArgsAttached(t *testing.T) {
	assert.Equal(opts['b'], "")
	assert.Equal(opts['l'], "mno")
}

func TestEmptyOptsWithArgs(t *testing.T) {
	assert := assert.New(t)
	opts := New()
	args, err := opts.Parse([]string{
		"-ab"}, "ab:")
	assert.Nil(err)
	assert.Equal(len(opts), 2)
	assert.Equal(len(args), 0)
	assert.Equal(opts['b'], "")
}