Skip to content

Commit

Permalink
- [+] add option to be quiet
Browse files Browse the repository at this point in the history
  • Loading branch information
suntong committed Apr 18, 2017
1 parent 6cf0d5b commit 87e4c58
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Options:
-d, --delimiter[= ] delimiter for pieces csv output
-w, --wrap-html wrap up the output with html tags
-b, --base base href tag used in the wrapped up html
-q, --quiet be quiet
```

Its output has two modes, _single selection mode_ and _block selection mode_, depending on whether the `--piece` parameter is given on the command line or not.
Expand Down
5 changes: 5 additions & 0 deletions cascadia.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@ Options:
Flag: b,base
Usage: base href tag used in the wrapped up html

- Name: Quiet
Type: bool
Flag: q,quiet
Usage: be quiet

1 change: 1 addition & 0 deletions cascadiaCLIDef.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type rootT struct {
Deli string `cli:"d,delimiter" usage:"delimiter for pieces csv output" dft:"\t"`
WrapHTML bool `cli:"w,wrap-html" usage:"wrap up the output with html tags"`
Base string `cli:"b,base" usage:"base href tag used in the wrapped up html"`
Quiet bool `cli:"q,quiet" usage:"be quiet"`
}

var root = &cli.Command{
Expand Down
9 changes: 6 additions & 3 deletions cascadiaMain.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ func cascadiaC(ctx *cli.Context) error {
</head>
<body>`, argv.Base)

Cascadia(argv.Filei, argv.Fileo, argv.CSS, argv.Piece, argv.Deli, argv.WrapHTML)
Cascadia(argv.Filei, argv.Fileo, argv.CSS, argv.Piece, argv.Deli,
argv.WrapHTML, argv.Quiet)
argv.Filei.Close()
argv.Fileo.Close()
return nil
Expand All @@ -79,7 +80,7 @@ func cascadiaC(ctx *cli.Context) error {
//--------------------------------------------------------------------------

// Cascadia filters the input buffer/stream `bi` with CSS selectors `css` and write to the output buffer/stream `bw`.
func Cascadia(bi io.Reader, bw io.Writer, css string, piece MapStringString, deli string, wrapHTML bool) error {
func Cascadia(bi io.Reader, bw io.Writer, css string, piece MapStringString, deli string, wrapHTML bool, beQuiet bool) error {
if len(piece.Values) == 0 {
doc, err := html.Parse(bi)
abortOn("Input", err)
Expand All @@ -88,7 +89,9 @@ func Cascadia(bi io.Reader, bw io.Writer, css string, piece MapStringString, del

// https://godoc.org/github.com/andybalholm/cascadia
ns := c.MatchAll(doc)
fmt.Fprintf(os.Stderr, "%d elements for '%s':\n", len(ns), css)
if !beQuiet {
fmt.Fprintf(os.Stderr, "%d elements for '%s':\n", len(ns), css)
}
for _, n := range ns {
html.Render(bw, n)
fmt.Fprintf(bw, "\n")
Expand Down
2 changes: 1 addition & 1 deletion cascadia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func TestSelectors(t *testing.T) {
for _, test := range selectorTests {
buf := bytes.NewBufferString("")
Cascadia(strings.NewReader(test.HTML), buf, test.selector, MapStringString{}, ",", false)
Cascadia(strings.NewReader(test.HTML), buf, test.selector, MapStringString{}, ",", false, false)
got := buf.String()
if len(got) == 0 && len(test.results) == 0 {
// correct
Expand Down

0 comments on commit 87e4c58

Please sign in to comment.