v0.2: Read stdin if filename is empty or "-"

This commit is contained in:
yo 2023-07-25 09:44:08 +02:00
parent f82e0ddea3
commit 6e23343c68

34
main.go
View File

@ -17,27 +17,43 @@ import (
)
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() {
var input string
flag.Parse()
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]
flag.Usage = Usage
// 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)
if err != nil {
fmt.Errorf("%v\n", err)
}
// remember to close the file at the end of the program
defer f.Close()
} else {
f = os.Stdin
}
// read the file line by line using scanner
scanner := bufio.NewScanner(f)