

Laravel 初心者の質問です．

## 現象

次のような簡単なLaravelのコマンドラインプログラムを作ってみたのですが、

ElasticIndexMaker.php

[code]

    <?php
    
    namespace App\Console\Commands\User;
    
    use Illuminate\Console\Command;
    
    class ElasticIndexMaker extends Command
    {
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
        //protected $signature = 'elasticindexmaker {index: Index specified by the target S3 directry structure starting first character with slash}';
        protected $signature = "elasticindexmaker {index: Index name}";
    
        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'Generate Elasticsearch index';
    
        /**
         * Create a new command instance.
         *
         * @return void
         */
        public function __construct()
        {
            parent::__construct();
        }
    
        /**
         * Execute the console command.
         *
         * @return int
         */
        public function handle()
        {
            $index = $this -> argument("index");
            $verboseLog = $this -> option("verbose");
    
            $this->info("Index: " . $index);
            if ($this->confirm("Are you sure to generate index by this parameter?")) {
            } else {
                return;
            }
            return 0;
        }
    }
    
[/code]

kernel.php

[code]

    <?php
    
    namespace App\Console;
    
    use Illuminate\Console\Scheduling\Schedule;
    use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
    
    class Kernel extends ConsoleKernel
    {
        /**
         * The Artisan commands provided by your application.
         *
         * @var array
         */
        protected $commands = [
            Commands\User\ElasticIndexMaker::class
        ];
    
        /**
         * Define the application's command schedule.
         *
         * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
         * @return void
         */
        protected function schedule(Schedule $schedule)
        {
            // $schedule->command('inspire')->hourly();
        }
    
        /**
         * Register the commands for the application.
         *
         * @return void
         */
        protected function commands()
        {
            $this->load(__DIR__.'/Commands');
    
            require base_path('routes/console.php');
        }
    }
    
[/code]

### 1\. -h だとパラメータ index は認識されているみたいです

[code]

    PS D:\My_Documents\proj\Elasticsearch\index-gen-laravel> php artisan elasticindexmaker -h     
    Description:
      Generate Elasticsearch index
    
    Usage:
      elasticindexmaker <index: Index name>
    
    Arguments:
      index: Index name     
    
    Options:
      -h, --help            Display help for the given command. When no command is given display help for the list command    
      -q, --quiet           Do not output any message
      -V, --version         Display this application version
          --ansi            Force ANSI output
          --no-ansi         Disable ANSI output
      -n, --no-interaction  Do not ask any interactive question
          --env[=ENV]       The environment the command should run under
      -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
    PS D:\My_Documents\proj\Elasticsearch\index-gen-laravel> 
    
[/code]

### 2\. 実際にパラメータを指定して動かすと例外になります．

ところが 実際にパラメータを指定して使ってみるとエラーで落ちてしまいます．（中で例外が起こっています．）

[code]

    PS D:\My_Documents\proj\Elasticsearch\index-gen-laravel> php artisan elasticindexmaker aa/bb
    
      The "index" argument does not exist.  
    
    PS D:\My_Documents\proj\Elasticsearch\index-gen-laravel>
    
[/code]

VSCodeでデバッグしてみると以下で引っかかっているようです．（画像見れるでしょうか？）何かパラメータの`index`が認識されていないみたいです．

[code]

    Exception has occurred.
    Symfony\Component\Console\Exception\RuntimeException: No arguments expected for "elasticindexmaker" command, got "aa/bb".
    
[/code]

[![例外発生個所](https://i.stack.imgur.com/3Ue7x.png)](https://i.stack.imgur.com/3Ue7x.png)

どこがまずいのか教示いただければ幸いです．

## 環境

Windows 10 + VSCodeでやっています．

以下composer.jsonからの抜粋です．

[code]

        "require": {
            "php": "^7.3|^8.0",
            "aws/aws-sdk-php": "^3.171",
            "aws/aws-sdk-php-laravel": "^3.6",
            "elasticsearch/elasticsearch": "^7.10",
            "fideloper/proxy": "^4.4",
            "fruitcake/laravel-cors": "^2.0",
            "guzzlehttp/guzzle": "^7.0.1",
            "laravel/framework": "^8.12",
            "laravel/tinker": "^2.5"
        },
    
[/code]

不足情報ありましたらお知らせください．

以上 よろしくお願いいたします．


CC BY-SA 4.0
2021-01-22T08:10:48.067
73496
2023-10-02T03:11:26.403
2021-01-22T08:24:21.343
9503
9503
question
laravel command-line
Laravelのコマンドラインプログラムでパラメータが認識されず落ちてしまいます．


失礼しました．自己RESです．本家Stackoverflowに聞いたら30秒で解決しました．`index`の後のスペース1文字の問題でした．

app/Console/Commands/User/ElasticIndexMaker.php （修正後）

[code]

    protected $signature = "elasticindexmaker {index : Index name}";
    
[/code]

実行画面

[![実行画面スナップショット](https://i.stack.imgur.com/BbeAb.png)](https://i.stack.imgur.com/BbeAb.png)


CC BY-SA 4.0
2021-01-22T14:45:47.000
73502
2021-01-22T14:45:47.000
9503
73496
answer