Skip to content

Commit

Permalink
Merge pull request #22 from AkihiroSuda/stdio
Browse files Browse the repository at this point in the history
use stdio for default input and output
  • Loading branch information
cpuguy83 authored Sep 2, 2016
2 parents 2724a9c + dbd16da commit 6a13ed3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
2 changes: 2 additions & 0 deletions go-md2man.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ go-md2man 1 "January 2015" go-md2man "User Manual"
go-md2man converts standard markdown formatted documents into manpages. It is
written purely in Go so as to reduce dependencies on 3rd party libs.

By default, the input is stdin and the output is stdout.

# Example
Convert the markdown file "go-md2man.1.md" into a manpage.

Expand Down
29 changes: 18 additions & 11 deletions md2man.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ import (
"github.com/cpuguy83/go-md2man/md2man"
)

var inFilePath = flag.String("in", "", "Path to file to be processed")
var outFilePath = flag.String("out", "", "Path to output processed file")
var inFilePath = flag.String("in", "", "Path to file to be processed (default: stdin)")
var outFilePath = flag.String("out", "", "Path to output processed file (default: stdout)")

func main() {
var err error
flag.Parse()

inFile, err := os.Open(*inFilePath)
if err != nil {
fmt.Println(err)
os.Exit(1)
inFile := os.Stdin
if *inFilePath != "" {
inFile, err = os.Open(*inFilePath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
defer inFile.Close()

Expand All @@ -30,12 +34,15 @@ func main() {

out := md2man.Render(doc)

outFile, err := os.Create(*outFilePath)
if err != nil {
fmt.Println(err)
os.Exit(1)
outFile := os.Stdout
if *outFilePath != "" {
outFile, err = os.Create(*outFilePath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer outFile.Close()
}
defer outFile.Close()
_, err = outFile.Write(out)
if err != nil {
fmt.Println(err)
Expand Down

0 comments on commit 6a13ed3

Please sign in to comment.