From 243b90195bb1e617f32f11d9c10f2252f9657f1f Mon Sep 17 00:00:00 2001 From: closer-claudio Date: Sun, 22 Nov 2026 20:20:28 -0800 Subject: [PATCH] -? prints the usage, and the first of -?, -h and -V is the whole command, as in Graphviz Graphviz's dot takes -? for its help: usage on standard output, exit 0. The command called it an unknown flag. It now takes -? as it takes -h (which Graphviz's dot does not have, and which stays). Graphviz's dot acts on -? and -V where it meets them and reads nothing after: `dot -? -Tnope` and `dot -V -Tnope` exit 0, `dot -Tnope -?` exits 1, `dot -V -?` prints the version. The command read the whole line first, so a bad flag anywhere won over the help, and -h won over -V wherever each stood. parseArgs now returns at the first of them. TestTheFirstOfHelpAndVersionIsTheWholeCommand holds seven orderings, each also run against the native dot under DOT_ORACLE (all seven agree with 16.1.1); the contract table gains -? and the orderings with their streams, TestParseArgs the same at the parser. Co-Authored-By: Claude Fable 5.2 Signed-off-by: closer-claudio --- README.md | 4 ++- cmd/dot/contract_test.go | 53 +++++++++++++++++++++++++++++++++++++++- cmd/dot/main.go | 8 ++++-- cmd/dot/main_test.go | 4 +++ 4 files changed, 65 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7891240..c3c46d9 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,9 @@ dot [-K engine] [-T format] [-o path] [input] | input | a DOT file, or `-` | standard input | Flags take Graphviz's glued form (`-Tpng`, `-oout.png`) as well as the spaced -one. `-V` prints the version, `-h` the usage. +one. `-V` prints the version; `-?`, or `-h`, the usage. The first of them +on the command line is the whole command, as with Graphviz's `dot`: what +follows it is not read. The exit statuses are those of Graphviz's `dot`: 0 on success, 2 when the input cannot be read (a file that is not there or may not be opened), and 1 diff --git a/cmd/dot/contract_test.go b/cmd/dot/contract_test.go index 5f25a96..5658024 100644 --- a/cmd/dot/contract_test.go +++ b/cmd/dot/contract_test.go @@ -142,7 +142,12 @@ func TestExitStatusesAndStreams(t *testing.T) { {"--version", "", []string{"--version"}, 0, "", false}, {"-h beats a bad graph", "nonsense", []string{"-h"}, 0, "usage:", false}, {"-V beats a bad graph", "nonsense", []string{"-V"}, 0, "", false}, - {"-h with -V", "", []string{"-V", "-h"}, 0, "usage:", false}, + {"-?", "", []string{"-?"}, 0, "usage:", false}, + {"-h before -V", "", []string{"-h", "-V"}, 0, "usage:", false}, + {"-? before a bad flag", "", []string{"-?", "-Tgif"}, 0, "usage:", false}, + {"-V before a bad flag", "", []string{"-V", "-Tgif"}, 0, "", false}, + {"-V before -h", "", []string{"-V", "-h"}, 0, "", false}, + {"a bad flag before -?", "", []string{"-Tgif", "-?"}, 1, "", true}, {"unknown flag", graph, []string{"-x"}, 1, "", true}, {"unknown format", graph, []string{"-Tgif"}, 1, "", true}, {"unknown layout", graph, []string{"-Kbogus"}, 1, "", true}, @@ -278,6 +283,52 @@ func TestFailuresExitAsGraphvizsDot(t *testing.T) { } } +// The first of -?, -h and -V on the command line is the whole command, as +// with Graphviz's dot: a bad flag after it is not read, one before it is the +// failure. Each status is held to the native dot when there is one (-h +// apart, which it does not have). +func TestTheFirstOfHelpAndVersionIsTheWholeCommand(t *testing.T) { + t.Parallel() + + cases := []struct { + args []string + code int + }{ + {[]string{"-?"}, 0}, + {[]string{"-?", "-Tnope"}, 0}, + {[]string{"-?", "-V"}, 0}, + {[]string{"-V", "-Tnope"}, 0}, + {[]string{"-V", "-?"}, 0}, + {[]string{"-Tnope", "-?"}, 1}, + {[]string{"-Tnope", "-V"}, 1}, + } + + for _, c := range cases { + t.Run(strings.Join(c.args, " "), func(t *testing.T) { + t.Parallel() + + if got := dot(t, t.TempDir(), "", c.args...); got.code != c.code { + t.Fatalf("exit %d, want %d\nstdout %q\nstderr %q", got.code, c.code, got.stdout, got.stderr) + } + + t.Run("as Graphviz's dot has it", func(t *testing.T) { + code := 0 + + err := child(t, nativeDot(t), c.args...).Run() + if exit, ok := errors.AsType[*exec.ExitError](err); ok { + code = exit.ExitCode() + } else if err != nil { + t.Fatal(err) + } + + if code != c.code { + t.Errorf("the native dot exits %d, want %d", code, c.code) + } + }) + }) + } +} + // A text with no graph in it, empty, blank or comments alone, is nothing to // draw and no failure, as with Graphviz's dot: exit 0, nothing on either // stream, and with -o no file made and the one at the path left as it was. diff --git a/cmd/dot/main.go b/cmd/dot/main.go index 85e2322..d95c400 100644 --- a/cmd/dot/main.go +++ b/cmd/dot/main.go @@ -36,7 +36,7 @@ const usage = `usage: dot [-K engine] [-T format] [-o path] [input] -T format output format: dot (default), xdot, svg, png, jpg -o path output file (default: standard output) -V print the version - -h print this help + -h, -? print this help input DOT file (default, or "-": standard input) ` @@ -119,10 +119,14 @@ func parseArgs(args []string) (options, error) { arg := args[index] switch { - case arg == "-h", arg == "--help": + // As with Graphviz's dot, the first of these to be reached is the + // whole command: what follows it is not read, a bad flag included. + case arg == "-h", arg == "-?", arg == "--help": opt.help = true + return opt, nil case arg == "-V", arg == "--version": opt.version = true + return opt, nil case arg == "-", !strings.HasPrefix(arg, "-"): if opt.named { return opt, fmt.Errorf("%w: one input at most, got %q and %q", errUsage, opt.input, arg) diff --git a/cmd/dot/main_test.go b/cmd/dot/main_test.go index a7949f7..c31db9e 100644 --- a/cmd/dot/main_test.go +++ b/cmd/dot/main_test.go @@ -32,6 +32,10 @@ func TestParseArgs(t *testing.T) { false, }, {[]string{"-h"}, options{layout: graphviz.DOT, format: graphviz.GV, help: true}, false}, + {[]string{"-?"}, options{layout: graphviz.DOT, format: graphviz.GV, help: true}, false}, + {[]string{"-?", "-Tgif"}, options{layout: graphviz.DOT, format: graphviz.GV, help: true}, false}, + {[]string{"-V", "-?"}, options{layout: graphviz.DOT, format: graphviz.GV, version: true}, false}, + {[]string{"-Tgif", "-?"}, options{}, true}, {[]string{"-V"}, options{layout: graphviz.DOT, format: graphviz.GV, version: true}, false}, {[]string{"-Tgif"}, options{}, true}, {[]string{"-Kbogus"}, options{}, true},