Help Message for Shell Scripts
samizdat.dev
Help Message for Shell Scripts
1–10 of 129 posts
Re: Help Message for Shell Scripts
#2When I write a shell script, I often write a help function if it's not a totally trivial script, but there's no need for this cryptic sed expression, right? You can just call `echo` a few times and do it the obvious way. That works better for maintainability and if you put it at the top of the file then it's immediately visible when opening or using `head` on it.
Neat trick though - sed is super useful for all kinds of things. I had a co-worker who bound a keybinding to a sed one-liner that would automagically reconfigure some files that needed to be changed regularly. I ended up using that trick to allow for changing my terminal console colorscheme with a single command.
Re: Help Message for Shell Scripts
#3This seems like a neat sed trick, but I'm not sure that it's useful for this particular case? When I write a shell script, I often write a help function if it's not a totally trivial script, but there's no need for this cryptic sed expression, right? You can just call `echo` a few times and do it the obvious way. That works better for maintainability and if you put it at the top of the file then it's immediately visi…
But at any rate my typical usage() is generally along these lines (warning watch out for expansions):
usage()
{
cat - [-o|--optional-param]
Etc. Possibly referencing
default value: ${DEFAULT_VALUE}
EOF
}Re: Help Message for Shell Scripts
#4% sed -rn 's/^### ?//;T;p' testfile
sed: 1: "s/^### ?//;T;p": invalid command code T
Looks like it might need GNU Sed or something. But honestly if I want to read the top of the file less works just as well.
Re: Help Message for Shell Scripts
#5 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
#6Hmm: % sed -rn 's/^### ?//;T;p' testfile sed: 1: "s/^### ?//;T;p": invalid command code T Looks like it might need GNU Sed or something. But honestly if I want to read the top of the file less works just as well.
You could do the same thing with awk instead:
awk '{ if (sub("^### ?", "")) { print; } else { exit; } }'Re: Help Message for Shell Scripts
#7https://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...
Re: Help Message for Shell Scripts
#8You 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
#9Hmm: % sed -rn 's/^### ?//;T;p' testfile sed: 1: "s/^### ?//;T;p": invalid command code T Looks like it might need GNU Sed or something. But honestly if I want to read the top of the file less works just as well.
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; } }'
sed -rn 's/^### ?//p'Re: Help Message for Shell Scripts
#10Handling 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...