ref: Convert all programs in ref to use package cmdline.

The exceptions are inithelper and suidhelper, since they are
meant to use a minimal amount of code for easy auditing.

The rpc/benchmark/main is also left alone, since it relies on a
specific init sequence, and it's just for our own internal
benchmarking anyways.

Changed global flags to flags on the root command for many
programs, to get better help messages.

Change-Id: I48d61327862506ca50f9b8d57818a01941b256f1
diff --git a/services/proxy/proxyd/doc.go b/services/proxy/proxyd/doc.go
new file mode 100644
index 0000000..7fd0915
--- /dev/null
+++ b/services/proxy/proxyd/doc.go
@@ -0,0 +1,77 @@
+// 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.
+
+// This file was auto-generated via go generate.
+// DO NOT UPDATE MANUALLY
+
+/*
+Command proxyd is a daemon that listens for connections from Vanadium services
+(typically behind NATs) and proxies these services to the outside world.
+
+Usage:
+   proxyd [flags]
+
+The proxyd flags are:
+ -access-list=
+   Blessings that are authorized to listen via the proxy.  JSON-encoded
+   representation of access.AccessList.  An empty string implies the default
+   authorization policy.
+ -healthz-address=
+   Network address on which the HTTP healthz server runs.  It is intended to be
+   used with a load balancer.  The load balancer must be able to reach this
+   address in order to verify that the proxy server is running.
+ -name=
+   Name to mount the proxy as.
+ -published-address=
+   DEPRECATED - the proxy now uses listenspecs and the address chooser
+   mechanism.
+
+The global flags are:
+ -alsologtostderr=true
+   log to standard error as well as files
+ -log_backtrace_at=:0
+   when logging hits line file:N, emit a stack trace
+ -log_dir=
+   if non-empty, write log files to this directory
+ -logtostderr=false
+   log to standard error instead of files
+ -max_stack_buf_size=4292608
+   max size in bytes of the buffer to use for logging stack traces
+ -stderrthreshold=2
+   logs at or above this threshold go to stderr
+ -v=0
+   log level for V logs
+ -v23.credentials=
+   directory to use for storing security credentials
+ -v23.i18n-catalogue=
+   18n catalogue files to load, comma separated
+ -v23.metadata=<just specify -v23.metadata to activate>
+   Displays metadata for the program and exits.
+ -v23.namespace.root=[/(dev.v.io/role/vprod/service/mounttabled)@ns.dev.v.io:8101]
+   local namespace root; can be repeated to provided multiple roots
+ -v23.permissions.file=map[]
+   specify a perms file as <name>:<permsfile>
+ -v23.permissions.literal=
+   explicitly specify the runtime perms as a JSON-encoded access.Permissions.
+   Overrides all --v23.permissions.file flags.
+ -v23.proxy=
+   object name of proxy service to use to export services across network
+   boundaries
+ -v23.tcp.address=
+   address to listen on
+ -v23.tcp.protocol=wsh
+   protocol to listen with
+ -v23.vtrace.cache-size=1024
+   The number of vtrace traces to store in memory.
+ -v23.vtrace.collect-regexp=
+   Spans and annotations that match this regular expression will trigger trace
+   collection.
+ -v23.vtrace.dump-on-shutdown=true
+   If true, dump all stored traces on runtime shutdown.
+ -v23.vtrace.sample-rate=0
+   Rate (from 0.0 to 1.0) to sample vtrace traces.
+ -vmodule=
+   comma-separated list of pattern=N settings for file-filtered logging
+*/
+package main
diff --git a/services/proxy/proxyd/main.go b/services/proxy/proxyd/main.go
index 920c7ad..2804ef6 100644
--- a/services/proxy/proxyd/main.go
+++ b/services/proxy/proxyd/main.go
@@ -2,51 +2,65 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// Daemon proxyd listens for connections from Vanadium services (typically
-// behind NATs) and proxies these services to the outside world.
+// The following enables go generate to generate the doc.go file.
+//go:generate go run $V23_ROOT/release/go/src/v.io/x/lib/cmdline/testdata/gendoc.go . -help
+
 package main
 
 import (
 	"bytes"
 	"encoding/json"
-	"flag"
 	"fmt"
 	"net/http"
 	"time"
 
 	"v.io/v23"
+	"v.io/v23/context"
 	"v.io/v23/rpc"
 	"v.io/v23/security"
 	"v.io/v23/security/access"
+	"v.io/x/lib/cmdline"
 	"v.io/x/lib/vlog"
-
 	"v.io/x/ref/lib/signals"
+	"v.io/x/ref/lib/v23cmd"
 	"v.io/x/ref/runtime/factories/static"
 )
 
