From 7935c4732f551b601d01dcc66bde17a801092a1d Mon Sep 17 00:00:00 2001 From: Nhoya Date: Fri, 22 Sep 2017 15:51:29 +0200 Subject: [PATCH] Added -f option for file input --- README.md | 2 +- multibase.go | 31 +++++++++++++++++++------------ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index db532aa..65303ee 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ Usage: multibase [OPTIONS] Application Options: + -f, --file= Specify input file --b32 Generate base32 of given string/file --b58 Generate base58 of given string/file --b64 Generate base64 of given string/file @@ -29,7 +30,6 @@ Application Options: Help Options: -h, --help Show this help message - ``` ## Installation diff --git a/multibase.go b/multibase.go index 8cc8e4d..843bfd2 100644 --- a/multibase.go +++ b/multibase.go @@ -12,29 +12,34 @@ import ( ) var opts struct { - Base32 bool `long:"b32" description:"Generate base32 of given string/file"` - Base58 bool `long:"b58" description:"Generate base58 of given string/file"` - Base64 bool `long:"b64" description:"Generate base64 of given string/file"` - Base64URL bool `long:"b64u" description:"Generate URL-compatible base64"` - Base85 bool `long:"b85" description:"Generate Abobe's PostScript/PDF base85 of given string/file"` - Decode bool `short:"d" long:"decode" description:"Decode data"` + File string `short:"f" long:"file" description:"Specify input file"` + Base32 bool `long:"b32" description:"Generate base32 of given string/file"` + Base58 bool `long:"b58" description:"Generate base58 of given string/file"` + Base64 bool `long:"b64" description:"Generate base64 of given string/file"` + Base64URL bool `long:"b64u" description:"Generate URL-compatible base64"` + Base85 bool `long:"b85" description:"Generate Abobe's PostScript/PDF base85 of given string/file"` + Decode bool `short:"d" long:"decode" description:"Decode data"` } func main() { result := "" + var target []byte var byteResult []byte _, err := flags.Parse(&opts) if err != nil { os.Exit(1) } + parser := flags.NewParser(&opts, flags.Default) - target, _ := ioutil.ReadAll(os.Stdin) - targetString := string(target[:]) - - if target == nil { - fmt.Println("You must specify input") - os.Exit(1) + if opts.File != "" { + target, err = ioutil.ReadFile(opts.File) + if err != nil { + fmt.Println(err) + } + } else { + target, _ = ioutil.ReadAll(os.Stdin) } + targetString := string(target[:]) if opts.Base32 { if opts.Decode { @@ -74,6 +79,8 @@ func main() { ascii85.Encode(buffer, target) result = string(buffer) } + } else { + parser.WriteHelp(os.Stderr) } if result != "" {