LoginSignup
0
1

More than 3 years have passed since last update.

Pythonのargparseで「'required' is an invalid argument for positionals」が出たら、requiredを消せばいい

Last updated at Posted at 2020-08-10

ドキュメントが少し分かりづらかったのでメモ。

起動時の引数を便利に扱えるargparseモジュール

Pythonには、起動時の引数を便利に扱えるargparseモジュールがあります。

位置引数とオプション引数

argparseで扱う引数には以下の2種類があります。

add_argument()メソッド

どんな引数を指定できるかをadd_argument()メソッドで指定できますが、位置引数が必須だからと

argument.add_argument(
    'infile1',
    required=True,
    help='input file'
)

のように書いてしまうと、実行時に「'required' is an invalid argument for positionals」とエラーになり実行できません。

required」はオプション引数のみに指定できる

required」はオプション引数に対する指定なため、位置引数に指定するとエラーになります。
もともと位置引数は必須なため、該当オプションの「required=True」を消せば、想定した動作になります。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1