-
Notifications
You must be signed in to change notification settings - Fork 17
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
Pass globals as command-line options #78
Conversation
cc @BekaValentine — this might be convenient for you. |
I'm ok with leaving both the "how do we handle non- |
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.
Ready for review; see above for comment about non-string-valued globals & empty strings. If we're ok with leaving them as future work, I'll file issues when merging this PR.
version = "2.32" | ||
version = "3.2" |
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.
Had to bump this to be able to supply the args the way I wanted.
.short("q") | ||
.short('q') |
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.
Consequence of bumping clap
.
let globals = matches.get_many::<String>("global").unwrap_or_default(); | ||
let mut globals_ = Variables::new(); | ||
for kv in globals { | ||
let kv_ = kv | ||
.split_once('=') | ||
.with_context(|| format!("Expected key-value pair separated by '=', got {}.", kv))?; | ||
globals_.add( | ||
Identifier::from(kv_.0), | ||
graph::Value::String(kv_.1.to_string()), | ||
)?; | ||
} |
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.
I would have preferred to do this with a single map but I couldn't figure out how to express the two nested levels of failure (failure in parsing global values, and failure in the surrounding context). If anyone has suggestions, I'm all ears; I think we should be able to avoid both the second variable and the mutability, and (ideally) end up with something more readable as well.
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.
I actually like how you've done it here! As you point out, it becomes messy with the two levels of error handling, and I think this rendering makes the control flow clearer.
(It's doable, and probably involves the collect
method, which takes a vec of results and turns it into a result of a vec. But again, I'd rather we keep it as you've written it here.)
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.
(Also, if you want to bound the region where globals_
is mutable, you can do something like this:
let globals_ = globals_;
after you're done mutating it. That makes it immutable for the remainder of the scope.)
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.
I actually like how you've done it here! As you point out, it becomes messy with the two levels of error handling, and I think this rendering makes the control flow clearer.
(It's doable, and probably involves the
collect
method, which takes a vec of results and turns it into a result of a vec. But again, I'd rather we keep it as you've written it here.)
Cool, if this is idiomatic then I'll be happy to
(Also, if you want to bound the region where
globals_
is mutable, you can do something like this:let globals_ = globals_;after you're done mutating it. That makes it immutable for the remainder of the scope.)
Oh, that's a lovely use of shadowing! Thank you.
let mut config = ExecutionConfig::new(&mut functions, &globals).lazy(lazy); | ||
let mut config = ExecutionConfig::new(&mut functions, &globals_).lazy(lazy); |
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.
This is the key to how this all works: instead of passing in an empty map of Variables
, we pass in the one we built above instead. Really quite tidy, that.
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.
This looks great to me!
let globals = matches.get_many::<String>("global").unwrap_or_default(); | ||
let mut globals_ = Variables::new(); | ||
for kv in globals { | ||
let kv_ = kv | ||
.split_once('=') | ||
.with_context(|| format!("Expected key-value pair separated by '=', got {}.", kv))?; | ||
globals_.add( | ||
Identifier::from(kv_.0), | ||
graph::Value::String(kv_.1.to_string()), | ||
)?; | ||
} |
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.
I actually like how you've done it here! As you point out, it becomes messy with the two levels of error handling, and I think this rendering makes the control flow clearer.
(It's doable, and probably involves the collect
method, which takes a vec of results and turns it into a result of a vec. But again, I'd rather we keep it as you've written it here.)
Nice improvement @robrix! |
How should types other thanSee Pass globals of non-string types on CLI #86.String
be handled?See --global rejects empty string values #87.String::split_once()
seems not to match when the rhs (after the delimiter) is empty. How should we handle empty globals, e.g. empty strings?