-
Notifications
You must be signed in to change notification settings - Fork 9
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
feat: variable string substitution functions #148
Conversation
I'm taking care of this but I think it is better to merge #169 first. |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #148 +/- ##
==========================================
+ Coverage 61.84% 62.64% +0.79%
==========================================
Files 30 30
Lines 2967 3183 +216
==========================================
+ Hits 1835 1994 +159
- Misses 1132 1189 +57
|
I'll look into why this is failing on Windows tomorrow. Meanwhile @certik let me know if you have any ideas. @wolfv One thing I noticed while working on this issue is that the state – more specifically env vars can be modified through arithmetic operations and hence each function at any point should contain a list of changes because this change can happen almost anywhere now. This led to implementation of flag=0 && echo ${FOO:-$((flag=1, 5))} This is supported now. |
Just added support for all three of substring expansion, default value expansion, and alternate value expansion. echo ${var:-val is not set}
echo ${HOMEDIR:-~} This should adding for loop support much easier. |
Looks great, works great! One thing I tried (not really important) doesn't work, which is arithmetic expression in the substring function:
The error message is top notch though!! |
Great job! I have two questions:
It's really easy to accidentally implement something that is actually extending Bash, like we did with the |
@wolfv This is now supported along with negative value for the start as well: FOO=12345 && echo ${FOO: -4: 2}
FOO=12345 && echo ${FOO: -2}
FOO=12345 && echo ${FOO: -2:-1} |
I am essentially checking Bash's behaviour from Bash's reference manual. My goal is also to keep the same behaviour as Bash.
I have checked the outputs of the tests added in the |
@prsabahrami excellent. Looks like you did the best we can do so far, so that's good enough. Eventually we should indeed create our test suite in such a way so that we can execute it via Bash / Zsh at our CI, to actually guarantee that all our tests work there too. Until then, we just have to do our best manually. |
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.
The changes are extensive and the Codecov comments are really distracting to see the bigger picture (why don't we just disable them completely, and just review Codecov on an external website?). However the test changes look good, the grammar I think is ok. I have not carefully reviewed all the changes in the shell
subdirectory.
@wolfv, @Hofer-Julian if the changes look good to you, then we can merge.
@prsabahrami I agree about #62 being the priority.
@certik Got rid of the comments! |
@Hofer-Julian Any thoughts on this? |
@prsabahrami sorry, didn't get around playing with that yet. Will check it out tomorrow! |
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've tried the following: echo "${x:-100}"
In bash this gives "100", in shell it gives 1.
Is this expected?
I also saw that we underused miette::bail!
and changed a few in a commit
@Hofer-Julian I've added double quoted tests here as well. Also, tried |
I tried it again, and indeed I couldn't reproduce it. Sorry for the noise :) |
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.
Great work Parsa and Wolf ✨
Started to implement some functions in the style of:
${VAR:2:3}
(substring starting at index 2 length 3)${VAR:-DEFAULT}
(use VAR if it exists, elseDEFAULT
).Also parses
${VAR:=FOO}
and${VAR:+FOO}
which are not properly implemented yet.More docs: https://mywiki.wooledge.org/BashGuide/Parameters#Parameter_Expansion
However, this implementation falls a little short.
For dealing with the assignment we would need to change the implementation a bit.
Also arguments can be expanded themselves, e.g.
${FOO:-${OTHER}}
should work, so the options would technically have to be parsed as Variables themselves and evaluated on demand.