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/vom/doc.go b/cmd/vom/doc.go
index 09aebc3..e34f185 100644
--- a/cmd/vom/doc.go
+++ b/cmd/vom/doc.go
@@ -17,51 +17,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
Vom decode
@@ -119,11 +76,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:
vom help [flags] [command/topic ...]
@@ -136,5 +88,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/vom/vom.go b/cmd/vom/vom.go
index 1c7398f..f387762 100644
--- a/cmd/vom/vom.go
+++ b/cmd/vom/vom.go
@@ -17,39 +17,28 @@
"strings"
"unicode"
- "v.io/v23"
"v.io/v23/vdl"
"v.io/v23/vom"
- "v.io/x/lib/cmdline"
- _ "v.io/x/ref/profiles/static"
+ "v.io/x/lib/cmdline2"
)
func main() {
- cmdline.HideGlobalFlagsExcept()
- os.Exit(cmdVom.Main())
+ cmdline2.Main(cmdVom)
}
-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 cmdVom = &cmdline.Command{
+var cmdVom = &cmdline2.Command{
Name: "vom",
Short: "helps debug the Vanadium Object Marshaling wire protocol",
Long: `
Command vom helps debug the Vanadium Object Marshaling wire protocol.
`,
- Children: []*cmdline.Command{cmdDecode, cmdDump},
+ Children: []*cmdline2.Command{cmdDecode, cmdDump},
}
-var cmdDecode = &cmdline.Command{
- Run: runHelper(runDecode),
- Name: "decode",
- Short: "Decode data encoded in the vom format",
+var cmdDecode = &cmdline2.Command{
+ Runner: cmdline2.RunnerFunc(runDecode),
+ Name: "decode",
+ Short: "Decode data encoded in the vom format",
Long: `
Decode decodes data encoded in the vom format. If no arguments are provided,
decode reads the data from stdin, otherwise the argument is the data.
@@ -63,10 +52,10 @@
ArgsLong: "[data] is the data to decode; if not specified, reads from stdin",
}
-var cmdDump = &cmdline.Command{
- Run: runHelper(runDump),
- Name: "dump",
- Short: "Dump data encoded in the vom format into formatted output",
+var cmdDump = &cmdline2.Command{
+ Runner: cmdline2.RunnerFunc(runDump),
+ Name: "dump",
+ Short: "Dump data encoded in the vom format into formatted output",
Long: `
Dump dumps data encoded in the vom format, generating formatted output
describing each portion of the encoding. If no arguments are provided, dump
@@ -102,12 +91,12 @@
"Data representation, one of "+fmt.Sprint(dataRepAll))
}
-func runDecode(cmd *cmdline.Command, args []string) error {
+func runDecode(env *cmdline2.Env, args []string) error {
// Convert all inputs into a reader over binary bytes.
var data string
switch {
case len(args) > 1:
- return cmd.UsageErrorf("too many args")
+ return env.UsageErrorf("too many args")
case len(args) == 1:
data = args[0]
default:
@@ -129,29 +118,29 @@
if err := decoder.Decode(&result); err != nil {
return err
}
- fmt.Fprintln(cmd.Stdout(), result)
+ fmt.Fprintln(env.Stdout, result)
if reader.Len() != 0 {
return fmt.Errorf("%d leftover bytes: % x", reader.Len(), reader.String())
}
return nil
}
-func runDump(cmd *cmdline.Command, args []string) error {
+func runDump(env *cmdline2.Env, args []string) error {
// Handle non-streaming cases.
switch {
case len(args) > 1:
- return cmd.UsageErrorf("too many args")
+ return env.UsageErrorf("too many args")
case len(args) == 1:
binbytes, err := dataToBinaryBytes(args[0])
if err != nil {
return err
}
- fmt.Fprintln(cmd.Stdout(), vom.Dump(binbytes))
+ fmt.Fprintln(env.Stdout, vom.Dump(binbytes))
return nil
}
// Handle streaming from stdin.
// TODO(toddw): Add a flag to configure stdout/stderr dumping.
- dumper := vom.NewDumper(dumpWriter{cmd.Stdout(), cmd.Stdout()})
+ dumper := vom.NewDumper(dumpWriter{env.Stdout, env.Stdout})
defer dumper.Close()
// Handle simple non-hex cases.
switch flagDataRep {