nk412 / optparse

Simple command line arguments parser for BASH
MIT License
158 stars 32 forks source link

optparse breaks when spaces or special characters are in default values #16

Open erikvip opened 8 years ago

erikvip commented 8 years ago

Error occurs when a space is in argument default values (or passed from command line too, I believe). This is because the value assignment is not quoted at all in the generated tmp file.

Also breaks when special characters (such as a single quote, double quote, any bash langauge specials) are in default values or passed.

Example code

#!/usr/bin/env bash
source "optparse.bash"
optparse.define short=d long=default-value-with-spaces desc="An argument which has spaces in it's default value" variable=DEFAULT_WITH_SPACES default="default value with spaces"
optparse.define short=s long=default-value-with-specials desc="An argument with a few special characters in it. A single quote should be handled ok" variable=DEFAULT_WITH_SPECIALS default="this is ' the !@#$%^&*( \${P\} special values" 

Output

$ ./test.sh
/tmp/optparse-19676.tmp: line 44: value: command not found
/tmp/optparse-19676.tmp: line 45: unexpected EOF while looking for matching `''
/tmp/optparse-19676.tmp: line 68: syntax error: unexpected end of file

Expected result

Should handle spaces & special characters gracefully, without throwing an error.

Fix

Easy fix, wrap assignment in quotes, and escape any quotes in the input. This fixes most special character use cases, better than nothing...

Pull request is on the way.