2013-10-22 19 views
10

bashスクリプトのヘルプテキストを出力して、列の行を適切に並べるには良い方法はありますか?bashスクリプト "使用法"出力書式

のようなもの:私はこのためcatを使用したい

Usage: mycommand [options] 

    -h| --help   this is some help text. 
         this is more help text. 
    -1|--first-option this is my first option 
    -2|--second-option this is my second option 

答えて

21

usage.sh

#!/bin/bash 

cat <<EOF 
Usage: $0 [options] 

-h| --help   this is some help text. 
        this is more help text. 
-1|--first-option this is my first option 
-2|--second-option this is my second option 
EOF 

この意志出力:

Usage: usage.sh [options] 

-h| --help   this is some help text. 
        this is more help text. 
-1|--first-option this is my first option 
-2|--second-option this is my second option 
+0

ええ、私はこれを既に知っていました。私はちょうどソースファイルの書式が気に入らないが、たぶん私は "使用"機能を実行し、私のスイッチのケースでそれを呼んでそこから行くだろう。私は時々bashでフォーマットを嫌う。 – flamusdiu

2

Heredocsにはタブインデント付きオプションもあります。これにより、コードの各行に任意の数のタブを付けることができます。これらのタブは、出力上で "食べられる"ようになり、出力を正当化します。末尾の 'EOF'(この例では)は完全にインデントされていなければならない(MUST)ことに注意してください。 'EOF'はタブの字下げができません。

タブ文字をスペースに変換するエディタ(たとえば、「vi」オプションはタブをスペースに変換する「expandtab」)に注意してください。残念ながら、複数のスペースはタブのように「食べていない」わけではありません。コードの書式設定のために 'expandtab'(または同様のオプション)を使用すると、heredocタブのインデントメソッドは役に立ちそうにありません。

以下の例では、 "< < - "は、タブインデントを尊重するheredocの構文です。

例:「タブ」は「猫」の前、およびそれ以降の行であることを

cat <<-EOF 
    usage.sh [options] 

    -h| --help   this is some help text. 
         this is more help text. 
    -1|--first-option this is my first option 
    -2|--second-option this is my second option 
EOF 

注 - HTMLの書式ここでは、例をカット&ペーストのn-できるようにするつもりはない可能性があります。

終了する「EOF」はどのように左揃えになっているか注意してください。

関連する問題