Hacker News new | past | comments | ask | show | jobs | submit login

Initially 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.



Yup. 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/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


> 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




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: