-
Notifications
You must be signed in to change notification settings - Fork 21
Introduce a glorious new cli arg splitter #29
Conversation
Allow &str and &[&str] as input for `command` Fixes #25
aed80a1
to
a04a0e1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I commented in German - didn't notice. ^^
The general approach is very good, but I see some problems with the parsing implementation. IMO we should define some invariants which should hold, i.e. stringify!("...").to_cmd()]
should equal ["..."].to_cmd()
for any string "..."
.
} else { | ||
in_quote.push(c); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ich glaube es gibt Randfälle, in denen sich das noch unerwartet verhalten könnte. Ich spiele mal Advocatus Diaboli:
assert_eq!(
r#"echo lorem ipsum"#.to_cmd(),
vec!["echo", "lorem", "ipsum"]
);
assert_eq!(
r#"echo "lorem' ipsum" dolor "sit' amet" '"#.to_cmd(),
vec!["echo", "lorem' ipsum", "dolor", "sit' amet"]
);
assert_eq!(
r#"echo "lorem\" ipsum" dolor "sit\" amet"'"#.to_cmd(),
vec!["echo", "lorem\" ipsum", "dolor", "sit\" amet"]
);
Bei echo ist das unkritisch, bei anderen Befehlen könnte es schon problematisch sein, wenn Argument 3 plötzlich an Position 4 steht, etc. Vielleicht kann man sich hier auch irgendein fuzzy-testing überlegen? Das macht es natürlich alles sehr sehr kompliziert. Wichtig wäre aber, dass es sich vorhersehbar verhält.
M.E. wäre folgende Invariante sehr erstrebenswert:
- Wenn
"..."
ein gültiger Rust String ist (...
sind Platzhalter), dann solltestringify!("...").to_cmd()]
gleich["..."].to_cmd()
sein.
Ich kenne die stringify!
Methode nicht im Detail, vermute aber, dass auf einen String angewendet, JSON::from_str(stringify!("..."))
weitestgehend die Identität sein sollte. Auf dieser Annahme basiert ja die aktuelle Makro-Logik. Auch hier wäre vielleicht mal eine genauere Recherche oder Fuzzy Testing sinnvoll. :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
r#"echo "lorem' ipsum" dolor "sit' amet" '"#.to_cmd()
Great test case! I was so focused on getting other stuff that I think I actually went a bit overboard with this: We don't really have to deal with nested quotes at all – just recognize and ignore escaped quotes, right?
I've also just seen this crate, but I think it's more complex than we need here. I don't want to emulate shell syntax itself, just split some args :)
I think I can add quickcheck to test the invariant, but I'm not sure JSON::from_str(stringify!("..."))
is that reliable. We'll see :)
PS: I would concentrate on supporting |
Let's put this on hold for now. |
Allow
&str
and&[&str]
as input forcommand
Fixes #25
Currently based on #24. Rebase on master before merging