Skip to content

Commit a473df2

Browse files
cli: input argument takes precedence over stdin
1 parent 9c53576 commit a473df2

File tree

5 files changed

+19
-23
lines changed

5 files changed

+19
-23
lines changed

cli/html2markdown/cmd/exec.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ func (cli *CLI) run() ([]error, error) {
134134
}
135135

136136
if len(cli.config.args) != 0 {
137-
138137
return nil, NewCLIError(
139138
fmt.Errorf("unknown arguments: %s", strings.Join(cli.config.args, " ")),
140139
Paragraph("Here is how you can use the CLI:"),

cli/html2markdown/cmd/exec_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ func TestExecute(t *testing.T) {
493493
modeStderr: modePipe,
494494

495495
inputStdin: []byte("<strong>stdin content</strong>"),
496-
inputArgs: []string{"html2markdown", "--input", filepath.Join(directoryPath, "website_a.html")},
496+
inputArgs: []string{"html2markdown", "--input", filepath.Join(directoryPath, "input", "website_a.html")},
497497
},
498498
},
499499
{

cli/html2markdown/cmd/io_input.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,11 @@ func fileNameWithoutExtension(fileName string) string {
2525
var defaultBasename = "output"
2626

2727
func (cli *CLI) listInputs() ([]*input, error) {
28-
if cli.isStdinPipe && cli.config.inputFilepath != "" {
29-
return nil, NewCLIError(
30-
fmt.Errorf("cannot use both stdin and --input at the same time. Use either stdin or specify an input file, but not both"),
31-
)
32-
}
33-
34-
if cli.isStdinPipe {
35-
data, err := io.ReadAll(cli.Stdin)
36-
if err != nil {
37-
return nil, err
38-
}
39-
return []*input{
40-
{
41-
inputFullFilepath: defaultBasename,
42-
data: data,
43-
},
44-
}, nil
45-
}
4628

29+
// NOTE: When both stdin and --input are specified,
30+
// the explicit --file argument takes precedence.
31+
// This improves interoperability with other tools like `xargs`.
32+
// https://github.com/JohannesKaufmann/html-to-markdown/issues/170
4733
if cli.config.inputFilepath != "" {
4834
matches, err := doublestar.FilepathGlob(cli.config.inputFilepath, doublestar.WithFilesOnly(), doublestar.WithNoFollow())
4935
if err != nil {
@@ -78,6 +64,19 @@ func (cli *CLI) listInputs() ([]*input, error) {
7864
return inputs, nil
7965
}
8066

67+
if cli.isStdinPipe {
68+
data, err := io.ReadAll(cli.Stdin)
69+
if err != nil {
70+
return nil, err
71+
}
72+
return []*input{
73+
{
74+
inputFullFilepath: defaultBasename,
75+
data: data,
76+
},
77+
}, nil
78+
}
79+
8180
return nil, NewCLIError(
8281
fmt.Errorf("the html input should be piped into the cli"),
8382
Paragraph("Here is how you can use the CLI:"),
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
2-
error: cannot use both stdin and --input at the same time. Use either stdin or specify an input file, but not both
3-
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**file content A**

0 commit comments

Comments
 (0)