-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
cmd_collapse.go
48 lines (36 loc) · 944 Bytes
/
cmd_collapse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/skx/subcommands"
)
// Structure for our options and state.
type collapseCommand struct {
// We embed the NoFlags option, because we accept no command-line flags.
subcommands.NoFlags
}
// Info returns the name of this subcommand.
func (c *collapseCommand) Info() (string, string) {
return "collapse", `Remove whitespace from input.
Details:
This command reads input and removes all leading and trailing whitespace
from it. Empty lines are also discarded.`
}
// Execute is invoked if the user specifies `collapse` as the subcommand.
func (c *collapseCommand) Execute(args []string) int {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
line = strings.TrimSpace(line)
if len(line) > 0 {
fmt.Println(line)
}
}
if err := scanner.Err(); err != nil {
fmt.Printf("Error: %s\n", err.Error())
return 1
}
return 0
}