Skip to content

CLI returns "unknown flag" error when you misspell a command and pass a flag #4550

@dvdksn

Description

@dvdksn
Contributor

Description

If you try to run a nonexistent command, the CLI returns the following error:

$ docker iamges
docker: 'iamges' is not a docker command.
See 'docker --help'

But if you also pass a flag, the error focuses on the flag instead of the misspelled command name:

$ docker iamges --filter
unknown flag: --filter
See 'docker --help'.

Reproduce

$ docker iamges --filter
unknown flag: --filter
$ docker iamges -f
unknown shorthand flag: 'f' in -f

Expected behavior

It should identify that the command name is wrong, and return docker: 'COMMAND' is not a docker command.

docker version

Client:
 Cloud integration: v1.0.35-desktop+001
 Version:           24.0.5
 API version:       1.43
 Go version:        go1.20.6
 Git commit:        ced0996
 Built:             Fri Jul 21 20:32:30 2023
 OS/Arch:           darwin/arm64
 Context:           desktop-linux

Server: Docker Desktop 4.22.0 (117440)
 Engine:
  Version:          24.0.5
  API version:      1.43 (minimum version 1.12)
  Go version:       go1.20.6
  Git commit:       a61e2b4
  Built:            Fri Jul 21 20:35:38 2023
  OS/Arch:          linux/arm64
  Experimental:     false
 containerd:
  Version:          1.6.21
  GitCommit:        3dce8eb055cbb6872793272b4f20ed16117344f8
 runc:
  Version:          1.1.7
  GitCommit:        v1.1.7-0-g860f061
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0


### docker info

```bash
$ docker info
Client:
 Version:    24.0.5
 Context:    desktop-linux
 Debug Mode: false
 Plugins:
  compose: Docker Compose (Docker Inc.)
    Version:  v2.20.2-desktop.1
    Path:     /Users/david/.docker/cli-plugins/docker-compose
  dev: Docker Dev Environments (Docker Inc.)
    Version:  v0.1.0
    Path:     /Users/david/.docker/cli-plugins/docker-dev
  extension: Manages Docker extensions (Docker Inc.)
    Version:  v0.2.20
    Path:     /Users/david/.docker/cli-plugins/docker-extension
  init: Creates Docker-related starter files for your project (Docker Inc.)
    Version:  v0.1.0-beta.6
    Path:     /Users/david/.docker/cli-plugins/docker-init
  sbom: View the packaged-based Software Bill Of Materials (SBOM) for an image (Anchore Inc.)
    Version:  0.6.0
    Path:     /Users/david/.docker/cli-plugins/docker-sbom
  scan: Docker Scan (Docker Inc.)
    Version:  v0.26.0
    Path:     /Users/david/.docker/cli-plugins/docker-scan
  scout: Command line tool for Docker Scout (Docker Inc.)
    Version:  0.20.0
    Path:     /Users/david/.docker/cli-plugins/docker-scout

Server:
 Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
 Images: 5
 Server Version: 24.0.5
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: cgroupfs
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 3dce8eb055cbb6872793272b4f20ed16117344f8
 runc version: v1.1.7-0-g860f061
 init version: de40ad0
 Security Options:
  seccomp
   Profile: unconfined
  cgroupns
 Kernel Version: 5.15.49-linuxkit-pr
 Operating System: Docker Desktop
 OSType: linux
 Architecture: aarch64
 CPUs: 5
 Total Memory: 7.667GiB
 Name: docker-desktop
 ID: 9f623071-542b-4659-9b96-3b3a0779d84a
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 HTTP Proxy: http.docker.internal:3128
 HTTPS Proxy: http.docker.internal:3128
 No Proxy: hubproxy.docker.internal
 Experimental: false
 Insecure Registries:
  hubproxy.docker.internal:5555
  127.0.0.0/8
 Live Restore Enabled: false


### Additional Info

_No response_

Activity

thaJeztah

thaJeztah commented on Sep 12, 2023

@thaJeztah
Member

Thanks for reporting, @dvdksn (thought I commented, but apparently forgot)

Some thoughts from the top of my head;

  • wondering if this is possibly related to our CLI-plugin handling code; ISTR we have code to get (top-level) flags, so that if we call a plugin, we can pass those flags as well
  • or if it's related to the code we have to conditionally show/hide commands and flags based on the environment (possibly that causes both commands and flags to be iterated up-front

But it could also be Golang or Cobra itself parsing flags before doing anything else 🤔

I guess we need to dig into "why" it parses flags before validating if it knows the command 😂

Billy-North

Billy-North commented on Sep 15, 2023

@Billy-North

I am interested in gaining a understanding of Docker's internal workings. Based on the description this looks like a pretty reasonable first ticket to get my hands dirty.
Mind if I have a go at taking on this one?

achneerov

achneerov commented on Oct 23, 2023

@achneerov
Contributor

It seems like there have not been any updates on Billy-North's change in a month. I will give it a try. if anyone opposes please let me know.

arxngr

arxngr commented on Nov 1, 2023

@arxngr

From the comment above the PR is drafting, but I want to continue to fix this, maybe this is my first contribution, can I work on this? thanks

thaJeztah

thaJeztah commented on Nov 1, 2023

@thaJeztah
Member

From the linked PR it looks like @Billy-North won't have time to work on it (#4568 (comment)) so it's probably fine to work on this if you know an alternative fix :)

Go for it, I have been too busy to look at this for the past few weeks and I am doubtful I will get a chance again for at least another few weeks. Feel free to take this one :)

athulnair02

athulnair02 commented on Apr 5, 2024

@athulnair02

Hi! I'm from the UT Austin virtualization class and as part of our open source project, I'd like to take on this issue if possible. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @thaJeztah@Billy-North@arxngr@dvdksn@athulnair02

      Issue actions

        CLI returns "unknown flag" error when you misspell a command and pass a flag · Issue #4550 · docker/cli