Live data from Hacker News

Help Message for Shell Scripts

samizdat.dev

21–30 of 129 posts

Re: Help Message for Shell Scripts

#23
post #9

Earlier 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'

Doesn't appear anyone has tried addressing before replacement - ie the simplest sed work-a-like - if you don't mind the leading ### is just:

  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

#24
post #8

You 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/

I've always avoided that for fear that my or someone else's editor will accidentally replace the tabs with space. Mixed is not a common configuration, these days.

Re: Help Message for Shell Scripts

#26
post #14

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

It’s fantastic and should be used by most CLI programs. Argparse is much faster and avoids having a dependency, so it does serve a purpose.

Re: Help Message for Shell Scripts

#28
post #8

Earlier quoted context omitted.

You can also use add a hyphen ( https://linuxhint.com/bash-heredoc-tutorial/

Oh, that's nice! And in spite of the labeling on that page, it doesn't seem to be a BASHism; it at least works in dash, too.

And ksh.

Re: Help Message for Shell Scripts

#29
post #19
post #17

I 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?

No, I think the readability of the help documentation itself is largely the same whether it is placed in the header or in a dedicated function. When I'm viewing the shell script code though, often I want to jump right in and see the actual code, not look at the help documentation.

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

#30
post #7

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

See also: https://github.com/nhoffman/argparse-bash

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.

Post reply on HN