# Magento 2 - Add Arguments to Command Line

I'm creating an article to add arguments, first of all you need to create [custom command line](https://s.id/1vo5l) if you want to follow this article.

## Define Arguments

Once you already have custom command line, what you need to do is configuring option(s) onto `configure` method.

```php
<?php

namespace Fiko\Magento\Console;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class Welcome extends Command
{
    const NAME = 'name';

    protected function configure()
    {
        $options = [
            new InputOption(
                self::NAME,
                '-a',
                InputOption::VALUE_OPTIONAL,
                'Your Name'
            ),
        ];

        $this->setName('fiko:welcome');
        $this->setDescription('Demo description of welcoming someone!');
        $this->setDefinition($options);

        parent::configure();
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        if ($name = $input->getOption(self::NAME)) {
            $output->writeln("Welcome Home, {$name}!");
        } else {
            $output->writeln("Welcome Home!");
        }
    }
}
```

Then you can try to execute it!

```bash
bin/magento fiko:welcome -aBorizqy
OR
bin/magento fiko:welcome --name=Borizqy
```

of if you want to read the help or documentation, you can try

```bash
bin/magento fiko:welcome -h
```

## Code Breakdown

### Required Class

<mark>Symfony\Component\Console\Input\InputOption</mark> is required class to create the argument.

```php
.....
use Symfony\Component\Console\Input\InputOption;
.....
```

### Prepare Option(s)

```php
    .....
    const NAME = 'name';
        .....
        $options = [
            new InputOption(
                self::NAME,
                '-a',
                InputOption::VALUE_OPTIONAL,
                'Your Name'
            ),
        ];
        .....
```

* <mark>const NAME = 'name';</mark> is optional, I recommend to use it so we only need to call `self::NAME`, and if we want to change it, just the the value of this content without we need to change multiple lines.
    
* <mark>$options = [</mark> it's array of the options, you can define more than one option, in order to do that just define multiple InputOption inside this array.
    

```php
        .....
        $options = [
            new InputOption(
                self::NAME,
                '-a',
                InputOption::VALUE_OPTIONAL,
                'Your Name'
            ),
            new InputOption(
                self::SURNAME,
                '-s',
                InputOption::VALUE_OPTIONAL,
                'Your Surname'
            ),
            .....
        ];
        .....
```

* <mark>self::NAME</mark> name of the option / argument
    
* <mark>'-a'</mark>, abbreviation of the argument, in case calling `--name Borizqy` is too long, we can use `-aBorizqy` or `-a Borizqy`
    
* <mark>InputOption::VALUE_OPTIONAL</mark> is the type of the argument, at the moment there are
    
    * <mark>InputOption::VALUE_OPTIONAL</mark> The option may or may not have a value (e.g. --yell or --yell=loud), or we can ignore this type of argument.
        
    * <mark>InputOption::VALUE_REQUIRED</mark> A value must be passed when the option is used (e.g. --iterations=5 or -i5).
        
    * <mark>InputOption::VALUE_NONE</mark> Do not accept input for the option (e.g. --yell). This is the default behavior of options.
        
    * <mark>InputOption::VALUE_IS_ARRAY</mark> The option accepts multiple values (e.g. --dir=/foo --dir=/bar).
        
* <mark>'Your Name'</mark> is Description of the argument
    

### Define Option(s)

Once options already prepared, then we need to define or register them to magento by adding `setDefinition` inside the `configure` method

```php
    .....
    protected function configure()
    {
        .....
        $this->setDefinition($options);
        .....
    }
    .....
```

### Fetching or Calling Option(s)

to fetch the argument we pass to the command line, use this line of code.

```php
.....
$input->getOption(self::NAME)
.....
```

---

Thanks :)
