Anonymous avatar Anonymous committed 21010a0

Adds support for default values

Comments (0)

Files changed (2)

 	allow_unknown_args bool
 
 	options     map[string]string
+	defaults    map[string]string
 	flags       map[string]bool
 	required    map[string]bool
 	environment map[string]string
 }
 
 type Options struct {
-	options map[string]string
-	Command string
-	Args    []string
+	options   map[string]string
+	defaults  map[string]string
+	Command   string
+	Args      []string
 }
 
 // MustParse() is a wrapper for Parse() for assigning global variables.
 func Parse(desc string) (spec *Spec, err error) {
 	spec = new(Spec)
 	spec.options = make(map[string]string, 0)
+	spec.defaults = make(map[string]string, 0)
 	spec.flags = make(map[string]bool, 0)
 	spec.required = make(map[string]bool, 0)
 	spec.commands = make(map[string]string, 0)
 				required = true
 			}
 
-			if strings.HasSuffix(option, "=") {
-				option = option[0 : len(option)-1]
+			if strings.Contains(option, "=") {
+				ks := strings.Split(option, "=")
+				option = ks[0]
+				spec.defaults[option] = ks[1]
 				flag = false
 			}
 
 			parts = strings.Split(parts[0], ",")
 
 			for _, part := range parts {
-				part = strings.SplitN(part, "=", 2)[0]
+				pieces := strings.SplitN(part, "=", 2)
+				part = pieces[0]
 
-				if strings.HasPrefix(part, "--") {
-					spec.options[part] = option
-					continue
-				}
-
-				if strings.HasPrefix(part, "-") {
+				if strings.HasPrefix(part, "--") || strings.HasPrefix(part, "-") {
 					spec.options[part] = option
 					continue
 				}
 func (spec *Spec) Interpret(args []string, environ []string) (o *Options, err error) {
 	opts := new(Options)
 	opts.options = make(map[string]string, 0)
+	opts.defaults = spec.defaults
 	opts.Args = []string{}
 
 	for _, env := range environ {
 	if value, present := opts.options[option]; present {
 		return value
 	}
-	return ""
+	return opts.defaults[option]
 }
 
 func (opts *Options) GetBool(option string) bool {
 }
 
 func (opts *Options) GetInt(option string) int {
-	i, err := strconv.Atoi(opts.Get(option))
-	if err != nil {
-		i = 0
-	}
+	i, _ := strconv.Atoi(opts.Get(option))
 	return i
 }
 	// Output:
 	// required: hello world
 }
+
+
+func TestDefaults(t *testing.T) {
+	spec, err := Parse(`
+    usage: haraway <flags>... <command> <args>...
+    --
+    root=XYZ  -r,--root=,HARAWAY_ROOT     Path to the haraway data root
+    num=2     -n=                         Path to the haraway install prefix.
+    --
+    --
+    exec      c,exec                      Execute a command within the haraway sanbox
+    shell     sh,shell                    Open a shell within the haraway sanbox
+    --
+    `)
+	if err != nil {
+		t.Error(err)
+	}
+
+	opts, err := spec.Interpret([]string{"haraway"}, []string{})
+
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if opts.Get("root") != "XYZ" {
+		t.Error("--root != XYZ")
+	}
+	if opts.GetInt("num") != 2 {
+		t.Error("--num != 2")
+	}
+
+
+	opts, err = spec.Interpret([]string{"haraway", "-r", "hello", "-n=5"}, []string{})
+
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if opts.Get("root") != "hello" {
+		t.Error("--root != hello")
+	}
+	if opts.GetInt("num") != 5 {
+		t.Error("-n != 5")
+	}
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.