ref: Convert all cmdline users to cmdline2.

This also includes changes in cmdline2 to allow commands with
both Children and Runner set, as long as the runner doesn't take
any args.  This makes sense since the runner handles the no-arg
case, while the children handle the with-args case.  This was
already being used by "v23 buildcop" and "deviced", and it seemed
better to allow this behavior.

Also fixed a bug in the cmdline2 package where the "at least one
of Children and Runner" check wasn't returning an error, if the
bad Command wasn't the root.  Added tests for this case.

Finally, but also important, changed how global flags are
handled.  Previously I was hoping we could simply change
"v.io/x/ref/lib/flags" to check flag.Parsed and avoid
double-parsing the flags.  But the previous strategy always
merged flags into a new FlagSet, and then overwrote
flag.CommandLine with that value.  Our usage of ref/lib/flags
stashes the flag.CommandLine pointer away in an init function,
which breaks that strategy.

In addition, ref/lib/flags has some weird logic to set certain
flags to their "default values".  Presumably this was a hack to
deal with double-parsing the flags for flags that are specified
multiple times on the command line, and append their values.
This makes it questionable whether we can really depend on not
double-parsing flags.

I'll look into changing our flags so that they don't depend on
the multi-specification-append behavior, since that's just weird.
But I'll do that separately.

The actual conversions of all programs is mechanical.

MultiPart: 3/3

Change-Id: I3292256e5c8e4a6acf99c9b92c98b009a7fbfcf4
diff --git a/cmd/uniqueid/doc.go b/cmd/uniqueid/doc.go
index ce5859d..08b1405 100644
--- a/cmd/uniqueid/doc.go
+++ b/cmd/uniqueid/doc.go
@@ -18,51 +18,8 @@
    help        Display help for commands or topics
 
 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
 
 Uniqueid generate
 
@@ -89,11 +46,6 @@
 
 "help ..." recursively displays help for all commands and topics.
 
-Output is formatted to a target width in runes, determined by checking the
-CMDLINE_WIDTH environment variable, falling back on the terminal width, falling
-back on 80 chars.  By setting CMDLINE_WIDTH=x, if x > 0 the width is x, if x < 0
-the width is unlimited, and if x == 0 or is unset one of the fallbacks is used.
-
 Usage:
    uniqueid help [flags] [command/topic ...]
 
@@ -106,5 +58,9 @@
       full    - Good for cmdline output, shows all global flags.
       godoc   - Good for godoc processing.
    Override the default by setting the CMDLINE_STYLE environment variable.
+ -width=<terminal width>
+   Format output to this target width in runes, or unlimited if width < 0.
+   Defaults to the terminal width if available.  Override the default by setting
+   the CMDLINE_WIDTH environment variable.
 */
 package main
diff --git a/cmd/uniqueid/main.go b/cmd/uniqueid/main.go
index 9cd2737..3d27018 100644
--- a/cmd/uniqueid/main.go
+++ b/cmd/uniqueid/main.go
@@ -11,43 +11,31 @@
 	"bytes"
 	"fmt"
 	"io/ioutil"
-	"os"
 	"regexp"
 
-	"v.io/v23"
 	"v.io/v23/uniqueid"
-	"v.io/x/lib/cmdline"
-	_ "v.io/x/ref/profiles/static"
+	"v.io/x/lib/cmdline2"
 )
 
 func main() {
-	cmdline.HideGlobalFlagsExcept()
-	os.Exit(cmdUniqueId.Main())
+	cmdline2.Main(cmdUniqueId)
 }
 
-func runHelper(run cmdline.Runner) cmdline.Runner {
-	return func(cmd *cmdline.Command, args []string) error {
-		_, shutdown := v23.Init()
-		defer shutdown()
-		return run(cmd, args)
-	}
-}
-
-var cmdUniqueId = &cmdline.Command{
+var cmdUniqueId = &cmdline2.Command{
 	Name:  "uniqueid",
 	Short: "generates unique identifiers",
 	Long: `
 Command uniqueid generates unique identifiers.
 It also has an option of automatically substituting unique ids with placeholders in files.
 `,
-	Children: []*cmdline.Command{cmdGenerate, cmdInject},
-	Topics:   []cmdline.Topic{},
+	Children: []*cmdline2.Command{cmdGenerate, cmdInject},
+	Topics:   []cmdline2.Topic{},
 }
 
-var cmdGenerate = &cmdline.Command{
-	Run:   runHelper(runGenerate),
-	Name:  "generate",
-	Short: "Generates UniqueIds",
+var cmdGenerate = &cmdline2.Command{
+	Runner: cmdline2.RunnerFunc(runGenerate),
+	Name:   "generate",
+	Short:  "Generates UniqueIds",
 	Long: `
 Generates unique ids and outputs them to standard out.
 `,
@@ -55,10 +43,10 @@
 	ArgsLong: "",
 }
 
-var cmdInject = &cmdline.Command{
-	Run:   runHelper(runInject),
-	Name:  "inject",
-	Short: "Injects UniqueIds into existing files",
+var cmdInject = &cmdline2.Command{
+	Runner: cmdline2.RunnerFunc(runInject),
+	Name:   "inject",
+	Short:  "Injects UniqueIds into existing files",
 	Long: `
 Injects UniqueIds into existing files.
 Strings of the form "$UNIQUEID$" will be replaced with generated ids.
@@ -68,9 +56,9 @@
 }
 
 // runGenerate implements the generate command which outputs generated ids to stdout.
-func runGenerate(command *cmdline.Command, args []string) error {
+func runGenerate(env *cmdline2.Env, args []string) error {
 	if len(args) > 0 {
-		return command.UsageErrorf("expected 0 args, got %d", len(args))
+		return env.UsageErrorf("expected 0 args, got %d", len(args))
 	}
 	id, err := uniqueid.Random()
 	if err != nil {
@@ -81,9 +69,9 @@
 }
 
 // runInject implements the inject command which replaces $UNIQUEID$ strings with generated ids.
-func runInject(command *cmdline.Command, args []string) error {
+func runInject(env *cmdline2.Env, args []string) error {
 	if len(args) == 0 {
-		return command.UsageErrorf("expected at least one file arg, got 0")
+		return env.UsageErrorf("expected at least one file arg, got 0")
 	}
 	for _, arg := range args {
 		if err := injectIntoFile(arg); err != nil {