|
| 1 | +--- |
| 2 | +slug: /experiments/split-cli-args/ |
| 3 | +--- |
| 4 | + |
| 5 | +import Tabs from '@theme/Tabs'; |
| 6 | +import TabItem from '@theme/TabItem'; |
| 7 | + |
| 8 | +# Split CLI Args (#2139) |
| 9 | + |
| 10 | +:::caution |
| 11 | + |
| 12 | +All experimental features are subject to breaking changes and/or removal _at any |
| 13 | +time_. We strongly recommend that you do not use these features in a production |
| 14 | +environment. They are intended for testing and feedback only. |
| 15 | + |
| 16 | +::: |
| 17 | + |
| 18 | +:::warning |
| 19 | + |
| 20 | +This experiment breaks the following functionality: |
| 21 | + |
| 22 | +- The `CLI_ARGS` variable will no longer work in arguments to string templating |
| 23 | + functions. |
| 24 | +- Outputting the `CLI_ARGS` variable directly will result in a string |
| 25 | + representation of an array (including the enclosing square brackets). |
| 26 | + |
| 27 | +::: |
| 28 | + |
| 29 | +:::info |
| 30 | + |
| 31 | +To enable this experiment, set the environment variable: |
| 32 | +`TASK_X_SPLIT_CLI_ARGS=1`. Check out [our guide to enabling experiments |
| 33 | +][enabling-experiments] for more information. |
| 34 | + |
| 35 | +::: |
| 36 | + |
| 37 | +This experiment changes the type of the `CLI_ARGS` variable from a string to an |
| 38 | +array of strings. This allows the user to more easily mutate and iterate over |
| 39 | +individual arguments. Arguments will no longer be quoted if they contain a |
| 40 | +space. However whitespace will still be escaped in arguments if the arguments |
| 41 | +are quoted properly. For example: |
| 42 | + |
| 43 | +```yaml |
| 44 | +version: '3' |
| 45 | + |
| 46 | +tasks: |
| 47 | + default: |
| 48 | + silent: true |
| 49 | + cmds: |
| 50 | + - "echo 'args: {{.CLI_ARGS}}'" |
| 51 | + - "echo 'first arg: {{index .CLI_ARGS 0}}'" |
| 52 | + - for: { var: CLI_ARGS, as: FILENAME } |
| 53 | + cmd: echo "{{.FILENAME}}" |
| 54 | +``` |
| 55 | +
|
| 56 | +<Tabs defaultValue="2" |
| 57 | + values={[ |
| 58 | + {label: 'Before', value: '1'}, |
| 59 | + {label: 'After', value: '2'} |
| 60 | + ]}> |
| 61 | +
|
| 62 | +<TabItem value="1"> |
| 63 | +
|
| 64 | +```shell |
| 65 | +$ task -- foo.txt bar.txt "hello world.txt" |
| 66 | +``` |
| 67 | +``` |
| 68 | +args: foo.txt bar.txt hello world.txt |
| 69 | +first arg: 102 |
| 70 | +foo.txt |
| 71 | +bar.txt |
| 72 | +'hello |
| 73 | +world.txt' |
| 74 | +``` |
| 75 | + |
| 76 | +</TabItem> |
| 77 | +<TabItem value="2"> |
| 78 | + |
| 79 | +```shell |
| 80 | +$ TASK_X_SPLIT_CLI_ARGS=1 task -- foo.txt bar.txt "hello world.txt" |
| 81 | +``` |
| 82 | +``` |
| 83 | +args: [foo.txt bar.txt hello world.txt] |
| 84 | +first arg: foo.txt |
| 85 | +foo.txt |
| 86 | +bar.txt |
| 87 | +hello world.txt |
| 88 | +``` |
| 89 | + |
| 90 | +</TabItem> |
| 91 | +</Tabs> |
| 92 | + |
| 93 | +## Migrating |
| 94 | + |
| 95 | +If you with to enable this experiments, but you don't want any behavior to |
| 96 | +change, you should update any uses of the `CLI_ARGS` variable to use the use the |
| 97 | +`join` function to convert the array back to a string. For example: |
| 98 | + |
| 99 | +<Tabs defaultValue="2" |
| 100 | + values={[ |
| 101 | + {label: 'Before', value: '1'}, |
| 102 | + {label: 'After', value: '2'} |
| 103 | + ]}> |
| 104 | + |
| 105 | +<TabItem value="1"> |
| 106 | + |
| 107 | +```yaml |
| 108 | +version: '3' |
| 109 | + |
| 110 | +tasks: |
| 111 | + example: |
| 112 | + cmds: |
| 113 | + - echo "CLI_ARGS: {{.CLI_ARGS}}" |
| 114 | +``` |
| 115 | +
|
| 116 | +</TabItem> |
| 117 | +<TabItem value="2"> |
| 118 | +
|
| 119 | +```yaml |
| 120 | +version: '3' |
| 121 | + |
| 122 | +tasks: |
| 123 | + example: |
| 124 | + cmds: |
| 125 | + - echo "CLI_ARGS: {{join " " .CLI_ARGS}}" |
| 126 | +``` |
| 127 | +
|
| 128 | +</TabItem> |
| 129 | +</Tabs> |
| 130 | +
|
| 131 | +Otherwise, if you want the new behavior, you can simply enable the experiment |
| 132 | +and update your tasks to use the new array of arguments. |
| 133 | +
|
| 134 | +{/* prettier-ignore-start */} |
| 135 | +[enabling-experiments]: ./experiments.mdx#enabling-experiments |
| 136 | +{/* prettier-ignore-end */} |
0 commit comments