Merge "ref: Rename ListenFlags.ListenProxy to ListenFlags.Proxy."
diff --git a/runtime/internal/mojo_util.go b/runtime/internal/mojo_util.go
deleted file mode 100644
index 308ba01..0000000
--- a/runtime/internal/mojo_util.go
+++ /dev/null
@@ -1,39 +0,0 @@
-// Copyright 2015 The Vanadium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-// +build mojo
-
-package internal
-
-import (
-	"flag"
-	"log"
-	"strings"
-
-	"v.io/x/ref/lib/flags"
-)
-
-// NOTE(nlacasse): This variable must be set at build time by passing the
-// "-ldflags" flag to "go build" like so:
-// go build -ldflags "-X v.io/x/ref/runtime/internal.commandLineFlags '--flag1=foo --flag2=bar'"
-var commandLineFlags string
-
-// TODO(sadovsky): Terrible, terrible hack.
-func parseFlagsInternal(f *flags.Flags, config map[string]string) error {
-	// We expect that command-line flags have not been parsed. v23_util
-	// performs command-line parsing at this point. For Mojo, we instead parse
-	// command-line flags from the commandLineFlags variable set at build time.
-	// TODO(sadovsky): Maybe move this check to util.go, or drop it?
-	if flag.CommandLine.Parsed() {
-		panic("flag.CommandLine.Parse() has been called")
-	}
-
-	// NOTE(nlacasse): Don't use vlog here, since vlog output depends on
-	// command line flags which have not been parsed yet.
-	log.Printf("Parsing flags: %v\n", commandLineFlags)
-
-	// TODO(sadovsky): Support argument quoting. More generally, parse this env
-	// var similar to how bash parses arguments.
-	return f.Parse(strings.Split(commandLineFlags, " "), config)
-}
diff --git a/runtime/internal/util.go b/runtime/internal/util.go
index f26022c..d645f1e 100644
--- a/runtime/internal/util.go
+++ b/runtime/internal/util.go
@@ -7,6 +7,7 @@
 import (
 	"fmt"
 	"net"
+	"os"
 	"strings"
 
 	"v.io/x/lib/netstate"
@@ -40,7 +41,7 @@
 	if handle != nil {
 		config = handle.Config.Dump()
 	}
-	return parseFlagsInternal(f, config)
+	return f.Parse(os.Args[1:], config)
 }
 
 // ParseFlagsAndConfigurGlobalLogger calls ParseFlags and then
diff --git a/runtime/internal/v23_util.go b/runtime/internal/v23_util.go
deleted file mode 100644
index 5375851..0000000
--- a/runtime/internal/v23_util.go
+++ /dev/null
@@ -1,17 +0,0 @@
-// Copyright 2015 The Vanadium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style
-// license that can be found in the LICENSE file.
-
-// +build !mojo
-
-package internal
-
-import (
-	"os"
-
-	"v.io/x/ref/lib/flags"
-)
-
-func parseFlagsInternal(f *flags.Flags, config map[string]string) error {
-	return f.Parse(os.Args[1:], config)
-}
diff --git a/services/syncbase/syncbased/mojo_main.go b/services/syncbase/syncbased/mojo_main.go
index a37acca..d7f6477 100644
--- a/services/syncbase/syncbased/mojo_main.go
+++ b/services/syncbase/syncbased/mojo_main.go
@@ -12,6 +12,7 @@
 
 import (
 	"log"
+	"os"
 
 	"mojo/public/go/application"
 	"mojo/public/go/bindings"
@@ -29,13 +30,22 @@
 import "C"
 
 type delegate struct {
-	ctx   *context.T
-	srv   rpc.Server
-	disp  rpc.Dispatcher
-	stubs []*bindings.Stub
+	ctx      *context.T
+	disp     rpc.Dispatcher
+	shutdown func()
+	srv      rpc.Server
+	stubs    []*bindings.Stub
 }
 
-func (d *delegate) Initialize(ctx application.Context) {
+func (d *delegate) Initialize(actx application.Context) {
+	// actx.Args() is a slice that contains the url of this mojo service
+	// followed by all arguments passed to the mojo service via the
+	// "--args-for" flag.
+	// Since the v23 runtime factories parse arguments from os.Args, we must
+	// overwrite os.Args with actx.Args().
+	// Note that os.Args must be set before calling v23.Init().
+	os.Args = actx.Args()
+	d.ctx, d.shutdown = v23.Init()
 	d.srv, d.disp = Serve(d.ctx)
 }
 
@@ -64,13 +74,12 @@
 	for _, stub := range d.stubs {
 		stub.Close()
 	}
+	d.shutdown()
 }
 
 //export MojoMain
 func MojoMain(handle C.MojoHandle) C.MojoResult {
-	ctx, shutdown := v23.Init()
-	defer shutdown()
-	application.Run(&delegate{ctx: ctx}, system.MojoHandle(handle))
+	application.Run(&delegate{}, system.MojoHandle(handle))
 	return C.MOJO_RESULT_OK
 }