Sub topic 3
How sf commands are structured
This chapter explains how sf commands are built. It covers topics, commands, flags, and the rules for combining them. It assumes you have installed the CLI (the previous chapter) and are comfortable typing commands in a terminal. You do not need an org yet, but some examples show what the commands would do against one.
The grammar of an sf command
Every sf command follows the same shape. The CLI is a tree. The word sf is the root. Words after it name a topic, then possibly a subtopic, and finally a command. Flags follow the command.
sf org login web --alias myorg --set-defaultHere is how the CLI reads that line:
| Part | Role |
|---|---|
| `sf` | The executable. It is the root of the command tree. |
| `org` | A topic. A topic groups related commands. |
| `login` | A subtopic inside `org`. |
| `web` | The command. It does the work. |
| `--alias myorg` | A flag with a value. |
| `--set-default` | A boolean flag. It takes no value. |
The topic path and the command name together form the full command name. In the example, the full name is org login web. Salesforce documentation often writes this as sf org login web.
The sf command line is built on the oclif framework. oclif is a toolkit for building command-line programs, and it gives every command the same parsing rules. Once you know the rules below, you can read any sf command, including ones you have never used.
Topics
A topic is a group of commands about one area of work. Common topics include:
orgfor working with orgs: logging in, listing, creating scratch orgs, opening an org in the browser.projectfor local project work: generating a project, deploying, retrieving.datafor records: querying, creating, updating, deleting.apexfor running Apex code, running tests, and reading logs.configfor CLI settings such as the default target org.pluginsfor managing CLI plugins.
Topics can nest. org login is a subtopic that holds several login commands, such as org login web and org login jwt. The command project deploy start has the path project then deploy then start. The depth is not fixed, so some commands have two words and others have three or four.
You can run a topic by itself. The CLI does not run anything. It prints the commands that live inside the topic.
sf orgThis is a safe way to explore. Nothing is changed in any org.
Commands and verbs
Most sf commands read as a path ending in a verb or action word: start, list, display, create, login, run. The same word appears in many topics, which makes the CLI predictable.
sf org listlists orgs.sf pluginslists plugins.sf org displayshows details of one org.sf project deploy startstarts a deployment.sf project retrieve startstarts a retrieval.sf apex runruns anonymous Apex.sf apex run testruns tests.
Notice that apex run is a command and apex run test is also a command. A command and a subtopic can share a name. The CLI picks the longest match it can find, so sf apex run test runs tests and sf apex run runs a script.
Many commands are long-running. project deploy start returns after the deployment starts or finishes, depending on the flags you use. Other commands in the same topic, such as project deploy report, check on the job later. The start and report pair is a common pattern in this CLI.
Flags
Flags are the options of a command. They come after the command name. Each flag has a long form that starts with two hyphens, and many also have a short form with one hyphen and one letter.
sf data query --query "SELECT Id, Name FROM Account LIMIT 5" --target-org myorg
sf data query -q "SELECT Id, Name FROM Account LIMIT 5" -o myorgBoth lines do the same thing. The first uses long flags. The second uses short flags: -q for --query and -o for --target-org.
Flags that take a value
Most flags need a value. You can separate the flag and the value with a space, or join them with an equals sign:
sf org login web --alias myorg
sf org login web --alias=myorgQuote any value that contains spaces or characters your shell treats specially. A SOQL query is the usual case. Without quotes, your shell would split the query into many words, and the CLI would fail with an unexpected-argument error.
Boolean flags
A boolean flag switches a behavior on. You write the flag and give no value. --set-default and --json are boolean flags. If you leave the flag out, the behavior stays off.
Required flags
Some flags are required. If you omit one, the CLI stops before doing any work and prints an error that names the missing flag. For example, sf data query needs either --query or --file. A required flag is an early check. It protects you from running a half-formed command.
Repeatable flags
A few flags accept more than one value. You can repeat the flag, and for some flags you can also list several values after one flag:
sf project deploy start --source-dir force-app/main/default/classes --source-dir force-app/main/default/triggersThe help for a command tells you which flags allow this. Check the help before you rely on it.
Mutually exclusive flags
Some flags cannot be used together. In the deployment command, you choose what to deploy with one approach, such as --source-dir, --metadata, or --manifest. If you mix flags that exclude each other, the CLI prints an error that names the conflict. This is not a bug. The two flags describe different ways to answer the same question.
Reading the usage line
Every command's help begins with a usage line. It uses a small notation:
- Words in capitals or angle brackets are placeholders you replace.
- Square brackets mean an option is not required.
- A pipe
|means you choose one of several values.
A line such as sf org login web [-a <value>] [-d] [-r <value>] shows three optional flags. Two take a value, written as <value>. One is boolean and has no <value>.
The next chapter covers help in detail. For now, remember that you can always add --help to a command to see its flags.
Global flags and the target org
Many commands act on an org. These commands share one flag, --target-org (short form -o). It says which org to use. If you leave it out, the CLI uses the default target org from your configuration, so you only need the flag when you want to act on a different org.
This pattern is why a later chapter on defaults is useful. Without a default, you would add --target-org to almost every command. With one, you type it only for exceptions.
The target org value can be a username or an alias. An alias is a short nickname you set at login with --alias. It saves you from typing a long username.
Another flag you will meet often is --json. It is available on nearly every command and changes the output format. The chapter on scripting covers it. You can try it now:
sf org list --jsonWith --json, the output is a JSON document with a status field, a result field, and, on failure, an error message. The exit code of the command also reports success or failure, which scripts can test.
Exit codes and errors
The CLI returns exit code 0 when a command succeeds. It returns a non-zero code when a command fails. You can read the last exit code in a Bash-like shell with $?.
sf org list
echo $?Common errors that come from command structure:
- Command not found. You misspelled a topic or command, or a plugin that provides it is not installed. Run the parent topic, such as
sf org, to see what exists. - Missing required flag. The message names the flag. Add it and rerun.
- Unexpected argument. Usually an unquoted value that the shell split into several words. Quote the value.
- Flags that conflict. Two flags exclude each other. Remove one.
- No default org. A command needs an org and finds neither
--target-orgnor a configured default. Pass the flag or set a default.
Old and new command names
Older material uses sfdx commands with colons, such as sfdx force:source:deploy. The current CLI is sf, which uses spaces and a different topic layout. If you follow an older tutorial, translate the commands before you run them. The two styles can look similar but are not interchangeable. When in doubt, run the topic in sf and read what it lists.
A short worked session
This session uses only the structure you have learned. It explores before it acts.
sf
sf org
sf org list --help
sf org list
echo $?sfprints the list of top-level topics.sf orgprints the commands inside theorgtopic.sf org list --helpshows the flags of one command, including--json.sf org listruns the command.echo $?shows the exit code, which is0on success.
None of these steps change anything in an org, so you can run them freely.
Summary of rules
| Rule | Example |
|---|---|
| Commands are a path of words | `sf project deploy start` |
| A topic alone lists its commands | `sf org` |
| Long flags use `--`, short flags use `-` | `--target-org`, `-o` |
| Values can follow a space or `=` | `--alias myorg`, `--alias=myorg` |
| Boolean flags take no value | `--json`, `--set-default` |
| Quote values with spaces | `--query "SELECT Id FROM Account"` |
| `--target-org` overrides the default org | `-o myorg` |
| Exit code `0` means success | `echo $?` |
Exercise
You run sf data query --query SELECT Id, Name FROM Account LIMIT 5 and the CLI reports an unexpected argument. What is wrong, and how do you fix the command?
Show answer
The query contains spaces and is not quoted, so the shell splits it into several words and the CLI sees extra arguments. Quote the value: sf data query --query "SELECT Id, Name FROM Account LIMIT 5".
Exercise
Name the topic, the subtopic, the command, and the flags in sf org login web --alias myorg --set-default. Which flag takes no value?
Show answer
The topic is org, the subtopic is login, and the command is web. The flags are --alias, which takes the value myorg, and --set-default. --set-default is a boolean flag and takes no value.
Exercise
A command that works against your default org must run once against a different org with the alias qa. Which flag do you add, and what are its long and short forms?
Show answer
Add --target-org qa, or the short form -o qa. It overrides the default target org for that one command.