次のヘルプセクションを作成しました。
-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
持ちたい
-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.
2つの列が欲しいと指定しますか?
答え1
以下は、ヘルプ関連項目を一度に1つずつ印刷するために使用できる機能です。-m
質問の例と長いヘルプ文字列を使用しました。この関数は、最初の列の幅を10にハードコードし、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"
50列画面の出力例:
-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
80列画面で:
-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
130列画面で:
-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
150列画面で:
-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
答え2
これは必須ではありませんが、パラメータ名と説明が同じ行にない場合は簡単な解決策です。fmt
2行目のインデントを維持します。
$ 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
答え3
このソリューションは、次にも有効です。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))