What's the language that describes CLI arguments?

Viewed 81

I was using a random gcloud command today, which happens to have a ton of arguments. I deliberately started it with no arguments, so I could use the error message to figure out which arguments it requires:

ram@turing:/$ gcloud compute backend-services add-backend del-me
ERROR: (gcloud.compute.backend-services.add-backend) Exactly one of ([--instance-group : --instance-group-region | --instance-group-zone] | [--network-endpoint-group : --global-network-endpoint-group | --network-endpoint-group-region | --network-endpoint-group-zone]) must be specified.
Usage: gcloud compute backend-services add-backend BACKEND_SERVICE_NAME ([--instance-group=INSTANCE_GROUP : --instance-group-region=INSTANCE_GROUP_REGION | --instance-group-zone=INSTANCE_GROUP_ZONE] | [--network-endpoint-group=NETWORK_ENDPOINT_GROUP : --global-network-endpoint-group | --network-endpoint-group-region=NETWORK_ENDPOINT_GROUP_REGION | --network-endpoint-group-zone=NETWORK_ENDPOINT_GROUP_ZONE]) [optional flags]
  optional flags may be  --balancing-mode | --capacity-scaler | --description |
                         --failover | --global |
                         --global-network-endpoint-group | --help |
                         --instance-group | --instance-group-region |
                         --instance-group-zone | --max-connections |
                         --max-connections-per-endpoint |
                         --max-connections-per-instance | --max-rate |
                         --max-rate-per-endpoint | --max-rate-per-instance |
                         --max-utilization | --network-endpoint-group |
                         --network-endpoint-group-region |
                         --network-endpoint-group-zone | --region

For detailed information on this command and its flags, run:
  gcloud compute backend-services add-backend --help

The pertinent error message here is "Exactly one of [Redacted long expression] must be specified." Here's that long expression simplified:

([IGa : IGb | IGc] | [EGa : EGb | EGc | EGd])

This expression is in some kind of language that uses these characters: []:|. I've seen this language before in other CLIs. What is the name of this language, and where can I learn how to read it?

1 Answers

It's called a grammar and it is not really a language.

In a nutshell, a grammar describes all valid combinations of how you can build expressions out of individual elements. A grammar can be described by Backus-Naur form or by railroad diagram. A parser processes text input that conform to some grammar, and builds data structures in memory that correspond to individual grammar rules. When a grammar is used to define the syntax of a programming language, it describes the structure of a source code file. After that, compiler does the job of generating an output file, performing optimizations etc.

In console applications, a grammar could be used to describe all valid combinations of command-line arguments. In this case, gcloud needs to parse its arguments, but it does not compile anything. Most console applications are simple and people who develop them don't care too much about grammars because they only need to support a small number of possible combinations. As such, the parser might be as trivial as just a few if-statements. Such grammars are used only once, they are specific to that particular program, and most likely don't have a name.

For some reason (probably historical) people who write console applications commonly use [] for denoting optional stuff, () for grouping, | for "or", and so on. I'm not sure about what Google developers meant by :, but I would guess that it stands for "exactly one of". There is no standard so everybody does it a bit differently.

Related