다음과 같은 도움말 섹션을 만들었습니다.
-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
다음은 도움말 관련 항목을 한 번에 하나씩 인쇄하는 데 사용할 수 있는 기능입니다. -m
질문의 예와 긴 도움말 문자열을 사용했습니다 . 이 함수는 첫 번째 열의 너비를 10으로 하드코딩한 다음 두 번째 열의 남은 공간을 계산합니다.
#!/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
두 번째 줄의 들여쓰기를 유지합니다 .
$ 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))