DEAR PEOPLE FROM THE FUTURE: Here's what we've figured out so far...

Welcome! This is a Q&A website for computer programmers and users alike, focused on helping fellow programmers and users. Read more

What are you stuck on? Ask a question and hopefully somebody will be able to help you out!
+5 votes

Other unix/posix-like shells are fine too.

Alternately, what are the best practices for writing a usage function for bash? I typically try to get mine to look like something that python argparse would produce with ./script_name --help. In the past I've even mocked my bash script up in python.

by
+1

usage function = help function?

+1

Just curious but why use Bash then? :) Python is preinstalled in pretty much every distro except the most minimal.

0

Yes, I mean usage function as in a help function.

This question is mostly for existing code at my day job. I almost always encourage using python, otherwise. I've been thinking a few different things. Maybe there's a tool that works with getopts. I found this after I posted the question, but I haven't had a chance to try it out yet:

https://github.com/Anvil/bash-argsparse

Or maybe there exists a tool I could get non-programmers to write them in, maybe I could use that, or use pandoc after that to generate the usage prompt and put that in a function.

1 Answer

0 votes

i dont know of any common library for this use-case - BASH hackers tend to roll-their-own helpers - parabola has helpers which generate typically formatted USAGE/--help messages for parabola's tools - feel free to adapt it to your use-case

https://git.parabola.nu/packages/libretools.git/tree/src/lib/messages.sh

it is used like this:

usage()
{
  echo "USAGE: $(basename $0) [ -h | -v ]"
  echo "This script does something awesome!"
  echo
  prose "While some scripts are less than awesome,
         this one has awesome-foo powers, which infuse it with awesomeness,
         providing a noticeably more awesome user-experience when foo-ing."
  echo "For example:"
  bullet "awesome-foobar is 30% more awesome
          than the standard foo+bar combination"
  bullet "plays soothing 8-bit chip-tunes at random intervals"
  bullet "it can cure your asthma too"
  echo
  echo "It also displays helpful error messages such as:"
  bullet "You did it wrong, dummy!"
  bullet "RTFM!"
  bullet "Please file a bug report with subject: \"it doesn't work\""
  echo
  echo "Options:"
  flag '-h' 'Show this message'
  flag '-v' 'Be verbose'
}
by
edited by
Contributions licensed under CC0
...