4 ms·
I do really like the implementation of help text: function bocker_help() { #HELP Display this message:\nBOCKER help sed -n "s/^.*#HELP\\s//p;" < "$1"
by phinnaeus 7y ago
I do really like the implementation of help text:
function bocker_help() { #HELP Display this message:\nBOCKER help
sed -n "s/^.*#HELP\\s//p;" < "$1" | sed "s/\\\\n/\n\t/g;s/$/\n/;s!BOCKER!${1/!/\\!}!g"
}
- _def 7y agoIt'd be even better if the help texts would be readable in the source, but that would cost some lines for sure
- phinnaeus 7y agoDo you just mean without the escaped newline characters? Because the fact that they serve as function comments is exactly what I found to be novel about it.
- _def 7y agoI like the idea too but just the thought of needing to go through those text blocks to edit them makes me sad
- vfinn 7y agoWhat do you mean? They are in the source.
- t0mek 7y agoInitially I thought that bash supports reflection and is able to get the function contents, including comments. But this function scans its own script file ($1), looking for the comments starting with #HELP and formatting them. This way the usage info can live near the functions implementing sub-commands.
- andreineculau 7y agoYup. It is a nice trick that you might find more often in Makefile. I have a make target that does this consistently in all the makefiles I write: https://github.com/andreineculau/core.inc.mk/blob/master/target.help.inc.mk https://github.com/andreineculau/core.inc.mk/blob/master/tar... target1: ## Do this target2: target3: target2 ## Do that would be nicely formatted as the output of `make help` into Available targets: target1 Do this target2 Do that and if you want to see all targets along with their origin file, even those without help messages, type `make help-all` to render Available targets: target1 path/to/Makefile Do this target2 path/to/Makefile target3 path/to/Makefile Do that
- einpoklum 7y ago> Initially I thought that bash supports reflection ... But this function scans its own script file That certainly counts as reflection in the context of shell scripting... :-P