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

Enable the half-implemented -f, --function option #57

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 13 additions & 2 deletions docopts.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ Options:
with -A argument.
--debug Output extra parsing information for debugging.
Output cannot be used in bash eval.
-f, --function Return instead of exit. Useful inside shell
functions.
`

// testing trick, out can be mocked to catch stdout and validate
Expand Down Expand Up @@ -210,6 +212,12 @@ func (d *Docopts) Print_bash_global(args docopt.Opts) error {
var new_name string
var err error
var out_buf string
var var_format_str string = "%s=%s\n"
var output_format_str string = "%s"
if d.Exit_function {
var_format_str = "%s=%s "
output_format_str = "local %s"
}

varmap := make(map[string]string)

Expand Down Expand Up @@ -241,11 +249,13 @@ func (d *Docopts) Print_bash_global(args docopt.Opts) error {
varmap[new_name] = key
}

out_buf += fmt.Sprintf("%s=%s\n", new_name, To_bash(args[key]))
out_buf += fmt.Sprintf(var_format_str, new_name, To_bash(args[key]))
}

// final output
fmt.Fprintf(out, "%s", out_buf)
if (len(out_buf) > 0) {
fmt.Fprintf(out, output_format_str, out_buf)
}

return nil
}
Expand Down Expand Up @@ -414,6 +424,7 @@ func main() {
separator := arguments["--separator"].(string)
d.Mangle_key = !arguments["--no-mangle"].(bool)
d.Output_declare = !arguments["--no-declare"].(bool)
d.Exit_function = arguments["--function"].(bool)
global_prefix, err := arguments.String("-G")
if err == nil {
d.Global_prefix = global_prefix
Expand Down