Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 52 additions & 1 deletion cmd/dot/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 6 additions & 2 deletions cmd/dot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
`

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions cmd/dot/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
Loading