Repository: incubator-htrace
Updated Branches:
  refs/heads/master 4a19eb444 -> 3efeab270


HTRACE-97. Support both -D and --D when specifying conf vars on the 
command-line (cmccabe)


Project: http://git-wip-us.apache.org/repos/asf/incubator-htrace/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-htrace/commit/3efeab27
Tree: http://git-wip-us.apache.org/repos/asf/incubator-htrace/tree/3efeab27
Diff: http://git-wip-us.apache.org/repos/asf/incubator-htrace/diff/3efeab27

Branch: refs/heads/master
Commit: 3efeab270198b265fe49e084b7bb766feb5e98dc
Parents: 4a19eb4
Author: Colin P. Mccabe <[email protected]>
Authored: Tue Feb 3 20:28:03 2015 -0800
Committer: Colin P. Mccabe <[email protected]>
Committed: Tue Feb 3 20:28:03 2015 -0800

----------------------------------------------------------------------
 .../src/go/src/org/apache/htrace/conf/config.go | 29 ++++++++++++++++----
 1 file changed, 23 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/3efeab27/htrace-core/src/go/src/org/apache/htrace/conf/config.go
----------------------------------------------------------------------
diff --git a/htrace-core/src/go/src/org/apache/htrace/conf/config.go 
b/htrace-core/src/go/src/org/apache/htrace/conf/config.go
index 4453c44..f9784c5 100644
--- a/htrace-core/src/go/src/org/apache/htrace/conf/config.go
+++ b/htrace-core/src/go/src/org/apache/htrace/conf/config.go
@@ -115,6 +115,26 @@ func openFile(cnfName string, paths []string) 
(io.ReadCloser, error) {
        return nil, nil
 }
 
+// Try to parse a command-line element as a key=value pair.
+func parseAsConfigFlag(flag string) (string, string) {
+       var confPart string
+       if strings.HasPrefix(flag, "-D") {
+               confPart = flag[2:]
+       } else if strings.HasPrefix(flag, "--D") {
+               confPart = flag[3:]
+       } else {
+               return "", ""
+       }
+       if len(confPart) == 0 {
+               return "", ""
+       }
+       idx := strings.Index(confPart, "=")
+       if idx == -1 {
+               return confPart, "true"
+       }
+       return confPart[0:idx], confPart[idx+1:]
+}
+
 // Build a new configuration object from the provided conf.Builder.
 func (bld *Builder) Build() (*Config, error) {
        // Load values and defaults
@@ -141,14 +161,11 @@ func (bld *Builder) Build() (*Config, error) {
        var i int
        for i < len(bld.Argv) {
                str := bld.Argv[i]
-               if strings.HasPrefix(str, "-D") {
-                       idx := strings.Index(str, "=")
-                       if idx == -1 {
-                               key := str[2:]
+               key, val := parseAsConfigFlag(str)
+               if key != "" {
+                       if val == "" {
                                cnf.settings[key] = "true"
                        } else {
-                               key := str[2:idx]
-                               val := str[idx+1:]
                                cnf.settings[key] = val
                        }
                        bld.Argv = append(bld.Argv[:i], bld.Argv[i+1:]...)

Reply via email to