Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use stdio for default input and output #22

Merged
merged 1 commit into from
Sep 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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