I'm trying to implement option handling that mirrors the behavior of graph options like title(): If the option has an unquoted multiword string, it is treated as a single string. If a multiword string is put into the option surrounded by quotes, the quotes are stripped off and it is treated as a single string. If two quoted strings are put into the option, they are treated as separate strings.
I can achieve this with the following, which wraps the option in a set of compound-quotes before parsing it, IF it is not already wrapped in quotes, but I'm wondering if there is a more elegant approach I'm missing:
This does what I want
I can achieve this with the following, which wraps the option in a set of compound-quotes before parsing it, IF it is not already wrapped in quotes, but I'm wondering if there is a more elegant approach I'm missing:
Code:
program myprog
version 15
syntax , [ notes(string asis) ]
// feels like a hack
if !strpos(`"`notes'"',`"""') {
local notes `"`"`notes'"'"'
}
tokenize `"`notes'"'
while !missing(`"`1'"') {
di `"A note: [`1']"'
macro shift
}
end
Code:
. myprog , notes("This is the first note" "This is a second note")
A note: [This is the first note]
A note: [This is a second note]
. myprog , notes("This is a single quoted note")
A note: [This is a single quoted note]
. myprog , notes(This is a single note)
A note: [This is a single note]

Comment