Help Message for Shell Scripts
31–40 of 129 posts
Re: Help Message for Shell Scripts
#32Handling 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...
> 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/
Re: Help Message for Shell Scripts
#33You 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 }
Re: Help Message for Shell Scripts
#34Pretty sure this won't work if the script is called via $PATH
x=$(command -v $0 2>/dev/null)
sed -rn 's/^### ?//;T;p' $x
Personally I would not use the author's chosen sed commands. exec sed -n '/^###/p' $x
would work fine.Re: Help Message for Shell Scripts
#35I 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"
Re: Help Message for Shell Scripts
#36You 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
#37Re: Help Message for Shell Scripts
#38Re: Help Message for Shell Scripts
#39I 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…
Re: Help Message for Shell Scripts
#40Handling 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...