Comments (6)
Bashly cares about generating properly indented, human readable code. Heredoc had to be sacrificed.
This was discussed in a few other issues.
There are a couple of possible workarounds in the Examples section.
I am open to ideas on how to better handle this, assuming there is an elegant way.
from bashly.
Sure thing. Since this bothers more than a handful of people, I will see if there is anything to be done to better support this.
from bashly.
Thanks for the quick reply and the links.
I was looking for similar issues but couldn't find anything directly related to Bashly. Sorry for the duplication.
Maybe it's just me, but I find both existing solutions rather clumsy, especially the one with BASHLY_TAB_INDENT
, which requires the use of Unicode-specific spaces instead of "normal" ones.
In the meantime, I've written a script that allows me to write here documents in a more natural way:
cat <<EOF
line one
line two
EOF
The following variants also work:
cat << EOF
line one
line two
EOF
cat << EOF > output.txt
hello
there
EOF
If I want to preserve whitespace, I can use <<-
(probably it should be the other way around):
cat <<-EOF
line one
line two (indented)
EOF
And here's the script to make it work:
#!/usr/bin/env bash
executable="$(awk 'NR == 1 {sub(/name:\s*/, ""); print; exit}' src/bashly.yml)"
bashly generate
# Remove leading spaces from end token
sed -i -r 's/^\s+(EOF)/\1/' "$executable"
# Remove leading spaces from docstring (not tabs)
while read -r num line; do
if [[ -n $line ]]; then
sed -i -r "$num s/^ +(.+)/\1/" "$executable"
fi
done <<EOF
$(awk -v lines="$(wc -l "$executable" | awk '{print $1}')" '{\
if (/^EOF/ || /<<- *EOF/) exit; \
if (/<< *EOF/) lines = NR; \
if (NR > lines) print NR, $0; \
}' "$executable")
EOF
The limitation I have with this is that I have to use EOF
, but this could be solved with an environment variable, e.g. HEREDOC_TOKEN=CUSTOM
.
Also, I haven't thought about \EOF
and 'EOF'
yet. There are probably other possible forms that I haven't covered, but I think this is a good starting point.
from bashly.
Sorry for the duplication.
It's ok.
Maybe it's just me, but I find both existing solutions rather clumsy
Not just you. I agree the workarounds are clumsy. But so is the implementation of heredoc in bash itself.
In other languages, there is a way to keep the EOF marker indented.
I am sure there is a way to make bashly more heredoc-friendly, I just haven't found it yet... :)
from bashly.
Not just you. I agree the workarounds are clumsy. But so is the implementation of heredoc in bash itself. In other languages, there is a way to keep the EOF marker indented.
Totally agree with you. Bash is just bad in so many ways :)
I will close this for now as this is a known issue with existing workarounds.
Thanks again for your help.
Best,
Dušan
from bashly.
I have made heredoc work naturally, without the need to do anything (#496). bashly will no longer apply additional indentation to the strings inside a heredoc block.
This is available in the edge version, if anyone wishes to test it.
from bashly.
Related Issues (20)
- Add key-value parameters HOT 6
- Add support for recursive --help HOT 6
- Build multi-arch docker HOT 2
- Default argument is not validated HOT 6
- Flag arguments with a single hyphen prefix are not working HOT 1
- bash completions can't work properly when the position args used 'allowed:' HOT 3
- Repeatable args don't escape quotation marks HOT 12
- Add option to disable splitting of flags with value for catch_all arguments HOT 9
- Lifecycle for temporary directories HOT 1
- Heredoc indentation issue in bashly generated functions. HOT 9
- The Flag value is detected as an Argument HOT 2
- Showing an additional usage line for forced commands HOT 3
- Is it possible to have no command and take arguments from user ? HOT 3
- Shellcheck failure on repeatable argument with allowed values HOT 3
- Default command is not triggered when only flags are provided HOT 1
- using bashly without ruby or docker HOT 3
- Bashly generated script fails shellcheck rule SC2059 HOT 11
- Allow semi-private commands HOT 5
- THIS_SCRIPT variable HOT 4
- Bug in ini library HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bashly.