-var (
-	pubAddress  = flag.String("published-address", "", "deprecated - the proxy now uses listenspecs and the address chooser mechanism")
-	healthzAddr = flag.String("healthz-address", "", "Network address on which the HTTP healthz server runs. It is intended to be used with a load balancer. The load balancer must be able to reach this address in order to verify that the proxy server is running")
-	name        = flag.String("name", "", "Name to mount the proxy as")
-	acl         = flag.String("access-list", "", "Blessings that are authorized to listen via the proxy. JSON-encoded representation of access.AccessList. An empty string implies the default authorization policy.")
-)
+var pubAddress, healthzAddr, name, acl string
 
 func main() {
-	ctx, shutdown := v23.Init()
-	defer shutdown()
+	cmdProxyD.Flags.StringVar(&pubAddress, "published-address", "", "DEPRECATED - the proxy now uses listenspecs and the address chooser mechanism.")
+	cmdProxyD.Flags.StringVar(&healthzAddr, "healthz-address", "", "Network address on which the HTTP healthz server runs.  It is intended to be used with a load balancer.  The load balancer must be able to reach this address in order to verify that the proxy server is running.")
+	cmdProxyD.Flags.StringVar(&name, "name", "", "Name to mount the proxy as.")
+	cmdProxyD.Flags.StringVar(&acl, "access-list", "", "Blessings that are authorized to listen via the proxy.  JSON-encoded representation of access.AccessList.  An empty string implies the default authorization policy.")
 
+	cmdline.HideGlobalFlagsExcept()
+	cmdline.Main(cmdProxyD)
+}
+
+var cmdProxyD = &cmdline.Command{
+	Runner: v23cmd.RunnerFunc(runProxyD),
+	Name:   "proxyd",
+	Short:  "Proxies services to the outside world",
+	Long: `
+Command proxyd is a daemon that listens for connections from Vanadium services
+(typically behind NATs) and proxies these services to the outside world.
+`,
+}
+
+func runProxyD(ctx *context.T, env *cmdline.Env, args []string) error {
 	listenSpec := v23.GetListenSpec(ctx)
 	if len(listenSpec.Addrs) != 1 {
-		vlog.Fatalf("proxyd can only listen on one address: %v", listenSpec.Addrs)
+		return env.UsageErrorf("proxyd can only listen on one address: %v", listenSpec.Addrs)
 	}
 	if listenSpec.Proxy != "" {
-		vlog.Fatalf("proxyd cannot listen through another proxy")
+		return env.UsageErrorf("proxyd cannot listen through another proxy")
 	}
 	var authorizer security.Authorizer
-	if len(*acl) > 0 {
+	if len(acl) > 0 {
 		var list access.AccessList
-		if err := json.NewDecoder(bytes.NewBufferString(*acl)).Decode(&list); err != nil {
-			vlog.Fatalf("invalid --access-list: %v", err)
+		if err := json.NewDecoder(bytes.NewBufferString(acl)).Decode(&list); err != nil {
+			return env.UsageErrorf("invalid -access-list: %v", err)
 		}
 		// Always add ourselves, for the the reserved methods server
 		// started below.
@@ -55,13 +69,13 @@
 		authorizer = list
 	}
 
-	proxyShutdown, proxyEndpoint, err := static.NewProxy(ctx, listenSpec, authorizer, *name)
+	proxyShutdown, proxyEndpoint, err := static.NewProxy(ctx, listenSpec, authorizer, name)
 	if err != nil {
-		vlog.Fatal(err)
+		return err
 	}
 	defer proxyShutdown()
 
-	if len(*name) > 0 {
+	if len(name) > 0 {
 		// Print out a directly accessible name for the proxy table so
 		// that integration tests can reliably read it from stdout.
 		fmt.Printf("NAME=%s\n", proxyEndpoint.Name())
@@ -69,30 +83,31 @@
 		fmt.Printf("Proxy listening on %s\n", proxyEndpoint)
 	}
 
-	if len(*healthzAddr) != 0 {
-		go startHealthzServer(*healthzAddr)
+	if len(healthzAddr) != 0 {
+		go startHealthzServer(healthzAddr)
 	}
 
 	// Start an RPC Server that listens through the proxy itself. This
 	// server will serve reserved methods only.
 	server, err := v23.NewServer(ctx)
 	if err != nil {
-		vlog.Fatalf("NewServer failed: %v", err)
+		return fmt.Errorf("NewServer failed: %v", err)
 	}
 	defer server.Stop()
 	ls := rpc.ListenSpec{Proxy: proxyEndpoint.Name()}
 	if _, err := server.Listen(ls); err != nil {
-		vlog.Fatalf("Listen(%v) failed: %v", ls, err)
+		return fmt.Errorf("Listen(%v) failed: %v", ls, err)
 	}
 	var monitoringName string
-	if len(*name) > 0 {
-		monitoringName = *name + "-mon"
+	if len(name) > 0 {
+		monitoringName = name + "-mon"
 	}
 	if err := server.ServeDispatcher(monitoringName, &nilDispatcher{}); err != nil {
-		vlog.Fatalf("ServeDispatcher(%v) failed: %v", monitoringName, err)
+		return fmt.Errorf("ServeDispatcher(%v) failed: %v", monitoringName, err)
 	}
 
 	<-signals.ShutdownOnSignals(ctx)
+	return nil
 }
 
 type nilDispatcher struct{}