Magento 2 - Add Arguments to Command Line

I wrote these tutorials for myself in future when I forget for the next steps.
Search for a command to run...

I wrote these tutorials for myself in future when I forget for the next steps.
No comments yet. Be the first to comment.
When you provision an Ubuntu instance on Oracle Cloud, you'll notice something different from standard Ubuntu installations: the system uses iptables instead of ufw for firewall management. This disti

When developing web applications locally, HTTPS is often overlooked. Yet, modern browsers and APIs increasingly require secure connections—even in development. That’s where mkcert comes in: a simple t

Testing the checkout success page in Magento 2 can be frustrating. By default, once an order is placed, Magento clears the quote session, meaning you can’t simply refresh or revisit the success page without going through the entire checkout process a...

Magento Commerce (EE) offers powerful features, but for many teams, Open Source (CE) is leaner, lighter, and more sustainable. If you're migrating from EE to CE, the Opengento downgrade tool provides a clean, scriptable way to remove proprietary modu...

If you’ve ever checked your Apache error logs and seen something like this: Code [core:error] (13)Permission denied: access to /robots.txt denied (filesystem path '/home/username/sites') because search permissions are missing on a component of the pa...

I'm creating an article to add arguments, first of all you need to create custom command line if you want to follow this article.
Once you already have custom command line, what you need to do is configuring option(s) onto configure method.
<?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!
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
bin/magento fiko:welcome -h
Symfony\Component\Console\Input\InputOption is required class to create the argument.
.....
use Symfony\Component\Console\Input\InputOption;
.....
.....
const NAME = 'name';
.....
$options = [
new InputOption(
self::NAME,
'-a',
InputOption::VALUE_OPTIONAL,
'Your Name'
),
];
.....
const NAME = 'name'; 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.
$options = [ 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.
.....
$options = [
new InputOption(
self::NAME,
'-a',
InputOption::VALUE_OPTIONAL,
'Your Name'
),
new InputOption(
self::SURNAME,
'-s',
InputOption::VALUE_OPTIONAL,
'Your Surname'
),
.....
];
.....
self::NAME name of the option / argument
'-a', abbreviation of the argument, in case calling --name Borizqy is too long, we can use -aBorizqy or -a Borizqy
InputOption::VALUE_OPTIONAL is the type of the argument, at the moment there are
InputOption::VALUE_OPTIONAL The option may or may not have a value (e.g. --yell or --yell=loud), or we can ignore this type of argument.
InputOption::VALUE_REQUIRED A value must be passed when the option is used (e.g. --iterations=5 or -i5).
InputOption::VALUE_NONE Do not accept input for the option (e.g. --yell). This is the default behavior of options.
InputOption::VALUE_IS_ARRAY The option accepts multiple values (e.g. --dir=/foo --dir=/bar).
'Your Name' is Description of the argument
Once options already prepared, then we need to define or register them to magento by adding setDefinition inside the configure method
.....
protected function configure()
{
.....
$this->setDefinition($options);
.....
}
.....
to fetch the argument we pass to the command line, use this line of code.
.....
$input->getOption(self::NAME)
.....
Thanks :)