Skip to content

Commit

Permalink
prototype get-wrapper docopts.sh for #60
Browse files Browse the repository at this point in the history
  • Loading branch information
Sylvain Viart committed Apr 22, 2022
1 parent c3dcdf2 commit 4bd5862
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docopts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package main

import (
_ "embed"
"fmt"
"github.com/docopt/docopt-go"
"io"
Expand Down Expand Up @@ -43,6 +44,7 @@ Usage:
docopts [options] [--no-declare] -A <name> -h <msg> : [<argv>...]
docopts [options] -G <prefix> -h <msg> : [<argv>...]
docopts [options] --no-mangle -h <msg> : [<argv>...]
docopts get-wrapper docopts.sh
Options:
-h <msg>, --help=<msg> The help message in docopt format.
Expand Down Expand Up @@ -79,12 +81,19 @@ Options:
with -A argument.
--debug Output extra parsing information for debugging.
Output cannot be used in bash eval.
Actions:
get-wrapper docopts.sh Print docopts.sh source content on stdout and quit.
`

// testing trick, out can be mocked to catch stdout and validate
// https://stackoverflow.com/questions/34462355/how-to-deal-with-the-fmt-golang-library-package-for-cli-testing
var out io.Writer = os.Stdout

// load the entire file into a string at compile time
//go:embed docopts.sh
var docopts_sh string

// debug helper
func print_args(args docopt.Opts, message string) {
fmt.Printf("################## %s ##################\n", message)
Expand Down Expand Up @@ -391,6 +400,11 @@ func main() {
panic(msg)
}

if arguments["get-wrapper"].(bool) {
fmt.Print(docopts_sh)
os.Exit(0)
}

debug := arguments["--debug"].(bool)
if debug {
print_args(arguments, "golang")
Expand Down

0 comments on commit 4bd5862

Please sign in to comment.