0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Artisanコマンドのargumentとoptionの指定方法と入力の相関

Last updated at Posted at 2020-12-20

先日仕事でLaravelのカスタムコンソールコマンドを書いてる時に、シグネチャのパラメータ処理が泥臭く感じてモジュール化できないかなーと思い、どんな時にどんな返り値になるのか調査したので、結果を残しておきます。

シグネチャ 入力 取得 結果
foo {key} foo - エラー
foo {key} foo value $this->argument('key'); "value"
foo {key?} foo $this->argument('key'); null
foo {key?} foo value $this->argument('key'); "value"
foo {key=default} foo $this->argument('key'); "default"
foo {key=default} foo value $this->argument('key'); "value"
foo {key*} foo - エラー
foo {key*} foo value $this->argument('key'); ["value"]
foo {--key} foo $this->option('key'); false
foo {--key} foo --key $this->option('key'); true
foo {--key} foo --key=value - エラー
foo {--key=} foo $this->option('key'); null
foo {--key=} foo --key $this->option('key'); null
foo {--key=} foo --key=valie $this->option('key'); "value"
foo {--key=default} foo $this->option('key'); "default"
foo {--key=default} foo --key $this->option('key'); null
foo {--key=default} foo --key=value $this->option('key'); "value"
foo {--key=*} foo $this->option('key'); []
foo {--key=*} foo --key $this->option('key'); [null]
foo {--key=*} foo --key=value $this->option('key'); ["value"]
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?