Formata uma seção de ajuda para ter a descrição da bandeira em uma coluna

0

Eu criei uma seção de ajuda com a seguinte exibição

-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

Eu gostaria de ter

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

Especifique que eu quero ter 2 colunas?

    
por user123456 10.10.2016 / 19:26

3 respostas

1

Aqui está uma função que eu coloquei que você poderia usar para imprimir itens relacionados à ajuda, um de cada vez. Eu usei o exemplo de -m e a longa sequência de ajuda da pergunta. A função codifica com firmeza uma largura de 10 para a primeira coluna e calcula o espaço restante para a coluna 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"

Exemplo de saída em uma tela de 50 colunas:

-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

e em uma tela de 80 colunas:

-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

e em uma tela de 130 colunas:

-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

e em uma tela de 150 colunas:

-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
    
por 10.10.2016 / 22:42
1

Isso não é exatamente o que você exigia, mas uma solução simples se você não tiver o nome do parâmetro e a descrição na mesma linha. Com fmt o recuo da segunda linha é mantido:

$ 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
    
por 10.10.2016 / 22:34
0

Esta solução também é válida para 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))
    
por 12.10.2016 / 12:43