Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added documentation for alias command #707

Merged
merged 5 commits into from
Apr 10, 2018
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 45 additions & 32 deletions docs-ref-conceptual/azure-cli-extension-alias.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ az extension add --name alias
Verify the installation of the extension with [az extension list](/cli/azure/extension#az-extension-list). If the alias extension was installed properly, it's listed in the command output.

```azurecli
az extension list --output table
az extension list --output table --query [].{Name:name}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In some shells like zsh, the [] token has a special meaning. Use single quotes '' around a query.

```

```output
ExtensionType Name Version
--------------- ------------------------- ---------
whl alias 0.2.0
Name
------
alias
```

## Keep the extension up to date
Expand All @@ -49,27 +49,23 @@ The alias extension is under active development and new versions are released re
az extension update --name alias
```

## Alias commands file format
## Manager Azure CLI aliases
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first word of H2 sections in our documentation should generally be an imperative verb, so the reader sees it as a call to action. This H2 should read something like Manage aliases for the Azure CLI.


Alias command definitions are written into a configuration file, located at `$AZURE_USER_CONFIG/alias`. The default value of `AZURE_USER_CONFIG` is `$HOME/.azure` on macOS and Linux, and `%USERPROFILE%\.azure` on Windows. The alias configuration file is written in the INI configuration file format. The general format for alias commands is:
The alias extension provides convenient and familiar commands to manage aliases. To view all the available commands and parameter detail, invoke the alias command with the help flag.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pluaralize to details

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Write flags/parameters the way that they would be passed to the CLI, i.e. --help here.


```
[command_name]
command = invoked_commands
```azurecli
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As convenient as the alias command, I think there is value to keep instructions of both add through command and add through config file.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add this section back.

az alias --help
```

Don't include `az` as part of the command.
## Examples
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this H2. H2s in our docs are always fully fleshed out sections that are designed to be easy to find and navigate to from the right-hand ToC. Users are more likely to look for a specific type of example rather than just any example.


## Create simple alias commands

One use of aliases is for shortening existing command groups or command names. For example, you can shorten the `group` command group to `rg` and the `list` command to `ls`.

```
[rg]
command = group

[ls]
command = list
```azurecli
az alias create --name rg --command group
az alias create --name ls --command list
```

These newly defined aliases can now be used anywhere that their definition would be.
Expand All @@ -80,11 +76,12 @@ az rg ls
az vm ls
```

Don't include az as part of the command.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Place az in preformat quotes `` so that it's clear that it's something users would type as part of a command. CLI commands are always written in backticks unless they are a link out to reference documentation.


Aliases can also be shortcuts for complete commands. The next example lists available resource groups and their locations in table output:

```
[ls-groups]
command = group list --query '[].{Name:name, Location:location}' --output table
```azurecli
az alias create --name ls-groups --command "group list --query '[].{Name:name, Location:location}' --output table"
```

Now `ls-groups` can be run like any other CLI command.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would include a line around here that lets users know that the value passed to --command must not contain newlines. This makes it clearer why all of the following examples don't span multiple lines, and will prevent a common user mistake.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alias would attempt to convert all excessive whitespaces and next line character \n to single whitespaces. So technically it is valid for users to pass in commands that span multiple lines, but the actual value that we store in the configuration file would be the whitespace-truncated string. Should I still include warning?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that's fine. In this case, can you put the newlines back into the examples they were removed from to make them more readable? The only reason I suggested removing them was that I wasn't sure if there was whitespace-stripping performed by the alias extension before writing to the config file.

Expand All @@ -95,18 +92,19 @@ az ls-groups

## Create an alias command with arguments

You can also add positional arguments to an alias command by including them as `{{ arg_name }}` in the alias name. The whitespace inside the braces is required.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that Jinja2 treats interior whitespace as significant. All of their documentation uses whitespace inside of brackets. If this is the case, this line should be left in.

From experience with documentation, not calling out where whitespace is significant as part of a spec can lead to user confusion and frustration, as whitespace is often not expected to be significant in a tokenizer where there is a clear delineation between alphanumeric and nonalphanumeric tokens.

You can also add positional arguments to an alias command by including them as `{{ arg_name }}` in the alias name.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The white space is not required anymore?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can add as many white spaces after the opening bracket and before the closing bracket


```
[alias_name {{ arg1 }} {{ arg2 }} ...]
command = invoke_including_args
```azurecli
az alias create --name "alias_name {{ arg1 }} {{ arg2 }} ..." --command "invoke_including_args"
```

The next example alias shows how to use positional arguments to get the public IP address for a VM.

```
[get-vm-ip {{ resourceGroup }} {{ vmName }}]
command = vm list-ip-addresses --resource-group {{ resourceGroup }} --name {{ vmName }} --query [0].virtualMachine.network.publicIpAddresses[0].ipAddress
```azurecli
az alias create \
--name "get-vm-ip {{ resourceGroup }} {{ vmName }}" \
--command "vm list-ip-addresses --resource-group {{ resourceGroup }} --name {{ vmName }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The INI file format does not support multi-line entries, so if the --command is stored in the passed format and does not have newlines removed, this argument must be on a single line.

--query [0].virtualMachine.network.publicIpAddresses[0].ipAddress"
```

When running this command, you give values to the positional arguments.
Expand All @@ -117,24 +115,39 @@ az get-vm-ip MyResourceGroup MyVM

You can also use environment variables in commands invoked by aliases, which are evaluated at runtime. The next example adds the `create-rg` alias, which creates a resource group in `eastus` and adds an `owner` tag. This tag is assigned the value of the local environment variable `USER`.

```azurecli
az alias create \
--name "create-rg {{ groupName }}" \
--command "group create --name {{ groupName }} --location eastus --tags owner=\$USER"
```
[create-rg {{ groupName }}]
command = group create --name {{ groupName }} --location eastus --tags owner=$USER
```

To register the environment variables inside the command of the alias, the dollar sign `$` needs to be escaped by adding a slash `\` in front of it.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave out the instructions on how to escape the $ token, and just leave it as the variable must be escaped. This is important since shell escape is only needed inside "" strings and users may write them as ''.


## Process arguments using Jinja2 templates

Argument substitution in the alias extension is performed by [Jinja2](http://jinja.pocoo.org/docs/2.10/), giving you full access to the capabilities of the Jinja2 template engine. Templates allow you to perform actions like data extraction and substitution on strings.

With Jinja2 templates, you can write aliases which take different types of arguments than the underlying command. For example, you can make an alias which takes a storage URL. Then this URL is parsed to pass the account and container names to the storage command.

```
[storage-ls {{ url }}]
command = storage blob list --account-name {{ url.replace('https://', '').split('.')[0] }} --container-name {{ url.replace('https://', '').split('/')[1] }}
```azurecli
az alias create \
--name 'storage-ls {{ url }}' \
--command "storage blob list
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above re: multiline commands

--account-name {{ url.replace('https://', '').split('.')[0] }}
--container-name {{ url.replace('https://', '').split('/')[1] }}"
```

To learn about the Jinja2 template engine, see [the Jinja2 documentation](http://jinja.pocoo.org/docs/2.10/templates/).


## Alias configuration file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include a newline after an H2.

Another way to create and modify aliaes is to alter the alias configuration file. Alias command definitions are written into a configuration file, located at `$AZURE_USER_CONFIG/alias`. The default value of `AZURE_USER_CONFIG` is `$HOME/.azure` on macOS and Linux, and `%USERPROFILE%\.azure` on Windows. The alias configuration file is written in the INI configuration file format. The general format for alias commands is:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"The other way"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove general here.

```
[command_name]
command = invoked_commands
```

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include a second example which shows how to write a command which takes arguments into the configuration file.


## Uninstall the alias extension

To uninstall the extension, use the [az extension remove](/cli/azure/extension#az-extension-remove) command.
Expand Down