Sub topic 3
How sf commands are structured
This chapter assumes you have installed the CLI and confirmed it with sf --version. You should be comfortable running commands in a terminal and know what a Salesforce org is. You do not need to be logged in to an org yet.
The working model
Every sf command follows the same pattern: the sf executable, then one or more topics, then a command, then flags.
sf data query --query "SELECT Id, Name FROM Account LIMIT 5"Here is how that line breaks down:
| Part | Value | Role |
|---|---|---|
| Executable | `sf` | Starts the CLI |
| Topic | `data` | Groups related commands |
| Command | `query` | The action to run |
| Flag | `--query` | A named option |
| Flag value | `"SELECT Id, Name FROM Account LIMIT 5"` | The value for that flag |
A topic is a group of related commands. The data topic holds commands for working with records. The org topic holds commands for working with orgs. A command is the action you want to perform. A flag is a named option that changes how the command runs.
Nested topics
Some topics contain sub-topics. The command sf org create scratch has the topic org, then the command create scratch. Here create acts as a sub-topic and scratch as the command name. You do not need to track this distinction. Read the words from left to right until the flags begin.
sf org login web --alias myorg
sf project deploy start --source-dir force-app
sf apex run test --test-level RunLocalTestsEach line starts with a broad topic (org, project, apex) and narrows toward a specific action. The CLI uses this layout for all commands. Once you know the layout, you can guess the shape of commands you have not used yet.
Flags
Flags come in two forms. The long form starts with two hyphens, such as --alias. Many flags also have a short form with one hyphen and one letter, such as -a. Short forms are handy when typing, but long forms are easier to read in scripts and documentation.
A flag can take a value or act as a switch:
--alias myorgtakes a value.--set-defaulttakes no value. It turns a behavior on.--jsontakes no value. It switches the output format to JSON.
Flag order does not matter. These two commands do the same thing:
sf org login web --alias myorg --set-default
sf org login web --set-default --alias myorgQuote any value that contains spaces or characters your shell treats specially. SOQL queries nearly always need quotes.
Required flags and defaults
Some flags are required. If you leave one out, the CLI stops and names the missing flag. Other flags are optional and fall back to a default. The --target-org flag is a common example. If you omit it, the command uses your default target org from the CLI configuration. You will set that default in a later chapter. Until then, pass --target-org with an alias or username when a command needs an org.
sf data query --query "SELECT Id FROM Account LIMIT 1" --target-org myorgCommon errors
- Command not found. If the shell reports
sf: command not found, the CLI is not on yourPATH. Reopen the terminal, then recheck the installation. - Missing required flag. The error message names the flag. Add it and run the command again.
- Unexpected argument. This usually means a value with spaces was not quoted, so the shell split it into several arguments. Wrap the value in quotes.
- Wrong topic. If a command is not found within a topic, you may have misspelled a word or placed words in the wrong order. Run the topic name alone, such as
sf org, to list the commands it contains.
Reading a command you have not seen
Take this command:
sf org create scratch --definition-file config/project-scratch-def.json --alias myscratch --duration-days 7 --set-defaultRead it in steps:
sfstarts the CLI.org create scratchis the topic path and command. It creates a scratch org.--definition-filetakes a path to a JSON file.--aliastakes a name for the new org.--duration-daystakes a number.--set-defaultis a switch with no value.
You can work out what the command does without having run it before.
Exercise
In the command sf project deploy start --source-dir force-app, which word is the topic, what is the command, and what is --source-dir?
Show answer
The topic is project. deploy is a sub-topic and start is the command, so deploy start is the action. --source-dir is a flag, and force-app is its value.
Exercise
You run sf data query --query SELECT Id FROM Account and get an unexpected-argument error. What is the likely cause and the fix?
Show answer
The query contains spaces and was not quoted, so the shell passed several separate arguments. Wrap the query in quotes: sf data query --query "SELECT Id FROM Account".