v0.2: Read stdin if filename is empty or "-"
This commit is contained in:
parent
f82e0ddea3
commit
6e23343c68
34
main.go
34
main.go
@ -17,27 +17,43 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
gVersion = "0.0.1"
|
gVersion = "0.2"
|
||||||
|
|
||||||
|
Usage = func() {
|
||||||
|
fmt.Printf("deldify v%s\n", gVersion)
|
||||||
|
fmt.Printf("Usage: deldify [-h|--help] [filename]\n")
|
||||||
|
fmt.Printf(" read stdin if filename is empty or '-'\n")
|
||||||
|
fmt.Printf("Flags:\n")
|
||||||
|
fmt.Printf(" -h|--help: Display help\n")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var input string
|
var input string
|
||||||
|
|
||||||
flag.Parse()
|
flag.Usage = Usage
|
||||||
if len(flag.Args()) < 1 {
|
|
||||||
fmt.Printf("deldify v%s\n", gVersion)
|
|
||||||
fmt.Printf("Usage: deldify filename\n")
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
input = flag.Args()[0]
|
|
||||||
|
|
||||||
// open file
|
flag.Parse()
|
||||||
|
|
||||||
|
if len(flag.Args()) > 0 {
|
||||||
|
input = "-"
|
||||||
|
}
|
||||||
|
|
||||||
|
// open file or stdin
|
||||||
|
var f *os.File
|
||||||
|
|
||||||
|
if !strings.EqualFold(input, "-") {
|
||||||
f, err := os.Open(input)
|
f, err := os.Open(input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Errorf("%v\n", err)
|
fmt.Errorf("%v\n", err)
|
||||||
}
|
}
|
||||||
// remember to close the file at the end of the program
|
// remember to close the file at the end of the program
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
} else {
|
||||||
|
f = os.Stdin
|
||||||
|
}
|
||||||
|
|
||||||
// read the file line by line using scanner
|
// read the file line by line using scanner
|
||||||
scanner := bufio.NewScanner(f)
|
scanner := bufio.NewScanner(f)
|
||||||
|
Loading…
Reference in New Issue
Block a user