Answers.Body: <p>I don't think you can directly pass arguments to <code>cv</code> scripts, but you can work around it:</p>
  <p><code>$ env CV_ARGS=&quot;arg1 arg2 arg3&quot; cv scr my_script.php</code></p>
  <p>my_script.php:</p>
  <pre><code>&lt;?php
  
  $my_args = getenv(&quot;CV_ARGS&quot;);
  
  var_dump($my_args);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-03T00:52:45.157Z
Answers.Text: That doesn't work so great, it will give a single string arg `"arg1 arg2 arg3"`.  You can split that, but splitting like the shell would do it is notoriously difficult to do properly.
Answers.UserDisplayName: Ken Williams
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-03T09:54:18.617Z
Answers.Text: Sure - you know what arguments you're passing to your script but mostly splitting on space is fine.  If you don't want to split just use multiple args: `env CV_ARG_1="arg1" CV_ARG_2="arg2" cv ...`
Answers.UserDisplayName: Aidan
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-04T02:12:30.110Z
Answers.Text: Thanks - I appreciate that this is the best one can do, but I'm going to submit a feature request for `cv`, because this isn't very satisfactory for general use.
Answers.UserDisplayName: Ken Williams
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-10-05T13:04:07.900Z
Answers.Text: Ticket posted at https://github.com/civicrm/cv/issues/133 .
Answers.UserDisplayName: Ken Williams
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-09-30T12:52:08.947Z
Answers.LastActivityDate: 2022-09-30T12:52:08.947Z
Answers.UserDisplayName: Aidan
Body: <p>Is it possible to pass command-line arguments to a script that's being run with <code>cv</code>?  If I try the obvious thing and just pass the argument after the script name, I get this:</p>
  <pre><code>$ sudo -u daemon `which cv` scr find_contact_id.php foo
  
                                                                
    [Symfony\Component\Console\Exception\RuntimeException]      
    Too many arguments, expected arguments &quot;command&quot; &quot;script&quot;.  
                                                                
  
  php:script [--level LEVEL] [-t|--test] [-U|--user USER] [--] &lt;script&gt;
  </code></pre>
  <p>No difference if I put a <code>--</code> after the script name to try to separate arguments.  If I quote <code>&quot;find_contact_id.php foo&quot;</code> as the script, it says:</p>
  <pre><code>$ sudo -u daemon `which cv` scr &quot;find_contact_id.php foo&quot;
  PHP Deprecated:  Array and string offset access syntax with curly braces is deprecated in /bitnami/drupal/sites/default/files/civicrm/ext/org.wikimedia.contacteditor/contacteditor.civix.php on line 246
  Deprecated: Array and string offset access syntax with curly braces is deprecated in /bitnami/drupal/sites/default/files/civicrm/ext/org.wikimedia.contacteditor/contacteditor.civix.php on line 246
  Failed to locate script: find_contact_id.php foo
  </code></pre>
  <p>Is there a way to do this?</p>
  <p>My <code>cv</code> version info:</p>
  <pre><code>$ sudo -u daemon `which cv` --version
  cv v0.3.27
  </code></pre>
  <p><strong>Update:</strong> <code>cv</code> ticket &quot;Feature request: command line arguments&quot; posted at <a href="https://github.com/civicrm/cv/issues/133" rel="nofollow noreferrer">https://github.com/civicrm/cv/issues/133</a> .</p>
  
Tags: <cv>
Title: 'cv' script with arguments
ThreadText: # 'cv' script with arguments
  
  (Asked by: username_0 on 2022-09-29)
  
  Is it possible to pass command-line arguments to a script that's being run with `cv`? If I try the obvious thing and just pass the argument after the script name, I get this:
  
  ```
  $ sudo -u daemon `which cv` scr find_contact_id.php foo
  
                                                                
    [Symfony\Component\Console\Exception\RuntimeException]      
    Too many arguments, expected arguments "command" "script".  
                                                                
  
  php:script [--level LEVEL] [-t|--test] [-U|--user USER] [--] <script>
  
  ```
  
  No difference if I put a `--` after the script name to try to separate arguments. If I quote `"find_contact_id.php foo"` as the script, it says:
  
  ```
  $ sudo -u daemon `which cv` scr "find_contact_id.php foo"
  PHP Deprecated:  Array and string offset access syntax with curly braces is deprecated in /bitnami/drupal/sites/default/files/civicrm/ext/org.wikimedia.contacteditor/contacteditor.civix.php on line 246
  Deprecated: Array and string offset access syntax with curly braces is deprecated in /bitnami/drupal/sites/default/files/civicrm/ext/org.wikimedia.contacteditor/contacteditor.civix.php on line 246
  Failed to locate script: find_contact_id.php foo
  
  ```
  
  Is there a way to do this?
  
  My `cv` version info:
  
  ```
  $ sudo -u daemon `which cv` --version
  cv v0.3.27
  
  ```
  
  **Update:** `cv` ticket "Feature request: command line arguments" posted at <https://github.com/civicrm/cv/issues/133> .
  
  **Tags:** cv
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-09-30. Score: 1 | Accepted answer
  
  I don't think you can directly pass arguments to `cv` scripts, but you can work around it:
  
  `$ env CV_ARGS="arg1 arg2 arg3" cv scr my_script.php`
  
  my\_script.php:
  
  ```
  <?php
  
  $my_args = getenv("CV_ARGS");
  
  var_dump($my_args);
  
  ```
  
  > **Comments:**
  > **username_0**: That doesn't work so great, it will give a single string arg `"arg1 arg2 arg3"`.  You can split that, but splitting like the shell would do it is notoriously difficult to do properly.  
  > **username_1**: Sure - you know what arguments you're passing to your script but mostly splitting on space is fine.  If you don't want to split just use multiple args: `env CV_ARG_1="arg1" CV_ARG_2="arg2" cv ...`  
  > **username_0**: Thanks - I appreciate that this is the best one can do, but I'm going to submit a feature request for `cv`, because this isn't very satisfactory for general use.  
  > **username_0**: Ticket posted at https://github.com/civicrm/cv/issues/133 .  
  
  ---
  
  