Understanding Commands, Arguments, Flags, and Options in CLI

When working with CLI (Command-Line Interface) tools like Docker, Kubernetes (kubectl), Git, Linux commands, etc., you often see syntax like this:

<COMMAND> [ARGUMENTS] [FLAGS] [OPTIONS]

Each part plays a distinct role. Let’s explore them in depth.


1️⃣ Command

A command is the core instruction that tells the program what action to perform. It is the primary function you want to execute.

Example (Git):

git commit
- commit is the command that tells Git to save changes to the repository.

Example (Kubectl):

kubectl create
- create is the command that instructs Kubernetes to create a resource.


2️⃣ Arguments

Arguments provide mandatory inputs required for the command to work. They typically specify the target of the command. Arguments are usually positional and provide core input (e.g., a filename or resource name).

Example (Linux mkdir):

mkdir my_folder
- mkdir is the command (make directory). - my_folder is the argument, specifying what to create.

Example (Kubectl create deployment):

kubectl create deployment my-app
- create deployment is the command. - my-app is the argument, specifying the name of the deployment.

📌 Rule: If you omit an argument when it’s required, you’ll usually get an error.


3️⃣ Flags

Flags modify the behavior of a command. They are usually optional and start with - (short flag) or -- (long flag).

Example (Linux ls with flags):

ls -l
- ls is the command (list files). - -l is a flag, telling it to display detailed (long) output.

Example (Docker run with flags):

docker run -d nginx
- run is the command. - -d is a flag, telling Docker to run the container in detached mode.

Example (Kubectl get pods with flags):

kubectl get pods --all-namespaces
- get pods is the command. - --all-namespaces is a flag, telling it to show pods from all namespaces.

Example (--dry-run=client)

--dry-run=client is a flag, and more specifically, it’s a named flag with an option value. - Flag--dry-run is the flag itself.

  • Option valueclient is the value assigned to the --dry-run flag.

🚀 Flags control the output or behavior, but they do not take values.


4️⃣ Options

Options are similar to flags, but they take values.

Example (Git commit with options):

git commit -m "Initial commit"
- commit is the command. - -m is an option. - "Initial commit" is the value for the option.

Example (Kubectl create deployment with options):

kubectl create deployment my-app --image=nginx
- create deployment my-app is the command + argument. - --image=nginx is an option with nginx as its value.

Example (Docker run with options):

docker run --name=my-container nginx
- run is the command. - --name=my-container is an option, where my-container is the value. - nginx is an argument, specifying the image to use.

🎯 Rule: Options always require a value, whereas flags do not.


🔥 Final Comparison Table

Component Purpose Example
Command The action to perform kubectl create
Argument The target of the command kubectl create deployment my-app (my-app is an argument)
Flag Modifies behavior (without value) kubectl get pods --all-namespaces (--all-namespaces is a flag)
Option Takes a value to customize behavior kubectl create deployment my-app --image=nginx (--image=nginx is an option)

🎯 Key Takeaways

Commands tell the CLI what action to perform.
Arguments specify what the command acts on.
Flags modify behavior without needing a value.
Options modify behavior and require a value.