Unable to pass arguments containing double-quotes
parseopt
for poKind, poKey, poVal in getopt():
case poKind
of cmdArgument:
discard
of cmdLongOption, cmdShortOption:
case poKey
of "n":
echo "name = " & poVal
of cmdEnd:
assert(false)
./test -n='"Weird Al" Yankovic'
It echoes back only "/" instead of '"Weird Al" Yankovic' .. is this a bug or workaround how the string is passed on the command-line?
I think this is definitely a bug in parseopt.
The main cause of the failure you see is the presence of spaces in the args.
Also, it also seems to matter if " is the first character of the string or not.
For example, below works as expected if no spaces are used:
bash-4.1$ ./parseopt_double_quotes -n='Yankovic"WierdAl"'
name = Yankovic"WierdAl"
But it fails if the double quoted part is moved to the beginning:
bash-4.1$ ./parseopt_double_quotes -n='"WeirdAl"Yankovic'
name = WeirdAl
I don't think this is a bug, as noted in #8452.
The gist: Use parseopt2 instead of parseopt if you need to operate on a list of properly quoted strings, rather than one long string.
(Parsing a single string into its arguments is hard, and most shells (e.g. bash) already do that for you.)
In the Issue, someone pointed out that parseopt2 is deprecated. I'm curious why.
Anyway, I was going to volunteer to fix parseopt -- to make it work on already separated strings -- but now I wish I hadn't bothered. I don't think I'm helping. There must be a reason why it is the way it is. I'll simply avoid it.