Repository: apache/airflow

Issue:
`airflow tasks states-for-dag-run` has no `map_index` column
### Apache Airflow version

2.3.0b1 (pre-release)

### What happened

I ran:

```
$ airflow tasks states-for-dag-run taskmap_xcom_pull 'manual__2022-04-14T13:27:04.958420+00:00'
dag_id            | execution_date                   | task_id   | state   | start_date                       | end_date
==================+==================================+===========+=========+==================================+=================================
taskmap_xcom_pull | 2022-04-14T13:27:04.958420+00:00 | foo       | success | 2022-04-14T13:27:05.343134+00:00 | 2022-04-14T13:27:05.598641+00:00
taskmap_xcom_pull | 2022-04-14T13:27:04.958420+00:00 | bar       | success | 2022-04-14T13:27:06.256684+00:00 | 2022-04-14T13:27:06.462664+00:00
taskmap_xcom_pull | 2022-04-14T13:27:04.958420+00:00 | identity  | success | 2022-04-14T13:27:07.480364+00:00 | 2022-04-14T13:27:07.713226+00:00
taskmap_xcom_pull | 2022-04-14T13:27:04.958420+00:00 | identity  | success | 2022-04-14T13:27:07.512084+00:00 | 2022-04-14T13:27:07.768716+00:00
taskmap_xcom_pull | 2022-04-14T13:27:04.958420+00:00 | identity  | success | 2022-04-14T13:27:07.546097+00:00 | 2022-04-14T13:27:07.782719+00:00
```

...targeting a dagrun for which `identity` had three expanded tasks.  All three showed up, but the output didn't show me enough to know which one was which.

### What you think should happen instead

There should be a `map_index` column so that I know which one is which.

### How to reproduce

Run a dag with expanded tasks, then try to view their states via the cli

### Operating System

debian (docker)

### Versions of Apache Airflow Providers

n/a

### Deployment

Astronomer

### Deployment details

_No response_

### Anything else

_No response_

### Are you willing to submit PR?

- [ ] Yes I am willing to submit a PR!

### Code of Conduct

- [X] I agree to follow this project's [Code of Conduct](https://github.com/apache/airflow/blob/main/CODE_OF_CONDUCT.md)



Discussion:

Resolving patch:
diff --git a/airflow/cli/commands/task_command.py b/airflow/cli/commands/task_command.py
--- a/airflow/cli/commands/task_command.py
+++ b/airflow/cli/commands/task_command.py
@@ -23,7 +23,7 @@
 import os
 import textwrap
 from contextlib import contextmanager, redirect_stderr, redirect_stdout
-from typing import List, Optional, Tuple, Union
+from typing import Dict, List, Optional, Tuple, Union
 
 from pendulum.parsing.exceptions import ParserError
 from sqlalchemy.orm.exc import NoResultFound
@@ -483,18 +483,22 @@ def task_states_for_dag_run(args, session=None):
             "not found"
         )
 
-    AirflowConsole().print_as(
-        data=dag_run.task_instances,
-        output=args.output,
-        mapper=lambda ti: {
+    has_mapped_instances = any(ti.map_index >= 0 for ti in dag_run.task_instances)
+
+    def format_task_instance(ti: TaskInstance) -> Dict[str, str]:
+        data = {
             "dag_id": ti.dag_id,
             "execution_date": dag_run.execution_date.isoformat(),
             "task_id": ti.task_id,
             "state": ti.state,
             "start_date": ti.start_date.isoformat() if ti.start_date else "",
             "end_date": ti.end_date.isoformat() if ti.end_date else "",
-        },
-    )
+        }
+        if has_mapped_instances:
+            data["map_index"] = str(ti.map_index) if ti.map_index >= 0 else ""
+        return data
+
+    AirflowConsole().print_as(data=dag_run.task_instances, output=args.output, mapper=format_task_instance)
 
 
 @cli_utils.action_cli(check_db=False)


Test patch: