Drush command structure

Drush ships with a set of grouped commands to perform different tasks. If you are fluent at executing commands in the terminal, you can skip this section and start exploring the details of each Drush command in the next chapter. If not, you should understand what arguments and options are to a command and how they affect its behavior.

Executing a command

Let's start with a very simple command such as core-status, which prints configuration information about Drush and, if applicable, a Drupal site. If executed at the root of a Drupal directory with its database configured at sites/default/settings.php, the following command would return:


$drush core-status 
Drupal version : 	7.4 
Site URI : 		http://drupal7.localhost 
Database driver : 	mysql 
Database hostname : 	localhost 
Database username : 	root 
Database name : 	drupal7 
Database : 		Connected 
Drupal bootstrap : 	Successful 
Drupal user : 		Anonymous 
Default theme : 		bartik 
Administration theme : 	seven 
PHP configuration : 	/etc/php5/apache2/php.ini 
Drush version : 		4.5 
Drush configuration : 
Drush alias files : 
Drupal root : 		/home/juampy/myDrupalSite 
Site path : 		sites/default 
File directory path : 	sites/default/files

This output is informing us the main configuration of our site and Drush, which is its default behavior. Now, we can print its help information to find out that it can actually do more than that:

$ drush help core-status 
Provides a birds-eye view of the current Drupal installation, if any.
Examples: drush status version 	Show all status lines that 
					contain version information.
drush status --pipe 			A list key=value items 
					separatedby line breaks.
drush status drush-version--pipe 	Emit just the drush version 
					with no label.
Arguments: item 			Optional. The status item 
					line(s) to display.
Options: --show-passwords 		Show database password.
Topics: 
   docs-readme 				README.txt
Aliases: status, st

As we can see, the core-status command accepts arguments and options when being called. We will now see how to use them.

Providing arguments to a command

An argument is a piece of information that acts as input data for a command. They are typed next to the command name and separated by spaces.

The help information of the core-status command (type drush help core-status to see it again) says that we can specify the items which we want it to print. Therefore, if we need to print just the items containing version in the item name, we could do the following:

$ drush core-status version Drupal version : 7.10 
Drush version : 4.5

You can try and change version by something else or even add more parameters after it, so the command will print items containing them as well. If we wanted version and database information to be printed, the following command would do it:

$ drush core-status version database 
Drupal version : 7.10 
Database driver : mysql 
Database hostname : localhost 
Database username : root 
Database name : drupal7db 
Database : Connected 
Drush version : 4.5

You can give any number of arguments to a command. Beware that some commands expect the arguments to be given in a certain order. For example, the command variable-set expects that the variable name to be set is the first argument and its new value is the second argument.

Hence, the following example sets the variable site-name with the value My Drupal site:

$ drush variable-set site_name "My Drupal site"

Modifying a command's behavior through options

Drush commands accept options, which modify their default behavior. If, for example, we wanted to list the database connection details of a Drupal site, we would do the following:

$ cd /path/to/drupal/root/ 
$ drush core-status --show-passwords database
Database driver : mysql 
Database hostname : localhost 
Database username : root 
Database name : drupal7site 
Database password : drupal7sitePassword 
Database : Connected

The option --show-passwords is telling Drush that we want to see the database password of the site where we currently are. This option is needed because, by default, the status command does not show database passwords.

Here is a full command that prints version and database information with database passwords in a key=value format. Its full syntax is detailed as follows:


$ drush core-status --show-passwords --pipe version database 
	command      options                               arguments

As you can see, options are given after the command name and arguments are given at the end. You can actually change the order and even mix them, but for clarity we will follow the given structure.

In order to read the description and available arguments and options for a command, use drush help and append to it the command name as an argument, such as:

$ drush help core-status

In the previous example, core-status is not a command but an argument for the help command telling it that we want to see help information about the core-status command.

Most of the Drush options have a short and long format and they may accept a value too. You can see if an option has a short format in the command help. As an example, if we wanted to tell a Drush command the URL of our site, we could do it in two ways. Here is the short one:

$ drush cache-clear -l drupal7.localhost all

And this is the long one:

$ drush cache-clear --uri=drupal7.localhost all

In this book, we will use the long format as it makes clearer the difference between option values from arguments.

There are some options which are applicable to most Drush commands. You can see a list of these using the following command:

$ drush topic core-global-options

Command aliases

Most of the Drush commands have a shorter alias to help us type less. You can see them between parenthesis next to each command name. Therefore, the following command $ drush status is a shortcut for $ drush core-status.

For clarity, we will not use command aliases in this book, but you should learn and use them. Here is an example showing a portion of the help information of the core-status command where its aliases are listed in parenthesis.

$ drush help core-status
 ... core-status (status, st) Provides a birds-eye view of the current Drupal 
 installation, if any 
 ...

This means that the three following commands give the same result:

$ drush core-status 
$ drush status 
$ drush st

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.186.201