5 * Example drush command.
7 * To run this *fun* command, execute `sudo drush --include=./examples mmas`
8 * from within your drush directory.
10 * See `drush topic docs-commands` for more information about command authoring.
12 * You can copy this file to any of the following
13 * 1. A .drush folder in your HOME folder.
14 * 2. Anywhere in a folder tree below an active module on your site.
15 * 3. /usr/share/drush/commands (configurable)
16 * 4. In an arbitrary folder specified with the --include option.
17 * 5. Drupal's /drush or /sites/all/drush folders, or in the /drush
18 * folder in the directory above the Drupal root.
22 * Implements hook_drush_command().
24 * In this hook, you specify which commands your
25 * drush module makes available, what it does and
28 * Notice how this structure closely resembles how
29 * you define menu hooks.
31 * See `drush topic docs-commands` for a list of recognized keys.
33 function sandwich_drush_command() {
36 // The 'make-me-a-sandwich' command.
37 $items['make-me-a-sandwich'] = array(
38 'description' => "Makes a delicious sandwich.",
40 'filling' => 'The type of the sandwich (turkey, cheese, etc.). Defaults to ascii.',
44 'description' => 'Comma delimited list of spreads.',
45 'example-value' => 'mayonnaise,mustard',
49 'drush mmas turkey --spreads=ketchup,mustard' => 'Make a terrible-tasting sandwich that is lacking in pickles.',
51 'aliases' => array('mmas'),
52 // No bootstrap at all.
53 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
56 // The 'sandwiches-served' command. Informs how many 'mmas' commands
58 $items['sandwiches-served'] = array(
59 'description' => "Report how many sandwiches we have made.",
61 'drush sandwiches-served' => 'Show how many sandwiches we have served.',
63 'aliases' => array('sws'),
64 // Example output engine data: command returns a single keyed
65 // data item (e.g. array("served" => 1)) that can either be
66 // printed with a label (e.g. "served: 1"), or output raw with
69 'outputformat' => array(
70 'default' => 'key-value',
71 'pipe-format' => 'string',
72 'label' => 'Sandwiches Served',
73 'require-engine-capability' => array('format-single'),
76 // No bootstrap at all.
77 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
80 // The 'spreads-status' command. Prints a table about available spreads.
81 $items['spreads-status'] = array(
82 'description' => "Show a table of information about available spreads.",
84 'drush spreads-status' => 'Show a table of spreads.',
86 'aliases' => array('sps'),
87 // Example output engine data: command returns a deep array
88 // that can either be printed in table format or as a json array.
90 'outputformat' => array(
92 'pipe-format' => 'json',
93 // Commands that return deep arrays will usually use
94 // machine-ids for the column data. A 'field-labels'
95 // item maps from the machine-id to a human-readable label.
96 'field-labels' => array(
98 'description' => 'Description',
102 // In table format, the 'column-widths' item is consulted
103 // to determine the default weights for any named column.
104 'column-widths' => array(
108 'require-engine-capability' => array('format-table'),
111 // No bootstrap at all.
112 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
115 // Commandfiles may also add topics. These will appear in
116 // the list of topics when `drush topic` is executed.
117 // To view this topic, run `drush --include=/full/path/to/examples topic`
118 $items['sandwich-exposition'] = array(
119 'description' => 'Ruminations on the true meaning and philosophy of sandwiches.',
122 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
123 'callback' => 'drush_print_file',
124 'callback arguments' => array(dirname(__FILE__) . '/sandwich-topic.txt'),
131 * Implements hook_drush_help().
133 * This function is called whenever a drush user calls
134 * 'drush help <name-of-your-command>'. This hook is optional. If a command
135 * does not implement this hook, the command's description is used instead.
137 * This hook is also used to look up help metadata, such as help
138 * category title and summary. See the comments below for a description.
140 function sandwich_drush_help($section) {
142 case 'drush:make-me-a-sandwich':
143 return dt("This command will make you a delicious sandwich, just how you like it.");
145 // The 'title' meta item is used to name a group of
146 // commands in `drush help`. If a title is not defined,
147 // the default is "All commands in ___", with the
148 // specific name of the commandfile (e.g. sandwich).
149 // Command files with less than four commands will
150 // be placed in the "Other commands" section, _unless_
151 // they define a title. It is therefore preferable
152 // to not define a title unless the file defines a lot
154 case 'meta:sandwich:title':
155 return dt("Sandwich commands");
157 // The 'summary' meta item is displayed in `drush help --filter`,
158 // and is used to give a general idea what the commands in this
159 // command file do, and what they have in common.
160 case 'meta:sandwich:summary':
161 return dt("Automates your sandwich-making business workflows.");
166 * Implements drush_hook_COMMAND_validate().
168 * The validate command should exit with
169 * `return drush_set_error(...)` to stop execution of
170 * the command. In practice, calling drush_set_error
171 * OR returning FALSE is sufficient. See drush.api.php
174 function drush_sandwich_make_me_a_sandwich_validate() {
175 if (drush_is_windows()) {
176 // $name = drush_get_username();
177 // @todo Implement check for elevated process using w32api
178 // as sudo is not available for Windows
179 // @see http://php.net/manual/en/book.w32api.php
180 // @see http://social.msdn.microsoft.com/Forums/en/clr/thread/0957c58c-b30b-4972-a319-015df11b427d
183 $name = posix_getpwuid(posix_geteuid());
184 if ($name['name'] !== 'root') {
185 return drush_set_error('MAKE_IT_YOUSELF', dt('What? Make your own sandwich.'));
191 * Implements drush_hook_COMMAND().
193 * The command callback is where the action takes place.
195 * The function name should be same as command name but with dashes turned to
196 * underscores and 'drush_commandfile_' prepended, where 'commandfile' is
197 * taken from the file 'commandfile.drush.inc', which in this case is
198 * 'sandwich'. Note also that a simplification step is also done in instances
199 * where the commandfile name is the same as the beginning of the command name,
200 * "drush_example_example_foo" is simplified to just "drush_example_foo".
201 * To also implement a hook that is called before your command, implement
202 * "drush_hook_pre_example_foo". For a list of all available hooks for a
203 * given command, run drush in --debug mode.
205 * If for some reason you do not want your hook function to be named
206 * after your command, you may define a 'callback' item in your command
207 * object that specifies the exact name of the function that should be
210 * In this function, all of Drupal's API is (usually) available, including
211 * any functions you have added in your own modules/themes.
213 * @see drush_invoke()
216 function drush_sandwich_make_me_a_sandwich($filling = 'ascii') {
218 // Read options with drush_get_option. Note that the options _must_
219 // be documented in the $items structure for this command in the 'command'
220 // hook. See `drush topic docs-commands` for more information.
221 if ($spreads = drush_get_option('spreads')) {
222 $list = implode(' and ', explode(',', $spreads));
223 $str_spreads = ' with just a dash of ' . $list;
225 $msg = dt('Okay. Enjoy this !filling sandwich!str_spreads.',
226 array('!filling' => $filling, '!str_spreads' => $str_spreads)
228 drush_print("\n" . $msg . "\n");
230 if (drush_get_context('DRUSH_NOCOLOR')) {
231 $filename = dirname(__FILE__) . '/sandwich-nocolor.txt';
234 $filename = dirname(__FILE__) . '/sandwich.txt';
236 drush_print(file_get_contents($filename));
237 // Find out how many sandwiches have been served, and set
238 // the cached value to one greater.
239 $served = drush_sandwich_sandwiches_served();
240 drush_cache_set(drush_get_cid('sandwiches-served'), $served + 1);
244 * Implements drush_hook_COMMAND().
246 * Demonstrates how to return a simple value that is transformed by
247 * the selected formatter to display either with a label (using the
248 * key-value formatter) or as the raw value itself (using the string formatter).
250 function drush_sandwich_sandwiches_served() {
252 $served_object = drush_cache_get(drush_get_cid('sandwiches-served'));
253 if ($served_object) {
254 $served = $served_object->data;
256 // In the default format, key-value, this return value
257 // will print " Sandwiches Served : 1". In the default pipe
258 // format, only the array value ("1") is returned.
263 * Implements drush_hook_COMMAND().
265 * This ficticious command shows how a deep array can be constructed
266 * and used as a command return value that can be output by different
269 function drush_sandwich_spreads_status() {
273 'description' => 'Some say its a vegetable, but we know its a sweet spread.',
277 'mayonnaise' => array(
278 'name' => 'Mayonnaise',
279 'description' => 'A nice dairy-free spead.',
285 'description' => 'Pardon me, but could you please pass that plastic yellow bottle?',
291 'description' => 'A necessary part of any sandwich that does not taste terrible.',
299 * Command argument complete callback.
301 * Provides argument values for shell completion.
304 * Array of popular fillings.
306 function sandwich_make_me_a_sandwich_complete() {
307 return array('values' => array('turkey', 'cheese', 'jelly', 'butter'));