~mendelmaleh/opt

c1802d2aa6794b22cd9772149439769951b90671 — Mendel E 6 months ago 6559cdb
Add comments, remove Opt struct type
1 files changed, 7 insertions(+), 5 deletions(-)

M opt.go
M opt.go => opt.go +7 -5
@@ 1,15 1,14 @@
package opt

// Opts is a map type, where options and their arguments are stored
type Opts map[rune]string
type Opt struct {
	Name  rune
	Value string
}

// New is a constructor for Opts
func New() Opts {
	return make(Opts)
}

// Parse parses the given argv, returns the extra args and stores the options and their respective args in opts
func (opts Opts) Parse(argv []string, _usage string) (args []string, err error) {
	usage := []rune(_usage)
	// errChar := '?'


@@ 19,6 18,7 @@ func (opts Opts) Parse(argv []string, _usage string) (args []string, err error) 
	// 	usage = usage[1:]
	// }

	// this map is for checking which options expect arguments
	optmap := make(map[rune]bool)
	for i := 0; i < len(usage); i++ {
		if i+1 < len(usage) && usage[i+1] == ':' {


@@ 44,11 44,13 @@ func (opts Opts) Parse(argv []string, _usage string) (args []string, err error) 
		arg = arg[1:]
		for j := 0; j < len(arg); j++ {
			opt := arg[j]
			// if opt not in optmap
			if _, ok := optmap[opt]; !ok {
				// todo: opt not in map
				// todo
				continue
			}

			// if opt doesn't expect an arg
			if !optmap[opt] {
				opts[opt] = ""
				continue