0

I have create a help section that have the following display

-m         This is a very very very very very very very very very very
very very very very very very very very very very long sentence

I would like to have

-m         This is a very very very very very very very very very very
           very very very very very very very very very very long 
           sentence.

Specify that I want to have 2 columns?

4
  • do you want to edit the (shell script?) to change how it's wrapping, or post-process the output afterwards? – Jeff Schaller Oct 10 '16 at 18:25
  • Change how it's wrapping. What is the most used syntax for flag description in help sections (cmd --help)? – user123456 Oct 10 '16 at 18:32
  • Are you looking to enforce the wrapping at a fixed width (~60 chars), or dynamically based on the terminal width? – Jeff Schaller Oct 10 '16 at 20:01
  • What stays constant is the distance between the flag and the description column. It may depend on the terminal width. – user123456 Oct 10 '16 at 20:07
1

Here's a function I put together that you could use to print help-related items, one at a time. I used the example of -m and the long help string from the question. The function hard-codes a width of 10 for the first column, then computes the remaining space for column 2.

#!/bin/bash

function printhelp() (
  c1width=10
  cols=$(tput cols)
  c2width=$((cols - c1width - 1))
  readarray -t lines < <(printf '%s' "$2" | fmt -w $c2width)
  for((line=0; line < ${#lines[*]}; line++))
  do
    if [ $line -eq 0 ]
    then
      col1="$1"
    else
      col1=" "
    fi
    printf "%-${c1width}s %-${c2width}s\n" "$col1" "${lines[line]}"
  done
)

optf='-m'
optd='This is a very very very very very very very very very very very very very very very very very very very very long sentence'
printhelp "$optf" "$optd"

Sample output on a 50-column screen:

-m         This is a very very very very very
           very very very very very very very
           very very very very very very very
           very long sentence

and on an 80-column screen:

-m         This is a very very very very very very very very very very very
           very very very very very very very very very long sentence

and on a 130-column screen:

-m         This is a very very very very very very very very very very very very very very very very very very very very
           long sentence

and on a 150-column screen:

-m         This is a very very very very very very very very very very very very very very very very very very very very long sentence
3
  • I'm not a zsh user but I've heard good things about it from smart people on here, so I'd be really surprised if it can't make an array from multi-line output. – Jeff Schaller Oct 11 '16 at 15:55
  • How can I rewrite the script without using readarray for zsh users to be able to run the function. – user123456 Oct 11 '16 at 19:21
  • take a look at unix.stackexchange.com/a/29748/117549 – Jeff Schaller Oct 11 '16 at 19:59
1

This is not exactly what you required, but an easy workaround if you do not have the parameter name and the description in the same line. With fmt the indentation of the second line is kept:

$ FOO="-m\n\tThis is a very very very very very very very very very very very very very very very very very very very very long sentence"

$ echo -e $FOO
-m
    This is a very very very very very very very very very very very very very very very very very very very very long sentence

$ echo -e $FOO | fmt 
-m
    This is a very very very very very very very very very very very
    very very very very very very very very very long sentence
0

This solution is also valid for zsh

FLAG="-m VARIABLE"
DESCRIPTION="This is a very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long sentence"
DISTANCE_FLAG=3

WIDTH1=$(echo "$FLAG" | wc -c ) 
WIDTH2=$(($(tput cols)-$DISTANCE_FLAG -$WIDTH1 ))

paste <(echo $FLAG ) <(echo $DESCRIPTION | fold -sw $(( $(tput cols) -     $DISTANCE_FLAG -$WIDTH1     )) )| pr -t -e$(($DISTANCE_FLAG+$WIDTH1))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.