-
Notifications
You must be signed in to change notification settings - Fork 350
Add input file and os.Stdin support #5
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,11 @@ import ( | |
"strings" | ||
|
||
qrcode "github.com/skip2/go-qrcode" | ||
"io/ioutil" | ||
) | ||
|
||
func main() { | ||
inFile := flag.String("i", "", "content file, empty for STDIN") | ||
outFile := flag.String("o", "", "out PNG file prefix, empty for stdout") | ||
size := flag.Int("s", 256, "image size (pixel)") | ||
flag.Usage = func() { | ||
|
@@ -41,12 +43,25 @@ Usage: | |
if *size <= 0 { | ||
checkError(fmt.Errorf("Error: value of -s should > 0")) | ||
} | ||
if len(flag.Args()) == 0 { | ||
flag.Usage() | ||
checkError(fmt.Errorf("Error: no content given")) | ||
} | ||
|
||
content := strings.Join(flag.Args(), " ") | ||
if *inFile != "" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey, I think reading content from STDIN/a file is a nice addition :) After this pull request there are three ways to specify content:
If you try the "qrcode hello | display" method, it now hangs and waits for something on STDIN. I suggest taking content from one source only (first the -i flag, then command line arguments, or lastly STDIN), so the usage is more obvious and easier to explain. This would work well. thanks, Tom |
||
b, err := ioutil.ReadFile(*inFile) | ||
if err != nil { | ||
checkError(err) | ||
} | ||
content += string(b) | ||
} else { | ||
b, err := ioutil.ReadAll(os.Stdin) | ||
if err != nil { | ||
checkError(err) | ||
} | ||
content += string(b) | ||
} | ||
if content == "" { | ||
flag.Usage() | ||
checkError(fmt.Errorf("Error: no content given?")) | ||
} | ||
|
||
var err error | ||
var q *qrcode.QRCode | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest prioritising the -i flag, then command line arguments, then reading from STDIN last, like: