From 243b90195bb1e617f32f11d9c10f2252f9657f1f Mon Sep 17 00:00:00 2001 From: closer-claudio Date: Sun, 22 Nov 2026 20:20:28 -0800 Subject: [PATCH 1/2] -? 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}, From 861f381ded69f43422b3ad0953186e08783bae74 Mon Sep 17 00:00:00 2001 From: closer-claudio Date: Sun, 22 Nov 2026 20:50:20 -0800 Subject: [PATCH 2/2] =?UTF-8?q?-V=20prints=20Graphviz's=20line,=20on=20sta?= =?UTF-8?q?ndard=20error:=20"dot=20-=20graphviz=20version=2016.1.1=20(?= =?UTF-8?q?=E2=80=A6)"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Graphviz's dot answers -V with one line on standard error and exits 0: "dot - graphviz version 16.1.1 (20261108.1752)" is Homebrew's. The command printed its own version alone on standard output ("v1.5.0", or "(devel)"), so what reads the Graphviz release out of `dot -V`, as build scripts and editors' plugins do, read nothing from it. -V now prints that line, in that shape and on that stream: the Graphviz release is the one the engine embeds, asked of the engine (graphviz.Version, which go-graphviz has from adc5c5a, the version go.mod now requires), and the parenthesis holds this command's version where Graphviz's dot has its build date: "(forkcloser/dot v1.5.0)", "devel" for a build with no version. Standard output has nothing. A version the engine cannot give is an error and exit 1. This breaks a caller that read the command's own version from standard output. TestVersionIsGraphvizsLine holds the line's shape, the stream and the exit, that the release is the one the engine is pinned to, and, under DOT_ORACLE, that the native dot's line has the same shape and the same major and minor release. The contract table's -V rows expect the line on standard error and nothing on standard output. Co-Authored-By: Claude Fable 5.2 Signed-off-by: closer-claudio --- README.md | 9 +++-- cmd/dot/contract_test.go | 72 ++++++++++++++++++++++++++++++++++++---- cmd/dot/main.go | 30 +++++++++++++---- go.mod | 2 +- go.sum | 4 +-- 5 files changed, 99 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index c3c46d9..82b1488 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,12 @@ 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; `-?`, 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. +one. `-?`, or `-h`, prints the usage. `-V` prints the version, one line on +standard error as Graphviz's `dot` prints its own and in the same shape, +`dot - graphviz version 16.1.1 (forkcloser/dot v1.5.0)`: the Graphviz release +the engine embeds, then this command's version where Graphviz's `dot` has its +build date. The first of these flags 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 5658024..288bbd6 100644 --- a/cmd/dot/contract_test.go +++ b/cmd/dot/contract_test.go @@ -13,6 +13,7 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "runtime" "strings" "testing" @@ -174,15 +175,17 @@ func TestExitStatusesAndStreams(t *testing.T) { t.Fatalf("stdout %q, want a prefix %q", got.stdout, c.stdout) } - switch c.code { - case 0: + switch { + case c.code == 0 && c.stdout == "": + // -V: its one line is on standard error, where Graphviz's + // dot prints its own, and standard output has nothing. + if got.stdout != "" || !versionLine.MatchString(got.stderr) { + t.Fatalf("stdout %q, stderr %q; want nothing and the version line", got.stdout, got.stderr) + } + case c.code == 0: if got.stderr != "" { t.Fatalf("stderr on success: %q", got.stderr) } - - if c.stdout == "" && strings.TrimSpace(got.stdout) == "" { - t.Fatalf("no version printed: %q", got.stdout) - } default: if got.stdout != "" { t.Fatalf("stdout on failure: %q", got.stdout) @@ -283,6 +286,63 @@ func TestFailuresExitAsGraphvizsDot(t *testing.T) { } } +// versionLine is what -V prints: Graphviz's dot's line, with this command's +// version where that one has its build date. +var versionLine = regexp.MustCompile(`^dot - graphviz version (\d+\.\d+\.\d+) \(forkcloser/dot \S+\)\n$`) + +// -V prints one line on standard error and exits 0, as Graphviz's dot does, +// and the line has that one's shape, so what reads the Graphviz release out +// of the one reads it out of the other. The release is the one the engine +// embeds. +func TestVersionIsGraphvizsLine(t *testing.T) { + t.Parallel() + + got := dot(t, t.TempDir(), "", "-V") + + match := versionLine.FindStringSubmatch(got.stderr) + if got.code != 0 || got.stdout != "" || match == nil { + t.Fatalf( + "exit %d, stdout %q, stderr %q; want 0, nothing and the version line", + got.code, + got.stdout, + got.stderr, + ) + } + + t.Run("the release is the engine's", func(t *testing.T) { + t.Parallel() + + if pin := enginePin(t); match[1] != pin { + t.Errorf("-V names Graphviz %s, the engine is built from %s", match[1], pin) + } + }) + + t.Run("as Graphviz's dot has it", func(t *testing.T) { + t.Parallel() + + cmd := child(t, nativeDot(t), "-V") + + var stdout, stderr bytes.Buffer + + cmd.Stdout, cmd.Stderr = &stdout, &stderr + + if err := cmd.Run(); err != nil || stdout.Len() != 0 { + t.Fatalf("the native dot: %v, stdout %q", err, stdout.String()) + } + + // The same words up to the release, one line, a parenthesis after. + native := regexp.MustCompile(`^dot - graphviz version (\d+\.\d+\.\d+) \(.+\)\n$`). + FindStringSubmatch(stderr.String()) + if native == nil { + t.Fatalf("the native dot's line is %q, not the shape -V follows", stderr.String()) + } + + if majorMinor(native[1]) != majorMinor(match[1]) { + t.Errorf("the native dot is %s, -V names %s", native[1], match[1]) + } + }) +} + // 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 diff --git a/cmd/dot/main.go b/cmd/dot/main.go index d95c400..2e0ed42 100644 --- a/cmd/dot/main.go +++ b/cmd/dot/main.go @@ -35,7 +35,7 @@ const usage = `usage: dot [-K engine] [-T format] [-o path] [input] osage, patchwork -T format output format: dot (default), xdot, svg, png, jpg -o path output file (default: standard output) - -V print the version + -V print the version, on standard error -h, -? print this help input DOT file (default, or "-": standard input) ` @@ -188,12 +188,21 @@ func parseFlag(opt *options, args []string, index int) (int, error) { return index, nil } -func version() string { - if bi, ok := debug.ReadBuildInfo(); ok && bi.Main.Version != "" { - return bi.Main.Version +// version is the line -V prints, in the shape of Graphviz's dot's: the +// Graphviz release the engine embeds where Graphviz's dot names its own, and +// this command's version where that one puts its build date. +func version() (string, error) { + engine, err := graphviz.Version() + if err != nil { + return "", fmt.Errorf("the engine's version: %w", err) + } + + own := "devel" + if bi, ok := debug.ReadBuildInfo(); ok && bi.Main.Version != "" && bi.Main.Version != "(devel)" { + own = bi.Main.Version } - return "(devel)" + return fmt.Sprintf("dot - graphviz version %s (forkcloser/dot %s)", engine, own), nil } // render lays graph out and writes it to out in the format asked for. @@ -245,7 +254,16 @@ func run(args []string, stdin io.Reader, stdout, stderr io.Writer) int { fmt.Fprint(stdout, usage) return 0 case opt.version: - fmt.Fprintln(stdout, version()) + var line string + + if line, err = version(); err != nil { + fmt.Fprintln(stderr, "dot:", err) + return statusFailed + } + + // On standard error, where Graphviz's dot prints its own. + fmt.Fprintln(stderr, line) + return 0 } diff --git a/go.mod b/go.mod index d38108f..4958ede 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/forkcloser/dot go 1.26.0 require ( - github.com/forkcloser/go-graphviz v0.6.1-0.20261123080857-0b16160518c7 + github.com/forkcloser/go-graphviz v0.6.1-0.20261123161547-adc5c5a433b3 github.com/forkcloser/xos v0.1.2 ) diff --git a/go.sum b/go.sum index dd8295e..56b7557 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/forkcloser/go-graphviz v0.6.1-0.20261123080857-0b16160518c7 h1:X4pQ/98Ml4B2uYcKa0DE7IOUlAykvGxMgAkMJTU7zTE= -github.com/forkcloser/go-graphviz v0.6.1-0.20261123080857-0b16160518c7/go.mod h1:SC+ch2AgTanRZJH8JpOw99BOCnLQdBLEu1xz4QuihcE= +github.com/forkcloser/go-graphviz v0.6.1-0.20261123161547-adc5c5a433b3 h1:tffQ7H6scYj55WfxWXQS+VIW8xG9JPvRn+4uZnvf2iM= +github.com/forkcloser/go-graphviz v0.6.1-0.20261123161547-adc5c5a433b3/go.mod h1:SC+ch2AgTanRZJH8JpOw99BOCnLQdBLEu1xz4QuihcE= github.com/forkcloser/xos v0.1.2 h1:7TTH/bJ8hRIBIJPZp5WgIY//bgJFlyd+dpsgSEjnwOM= github.com/forkcloser/xos v0.1.2/go.mod h1:BxyGnZr+xW0m+0ph1xQ0qwHYs30hiXeUP7foOlKvxP0= github.com/tetratelabs/wazero v1.12.0 h1:DuWcpNu/FzgEXgGBDp8J1Spc+CWOvvtvVyjKlaZopYU=