この
sampleCode.py
に引数を渡したいファイル。
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--num_epochs', '-n', default=2, type=int)
parser.add_argument('--directory', default='/some/string/')
parser.add_argument('--exclude_hydro', default=False, action='store_true')
args = parser.parse_args()
print(args.num_epochs) # 2
print(args.exclude_hydro) # False
次のコマンドは、intおよびstring引数に対して機能しますが、ブール値に対しては機能しません。
$python3 sampleCode.py -n3 #args.num_epochs=3
$python3 sampleCode.py --num_epochs 3 #args.num_epochs=3
$python3 sampleCode.py --directory /new/string #args.directory = /new/string
$python3 sampleCode.py --exclude_hydro True #error
ブール値の引数を渡すにはどうすればよいですか? 私は
type=bool
を試しました
.add_argument()
のパラメーターとしてしかし、それは機能しません。
parser.add_argument('--exclude-hydro', const=True)
動作するはずです。help="<help_text>"
も定義する必要があることに注意してください ユーザーが引数なしでアプリケーションを呼び出すと、各引数の目的を説明するヘルプテキストが表示されます。parser.add_argument('--exclude-hydro', const=True, help="<help_text>")