Control how Alibaba Cloud CLI API calls are executed
When you use Alibaba Cloud CLI to call APIs, you may need to retrieve all paginated data at once, wait for a resource to be created before proceeding, or preview a request without executing it. This topic describes the Global Flags that implement these three scenarios and how to configure them.
Choose the right parameter
All three are Global Flags that apply to a single API call. Choose the parameter based on your scenario:
|
Scenario |
Parameter |
|
A List API returns only one page but you need all data |
|
|
Wait for a resource status to become ready after creation |
|
|
Preview a request without sending an API request. |
|
Mutual exclusion rules
-
--pagerand--waitercannot be used together. The CLI returns the following error if you combine them:ERROR: flag --pager is exclusive with --waiter. -
--cli-dry-runis mutually exclusive with--pager,--waiter, and--quiet. They cannot be specified together in the same call.
Automatically aggregate all pages (--pager)
Basic usage
Append --pager to any paginated List/Describe API. The CLI automatically iterates through all pages and merges the results into a single output.
aliyun ecs DescribeInstances --RegionId cn-hangzhou --pager
Two paging modes
The CLI automatically determines which mode to use. No manual configuration is needed.
-
PageNumbermode: The API response containsPageNumber,PageSize, andTotalCountfields. The CLI increments the page number until all pages are retrieved. -
NextTokenmode: The API response contains aNextTokenfield. The CLI passes the returnedNextTokenvalue as the next request parameter untilNextTokenis an empty string.
If the response contains a NextToken field with a non-empty value, NextToken mode is used. Otherwise, PageNumber mode is used.
Field mapping
The following sub-parameters are all optional. You only need to specify them when an API uses non-default field names for pagination information, to tell the CLI what field names the API uses. In most cases, simply appending --pager is sufficient.
|
Sub-parameter |
Default |
When to specify |
|
|
(none) |
The data array path in the response is not at the top level (e.g., |
|
|
PageNumber |
The API uses a different field name for the page number |
|
|
PageSize |
The API uses a different field name for the page size |
|
|
TotalCount |
The API uses a different field name for the total record count |
|
|
NextToken |
The API uses a different field name for the pagination token |
Syntax: --pager key=value key=value. Multiple sub-parameters are separated by spaces. The value is the actual field name in the API response, not a specific value. For example, PageNumber=CurrentPage means the API uses the CurrentPage field to represent the page number, not that you are specifying a starting page.
Custom field mapping examples
The data array is nested at Vpcs.Vpc[] in the API response:
aliyun vpc DescribeVpcs --RegionId cn-hangzhou --pager path=Vpcs.Vpc[]
When the API uses another field (such as Token) instead of NextToken as the pagination token, append NextToken=Token to --pager to specify the mapping; the command form is the same as the previous example. The field value also supports nested paths, such as NextToken=Data.NextToken.
Wait for resource status (--waiter)
Basic usage
The CLI repeatedly calls the current command, extracts a value from each response using the expr expression, and stops polling when the extracted value equals the to value, returning the last response. When timeout and interval are not specified, the CLI polls every 5 seconds for a maximum of 180 seconds (3 minutes) by default.
aliyun ecs DescribeInstances \
--RegionId cn-hangzhou \
--InstanceIds '["i-bp1xxxxx"]' \
--waiter expr='Instances.Instance[0].Status' to=Running
Sub-parameters
|
Sub-parameter |
Required |
Default |
Value range |
Description |
|
|
Yes |
— |
— |
A JMESPath expression that specifies the field to poll in the JSON response. |
|
|
Yes |
— |
— |
The target value. Polling stops and the data is returned when the |
|
|
No |
180 |
1–600 (seconds) |
The maximum polling wait time. If the target value is not matched within this period, the CLI returns an error and exits. |
|
|
No |
5 |
2–10 (seconds) |
The interval between two polling attempts. |
-
The field referenced by
exprmust be of the string type. If the field in the API response is a number or a JSON boolean (true/false), the CLI returns an error immediately. -
The
tovalue is always compared as a string. For numeric statuses, writeto=1(notto=1.0). If the API returns a boolean as a string (e.g.,"true"), writeto=true. -
If an API call returns an HTTP error during polling, the CLI stops immediately and outputs the error without retrying.
-
When the
timeoutperiod elapses without matching the target value, the CLI exits with a non-zero exit code and outputs the following tostderr:wait 'Instances.Instance[0].Status' to 'Running' timeout(180seconds), last='Pending'The
lastvalue is the actual value obtained from the last poll.
Control polling frequency
Shorten the polling interval (wait up to 30 seconds, poll every 2 seconds):
aliyun ecs DescribeInstances \
--RegionId cn-hangzhou \
--InstanceIds '["i-bp1xxxxx"]' \
--waiter expr='Instances.Instance[0].Status' to=Running timeout=30 interval=2
Extend the wait time for resources that take longer to start (wait up to 10 minutes, poll every 10 seconds):
aliyun ecs DescribeInstances \
--RegionId cn-hangzhou \
--InstanceIds '["i-bp1xxxxx"]' \
--waiter expr='Instances.Instance[0].Status' to=Running timeout=600 interval=10
Preview a request without executing it (--cli-dry-run)
Basic usage
A boolean switch that takes effect when appended. No value assignment is needed. The CLI constructs and previews the request without sending an API request. The preview does not prove that permissions, server-side business validity, or required business conditions have been validated.
aliyun ecs DescribeInstances --RegionId cn-hangzhou --cli-dry-run
Output content
The output includes fields such as Style, Endpoint, Method, Action, Version, the request Headers, and the request parameters (Query). You can use this to verify whether the endpoint is correct and whether parameter serialization is as expected.
Example output:
---
Style: RPC
Endpoint: ecs-cn-hangzhou.aliyuncs.com
Method: POST
Action: DescribeInstances
Version: 2014-05-26
Headers:
x-sdk-client: golang/1.0.0
x-sdk-invoke-type: common
Accept-Encoding: identity
Note: SDK runtime may add signing and transport headers before sending the request.
Query:
RegionId = cn-hangzhou
---
Typical scenarios
-
Preview sensitive operations before execution, such as
DeleteInstanceandStopInstance. After confirming that the previewed request is as expected, remove--cli-dry-runto execute it. -
Preview the assembled request in CI/CD pipelines (without consuming resources or incurring costs).
Automation scenario: complete async workflow
Scenario: Create a VPC, wait until the status becomes available, then retrieve the full VPC list. The following demonstrates how the three parameters work together to complete an async operation.
Step 1: Preview the create request with --cli-dry-run
aliyun vpc CreateVpc \
--RegionId cn-hangzhou \
--CidrBlock 172.16.0.0/12 \
--VpcName test-vpc \
--cli-dry-run
After confirming that the Endpoint and parameter serialization are as expected, remove --cli-dry-run to execute the request.
Step 2: Execute the creation and extract the VPC ID
VPC_ID=$(aliyun vpc CreateVpc \
--RegionId cn-hangzhou \
--CidrBlock 172.16.0.0/12 \
--VpcName test-vpc \
--cli-query 'VpcId')
--cli-query extracts the VPC ID from the response.
Step 3: Wait for the instance to become ready with --waiter
The CLI polls every 5 seconds until the status changes to Available or the 60-second timeout is reached:
aliyun vpc DescribeVpcs \
--RegionId cn-hangzhou \
--VpcId "$VPC_ID" \
--waiter expr='Vpcs.Vpc[0].Status' to=Available timeout=60
Step 4: Retrieve all VPCs with --pager
When you have many VPCs, without --pager only the first page is returned and the new VPC may not be visible. Use --pager to confirm the newly created VPC appears in the list:
aliyun vpc DescribeVpcs --RegionId cn-hangzhou --pager
Step 5: Delete the VPC (optional)
Delete the VPC created earlier:
aliyun vpc DeleteVpc --VpcId $VPC_ID
FAQ
Can --pager and --waiter be used together?
No. They are mutually exclusive. The CLI returns the following error: ERROR: flag --pager is exclusive with --waiter. If you need to wait for a resource to become ready and then retrieve the full list, use two separate commands.