Watch videos with subtitles in your language, upload your videos, create your own subtitles! Click here to learn more on "how to Dotsub"

What are Drush Commands?

0 (0 Likes / 0 Dislikes)
[What are Drush Commands?] [Coding for Drush, Chapter 2 with Joe Shindelar] In this lesson, I'm going to take a look at what, exactly, a Drush command is. In order to demonstrate this better, we're going to take a look at the command that we hope to write as the result of the next couple of lessons. What we're going to do is run a couple of Drush commands, and talk about the difference between arguments and options when specifying a command. Then we're going to take a look at how you can get help for a command, so you can learn about what it does and what arguments and options it has. In addition to that, we're also going to take a look at where Drush looks for commands that we run, and how, when we run the Drush command, it gives us a list of all the things that it can do and where it finds those things. So let's go ahead and get started running a couple of commands, and just get a general, better understanding about what we're trying to build. In this series of videos, we're assuming that you already understand the basics of Drush and how to run basic Drush commands, how to use your terminal, and how all of those pieces interact with each other. If you're not familiar with those things, I recommend you check out the "Getting Started With Drush" series in order to learn some of those pieces first. Throughout this lesson and the next couple, we're going to focus on writing our own Drush commands. Before we do that, I'd like to review what a Drush command is, and what they're capable of doing. So what I'm going to do is show you is the end result of what we're going to produce over the series of lessons that we're working through now. We're going to start with the Databasics Module, which we've used in a couple of other Drupalize.me video series. And in this series, we're going to add some Drush commands to the Databasics Module. If you remember, the Databasics Module allows me to view a site, and it keeps track of who viewed each particular node on the site and how many times they viewed it. So right now, I've got a demo site set up with a handful of nodes created. I've got a handful of random accounts; I've also got the Databasics Module and the Views Module installed. I can go to my account and click on the databasics tab. You'll notice that it' empty. We're going to work on writing a Drush command that allows me to generate a bunch of random content so that I can populate the databasic's database table so that when I'm building views or that kind of thing, I actually have some data to work with. In my terminal, you can see when I type the Drush command, by default, if I just type the word Drush and hit enter, Drush will output a list of all of the available commands. Commands are kind of like little modules or plugins for Drush. Each one is a distinct task that you would like to ask Drush to do. Commands can take arguments; so you could say, for my databasics content, command here, which is the one that we're actually going to write. I can specify arguments, which are the number of nodes, or records, that I'd like to create in the database. I can also specify options, which are optional flags to modify how a particular command works. So here we've got our databasics content command. We've also got an alias for that command of dbgenc. I can get more help about any command in Drush by typing Drush help and then the command name or alias. So I type dbgenc. I can see the help outputted by Drush for that command. It tells me that this command will create dummy content for our table, that it takes 1 argument, and that argument is a maximum number of records to add. It also takes 2 different options: a - - purge option and a count range. And you can see over here what those options do. This is true of any command in Drush; if I was to type help for any command, it will give me additional information about what that command's capable of, kind of like man pages for Drush commands. This is the help for the dl or pm-download command. Much more extensive than ours but it also does a lot more than our command will. This will be pretty basic to start out. So if I view the help again for the dbgenc command, I see that it takes arguments. You'll also see that if I just try to run this command, like so, without passing in an argument, it's going to spit out an error. That's because in Drush arguments are required. If a command takes arguments, it won't work unless I pass in an argument. Options, however, are optional, and are not required. They're just used to modify the default behavior of the command. So I do need to specify the number of records to generate. I'm just going to say, generate a hundred. It did that; it took my hundred argument; it generated a bunch of content; it says everything is okay. If I switch over to my site and refresh, you should see there's now just a bunch of random data there that's useful for me to test and debug these views for the Databasics Module. Typing help for the dbgenc command again, I see that there's some additional options, like this - - purge flag, which says, "Delete all the existing records before generating new ones." So let's try running the command a second time. This time we'll add that option. When I click enter this time, it says it's deleted all 109 records, and it's generated some new ones. So now, it's performed the operation that this command was written to do, but I've also modified it slightly by passing in this additional option. Another thing that you commonly see with commands is the command halting and asking for user input. You can see this if you use the dl command with Drush. So I can say drush dl, and if I say a project name, so we'll just say, features, as an example. If I pass in the - - select option here, Drush does some processing. It runs this command which goes out and looks up some information from Drupal.org, and then it halts processing, and it asks me to input some information before it continues this. So one thing the commands can do is halt until it receives user input. In this case, I'll just say 0 for cancel. Another feature that a lot of Drush commands provides is a confirmation. So when I type a command, it'll halt processing and say I need to answer yes or no if I want to continue. An example of that is the disable command. So I could do Drush, d-i-s will disable the watchdog. And if I say yes, it's going to prompt me, and it says, "The following extension will be disabled; are you sure you want to continue?" And I can answer yes or no. Yes, I will continue the command. No and it will abort that command. This is the common pattern, as well. So we'll take a look at how we can do that in our own commands. It's also good to know how Drush finds and runs commands. Drush commands can be located in a number of different places. There's the commands that come with Drush by default. When I type Drush and just get a list here of commands that are available on my system, a lot of these are those that are provided by Drush Core. All these user commands, the SQL commands, those are all just part of the Drush package. Because I'm in the root of an installed Drupal 7 site that also has the Views Module installed and enabled, I get these additional commands for Drush that are provided by the Views Module. Those commands live inside of a Drush command file inside of the Views Module's directory. Same with our databasics-content command. The command that we're going to create will be part of the Databasics Module. Sometimes, though, you want commands that can be executed on any site and don't really live with a particular module. An example of that is this registry rebuild command. This is a command that I may want to use on any number of different sites, but I don't want to have to download the module on every site that I want to use it. So I can place these commands inside of my home folder in a directory that starts with a dot. So if I change to my home folder, I can get a list of the files inside homefolder.drush directory. There's a bunch of Drush command files in there. These commands are available anywhere that I can run Drush, versus the ones that are bundled with a module, which are only available when that module is installed for a particular site. So that's a quick overview of commands and the kinds of things that we're going to attempt to do throughout this series.

Video Details

Duration: 8 minutes and 32 seconds
Country: United States
Language: English
License: All rights reserved
Genre: None
Views: 77
Posted by: drupalizeme on Dec 3, 2012

In this lesson, we’re going to talk about what a drush command is exactly. We're assuming you’re at least somewhat familiar with drush and running commands, and general interaction with the terminal. In order to demonstrate both how drush commands work, and to give a better idea of what we’ll be working towards, we're going to take a look at the drush command we’re going to write for the databasics module and explain a bit about arguments and options. We’re going to


Run a couple of drush commands and see the difference between arguments and options in action.
Talk about the command that we’re going to learn to write in this series
See how to get a list of all the commands drush knows about and the help text for those commands
Learn about the pat

Caption and Translate

    Sign In/Register for Dotsub to translate this video.