I’m working on more bash completions. This time for some of the command line tools Apple provides for sysadmins.
I decided there had to be a way to get a list of the options from the
man page for a tool. After all they are all in there.
So I built a command line piece by piece. As an example let’s get a list of the options (with some caveats) for the tool
pkgbuild. We start with
man pkgbuild | col -b , the
col -b step takes out the special characters
man uses to show bold on screen. Now find all lines containing
grep, I liked
grep -e '--'. If you have a look at the output of that we are getting close.
Next I decided to use
sed to do a find and replace for the option itself. After some playing around I ended up with
sed -e 's#.*\(--[a-zA-Z-]*\).*$#\1#' An important note for young players, it did take some time and a few tries to get that substitution just right. Don’t be afraid and remember Google (and Stack Exchange) are your friends.
First, I should point out an old Unix hand’s trick. Most of the time you see
sed substitution commands using
/ as the separator but sed can use anything but
\ or newline – it uses the first character it sees after the ‘s’. I usually use
# as it makes the
\ used for special characters easier to spot and the string easier to read.