

# 在 AWS CLI 中启用和使用命令提示符
<a name="cli-usage-parameters-prompting"></a>

运行 `aws` 命令时，您可以让 AWS CLI 版本 2 提示输入命令、参数和资源。

**Topics**
+ [工作原理](#cli-usage-auto-prompt-about)
+ [自动提示功能](#cli-usage-auto-prompt-features)
+ [自动提示模式](#cli-usage-auto-prompt-modes)
+ [配置自动提示](#cli-usage-auto-prompt-configure)

## 工作原理
<a name="cli-usage-auto-prompt-about"></a>

如果启用，自动提示可让您使用 **Enter** 键完成部分输入的命令。按 **Enter** 键后，根据您继续键入的内容，建议命令、参数和资源。建议在左侧列出了命令、参数或资源的名称，在右侧列出了该命令、参数或资源的说明。要选择并使用建议，请使用箭头键突出显示某个行，然后按**空格**键。输入完命令后，按 **Enter** 键以使用该命令。以下示例演示了自动提示中的建议列表的外观。

```
$ aws
> aws a
       accessanalyzer                Access Analyzer
       acm                           AWS Certificate Manager
       acm-pca                       AWS Certificate Manager Private Certificate Authority
       alexaforbusiness              Alexa For Business
       amplify                       AWS Amplify
```

## 自动提示功能
<a name="cli-usage-auto-prompt-features"></a>

自动提示包含以下有用功能：

**文档面板**  
提供当前命令的帮助文档。要打开文档，请按 **F3** 键。

**命令完成**  
建议要使用的 `aws` 命令。要查看列表，请部分输入命令。以下示例正在搜索以字母 `a` 开头的服务。  

```
$ aws
> aws a
       accessanalyzer                Access Analyzer
       acm                           AWS Certificate Manager
       acm-pca                       AWS Certificate Manager Private Certificate Authority
       alexaforbusiness              Alexa For Business
       amplify                       AWS Amplify
```

**参数完成**  
键入命令后，自动提示功能会开始建议参数。参数的描述包括值类型以及对参数定义的描述。首先列出必要的参数，并将它们标记为必需。以下示例显示了 `aws dynamodb describe-table` 的自动提示参数列表。  

```
$ aws dynamodb describe-table
> aws dynamodb describe-table 
                              --table-name (required)  [string] The name of the table to describe.
                               --cli-input-json         [string] Reads arguments from the JSON string provided. The JSON string follows the format provide...
                               --cli-input-yaml         [string] Reads arguments from the YAML string provided. The YAML string follows the format provide...
                               --generate-cli-skeleton  [string] Prints a JSON skeleton to standard output without sending an API request. If provided wit...
```

**资源完成**  
自动提示会使用可用的 AWS 资源属性调用 AWS API 来建议资源值。这样就能够在输入参数时自动提示以建议您可能拥有的资源。在以下示例中，在填写 `aws dynamodb describe-table` 命令的 `--table-name` 参数时，自动提示会列出表名称。  

```
$ aws dynamodb describe-table
> aws dynamodb describe-table --table-name 
                                            Table1
                                            Table2
                                            Table3
```

**速记完成**  
对于使用速记语法的参数，自动提示会建议要使用的值。在以下示例中，自动提示列出了 `aws ec2 run-instances` 命令中 `--placement` 参数的速记语法值。  

```
$ aws ec2 run-instances
> aws ec2 run-instances --placement 
 AvailabilityZone=      [string] The Availability Zone of the instance. If not specified, an Availability Zone wil...
 Affinity=              [string] The affinity setting for the instance on the Dedicated Host. This parameter is no...
 GroupName=             [string] The name of the placement group the instance is in.
 PartitionNumber=       [integer] The number of the partition the instance is in. Valid only if the placement grou...
```

**文件完成**  
在 `aws` 命令中填写参数时，自动完成会在使用前缀 `file://` 或 `fileb://` 后建议本地文件名。在以下示例中，在为 `aws ec2 run-instances` 命令输入 `--item file://` 后，自动提示会建议使用本地文件。  

```
$ aws ec2 run-instances
> aws ec2 run-instances --item file:// 
                         item1.txt
                         file1.json
                         file2.json
```

**区域完成**  
使用全局参数 `--region` 时，自动提示会列出可供选择的可能区域。在以下示例中，在为 `aws dynamodb list-tables` 命令输入 `--region` 后，自动提示会按字母顺序建议区域。  

```
$ aws dynamodb list-tables
> aws dynamodb list-tables --region 
                                     af-south-1
                                     ap-east-1
                                     ap-northeast-1
                                     ap-northeast-2
```

**配置文件完成**  
使用全局参数 `--profile` 时，自动提示会列出您的配置文件。在以下示例中，在为 `aws dynamodb list-tables` 命令输入 `--profile` 后，自动提示会建议您的配置文件。  

```
$ aws dynamodb list-tables
> aws dynamodb list-tables --profile 
                                     profile1
                                     profile2
                                     profile3
```

**模糊搜索**  
包含一组特定字符的完整命令和值。在以下示例中，在为 `aws dynamodb list-tables` 命令输入 `--region eu` 后，自动提示会建议包含 `eu` 的区域。  

```
$ aws dynamodb list-tables
> aws dynamodb list-tables --region west
                                         eu-west-1
                                         eu-west-2
                                         eu-west-3
                                         us-west-1
```

**历史记录**  
要在自动提示模式下查看和运行先前使用的命令，请按 **Ctrl \$1 R**。历史记录列出了以前的命令，您可以使用箭头键进行选择。在以下示例中，将显示自动提示模式历史记录。  

```
$ aws
> aws 
        dynamodb list-tables
        s3 ls
```

## 自动提示模式
<a name="cli-usage-auto-prompt-modes"></a>

AWS CLI 版本 2 的自动提示有 2 种可以配置的模式：
+ **完全模式：**无论是使用 `--cli-auto-prompt` 参数手动调用 `aws` 命令还是永久启用该命令，每次尝试运行该命令时都会使用自动提示。这包括在完整或不完整的命令之后按 **Enter** 键。
+ **部分模式：**如果命令不完整或由于客户端验证错误而无法运行，则使用自动提示。如果您已有现有脚本、运行手册，或者如果您希望只对于不熟悉的命令收到自动提示，而不是每个命令都收到提示，则此模式将非常有帮助。

## 配置自动提示
<a name="cli-usage-auto-prompt-configure"></a>

要配置自动提示，您可以按优先顺序使用以下方法：
+ **命令行选项**启用或禁用单个命令的自动提示。使用 `--cli-auto-prompt` 调用自动提示，使用 `--no-cli-auto-prompt` 禁用自动提示。
+ **环境变量**使用 `aws\$1cli\$1auto\$1prompt` 变量。
+ **共享的配置文件**使用 `cli\$1auto\$1prompt` 设置。