0

I'm trying to create a drush command that need negative and positive numbers as argument, all works fine with the positive numbers, but once I tried to use the negative number I get this error:

[error]  Unknown option: --1.  See `drush help mw-reorder` for available options. To suppress this error, add the option --strict=0. 

Is like there is no way to use a negative value because drush think that this is an option.

I looked inside the drush code but I didn't found anything.

Maybe the solution is use a special option in the command to says that the argument will be consider as a negative value or I'm missing something here?

  • Put it in quotes? The parser is looking for -x and --xyz to find options, if your - is in a quote it shouldn't be seen as one, and php doesnt mind getting a negative number as a string. – Clive Sep 20 '17 at 18:48
  • @Clive something like drush mw-r devel '-1' give me the same error. – Adrian Cid Almaguer Sep 20 '17 at 18:59
  • Yeah that's what I meant, that way the last arg should be seen as a string arg instead of being parsed like an option – Clive Sep 20 '17 at 19:00
  • Oh didn't see that it gives the same error...that's a bit odd. Maybe try double quotes, they're usually interpreted differently – Clive Sep 20 '17 at 19:00
  • @Clive if you want to test the command install this module drupal.org/project/modules_weight/releases/8.x-1.x-dev – Adrian Cid Almaguer Sep 20 '17 at 19:03
2

It looks like it is not possible since the '-' is always picked up as an option being passed to the drush command. I'd suggest evaluating why you need a negative value and if it could be handled with an absolute value and/or some other creative way of indicating a negative value.

If this is not an option, I found this work around in several different articles/blogs:

php -r "print json_encode("-1");" | drush vset --format=json some_var_name - --y

The drush command docs also suggest a technique using arrays for passing a complex data structure to the command: https://drushcommands.com/drush-7x/core/variable-set/ (See the last example). This would let you pass in almost anything you wanted.

| improve this answer | |
  • I need a negative value because I'm setting the module weight, but at the end it seems that I will need to add an option in the command. – Adrian Cid Almaguer Sep 20 '17 at 19:22
  • Thanks for your solution but I think that this is a complicated way for a simple drush command – Adrian Cid Almaguer Sep 20 '17 at 19:26
  • Thanks, but is a little weird to use this solution I think, the different between the positive and negative value will be enormous. – Adrian Cid Almaguer Sep 20 '17 at 19:38
1

At the end I added a new option named --minus for the drush command.

function modules_weight_drush_command() {
  $items['mw-reorder'] = [
    'description' => dt('Configures the module weight.'),
    'arguments' => [
      'module' => dt('The module machine name'),
      'weight' => dt('The module weight'),
    ],
    'options' => [
      'force' => [
        'description' => dt('If the option is present the core modules weight will be canged even if the option to allow it is disabled.'),
      ],
      'minus' => [
        'description' => dt('If the option is present the weight will be consider as a negative value. Read https://drupal.stackexchange.com/q/246298/28275 for more information.'),
      ],
    ],
    'aliases' => ['mw-r'],
    'examples' => [
      'mw-reorder node_revision_delete' => dt('Show the node_revision_delete module weight.'),
      'mw-reorder onlyone -5 --minus' => dt('Set the onlyone module weight to -5.'),
      'mw-reorder no_autocomplete 15' => dt("Set the no_autocomplete module weight to 15."),
      'mw-reorder views 10 --force' => dt("Force to set the views module weight to 15, even if the core module reorder option is disabled."),
    ],
  ];

 return $items;
}

And this is how will works:

function drush_modules_weight_mw_reorder() {
  // Getting the minus option.
  $minus = drush_get_option('minus');
  // Applying the minus option.
  $weight = $minus ? -1 * $args[1] : $args[1];
  // Setting the new weight. 
  module_set_weight($args[0], $weight);
  // Printing the message.
  $message = dt('The module weight for @module_name was updated to @weight.', ['@module_name' => $args[0], '@weight' => $weight]);
  drush_log($message, 'success');
}

If you want to see all the code take a look inside the issue Allow to use negative values with the drush mw-reorder command

| improve this answer | |
1

Set up your module to accept a named parameter such as 'weight=5' or 'weight=-5' rather than a stand-alone numeric parameter.

It may work as a stand alone parameter entered as -'-5' or, if drush respects the common convention of '--' indicating end of flags, -- -5.

| improve this answer | |
  • Thanks for the answer, 'weight=5' as parameter will work, but I think that will be a little strange for people that usually use drush to use it. – Adrian Cid Almaguer Sep 22 '17 at 2:56
0

I have another less than ideal solution, I'm doing it as a drush sqlc command (I'm changing the variable table here):

drush sqlq "update variable set value='s:3:\"-10\";' where name='uc_pane_delivery_weight'";

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.