I also posted to the github gist, this the sed command here is not cross-plataform friendly. You can accomplish the same thing with an awk command though: awk '/^###/' "$0"
Help Message for Shell Scripts
21–30 of 129 posts
Re: Help Message for Shell Scripts
#22Re: Help Message for Shell Scripts
#23Earlier quoted context omitted.
Yeah, "man sed" on my machine says, "This is a GNU extension." You could do the same thing with awk instead: awk '{ if (sub("^### ?", "")) { print; } else { exit; } }'
Or sed -rn 's/^### ?//p'
sed -n '/^### /p'
I believe? (equivalent to grep).Then eg:
sed -nr '/^### /s/^.{4}(.*)/\1/p'
(or without the redundant addressing, just:) sed -nr 's/^### (.*)/\1/p'Re: Help Message for Shell Scripts
#24You can also use a "here document" help() { cat Options: Input file to read. Output file to write. Use '-' for stdout. -h Show this message. EOH } If the indentation bugs you, you can use a simpler sed trick to remove leading space so that you can indent it as desired: help() { sed -e 's/ //' Options: Input file to read. Output file to write. Use '-' for stdout. -h Show this message. EOH }
You can also use add a hyphen ( https://linuxhint.com/bash-heredoc-tutorial/
Re: Help Message for Shell Scripts
#25 help() { cat $0 }
"May the source be with you." : )Re: Help Message for Shell Scripts
#26Earlier quoted context omitted.
> https://docs.python.org/3/library/argparse.html is great Argparse is okay (and being in stdlib makes it always-available), but it's no click. https://click.palletsprojects.com/en/7.x/
Thumbs up for 'Click'. I used it for a project once, and I was really happy with it. Easy to use, good docs. Would use it again.
Re: Help Message for Shell Scripts
#27Re: Help Message for Shell Scripts
#28Re: Help Message for Shell Scripts
#29I like the idea of combining the header with the help documentation to reduce the number of areas to maintain in smaller scripts. For larger scripts though, I think I'd still prefer to have a separate function, so that the help documentation doesn't overwhelm the initial viewing of the actual code. I also like to feed a heredoc directly into man, which allows you to achieve nicer formatting for the help documentation…
> so that the help documentation doesn't overwhelm the initial viewing of the actual code. That is a very strange argument to me. You find that more cumbersome than jumping around to random functions?
By having the help documentation in a function in the middle or towards the end of the file, I don't have to page down through the help documentation to get to the code that is actually doing things. If I'm really interested in the help documentation, then I'd prefer to look at the nicely formatted version output by the script ( --help or whatever) rather than looking in the actual script code anyway.
Admittedly, this may be more of a subjective personal preference item.
Re: Help Message for Shell Scripts
#30Handling of arguments is one of the reasons I reach for Python or Powershell instead of a bash script when writing my own stuff. https://docs.python.org/3/library/argparse.html is great. Powershell has the Param keyword that functions like argparse in Python https://docs.microsoft.com/en-us/powershell/module/microsoft...
A great option when you're stuck with an old crusty script that you don't want to completely rewrite in python, but do want to clean up enough so that you can call it with `-h` and remember how to use it a few months in the future.
Unfortunately, this won't help you if you're on embedded where python isn't in the base system.