Sub topic 4
Finding help for commands and flags
This chapter assumes you have installed the CLI and checked its version. It also assumes you know that sf commands are written as a topic, then a command, then flags. If those ideas are new, review the previous two chapters first. You need only a terminal. No org is required, because help output is built into the CLI and works offline.
The Salesforce CLI has hundreds of commands, and each one has many flags. Nobody memorizes them. What matters is knowing where to look and how to read what you find. By the end of this chapter you will be able to:
- Browse topics and commands from the terminal.
- Read each section of a command's help text.
- Search for a command when you only know what you want to do.
- Find the right flag, including its short form.
- Decide when to use the online reference instead.
The help system in one picture
Help in sf follows the same hierarchy as the commands. You can ask for help at three levels: the whole CLI, a topic, or a single command. Each level shows what is available one step down.
Asking for help at each level
Start with the top level. This prints the CLI's usage line, its global flags, and a list of topics.
sf --helpA topic is the first word after sf, such as org, data, project, or apex. Add --help after a topic name to see what it contains.
sf org --helpThe output has a COMMANDS section. Some entries are commands, such as sf org list. Others are subtopics that hold their own commands. For example, org login is a subtopic that contains org login web. When you see a subtopic, go one level deeper.
sf org login --helpFinally, ask for help on a complete command.
sf org login web --helpYou can type -h instead of --help. The CLI also has a help command, which takes the command words as arguments. All of these print the same help for the same command.
sf org login web --help
sf org login web -h
sf help org login webWhich form you use is a matter of habit. --help placed at the end is handy because you can type a command, realize you forgot a flag, and append --help without retyping anything.
Reading a command's help text
The help for a command is divided into sections. Learn their order, and you can scan any command quickly. Here is the shape of sf data query --help. Wording and details change between CLI versions, so treat this as an outline.
Execute a SOQL query.
USAGE
$ sf data query -o <value> [--json] [--api-version <value>] [-q <value>]
[-f <value>] [-t] [-b] [--async] [-w <value>] [-r human|csv|json]
FLAGS
-f, --file=<value> File that contains the SOQL query.
-o, --target-org=<value> (required) Username or alias of the target org.
-q, --query=<value> SOQL query to execute.
-r, --result-format=<option> Format to display the results.
<options: human|csv|json>
GLOBAL FLAGS
--flags-dir=<value> Import flag values from a directory.
--json Format output as json.
DESCRIPTION
...
EXAMPLES
...
FLAG DESCRIPTIONS
...Each section answers a different question.
| Section | What it tells you |
|---|---|
| First line | A one-sentence summary of the command. |
| `USAGE` | The command with its flags. Square brackets mean optional. |
| `FLAGS` | Each flag with its short and long names and a short description. |
| `GLOBAL FLAGS` | Flags that work on most commands, such as `--json`. |
| `DESCRIPTION` | Background, caveats, and related behavior. |
| `EXAMPLES` | Ready-to-adapt command lines. |
| `FLAG DESCRIPTIONS` | Longer explanations for flags that need more than one line. |
Reading the usage line
The usage line is a compact grammar. Three details cover most of it.
- A flag without brackets is required. In the sketch above,
-o <value>has no brackets. - A flag in brackets, such as
[--json], is optional. - Values in angle brackets, such as
<value>, are placeholders you replace with your own text.
When a flag accepts only certain values, the help lists them, for example human|csv|json. Type one of those exact words.
Short and long flag names
Many flags have two names. -o and --target-org are the same flag. The short form is one hyphen and one letter. The long form is two hyphens and a full word. Use short names when typing interactively, and long names in scripts, where readability counts. Not every flag has a short form. The help text shows a short name only when one exists.
sf data query -q "SELECT Id FROM Account LIMIT 1" -o myorg
sf data query --query "SELECT Id FROM Account LIMIT 1" --target-org myorgBoth lines run the same query. Remember that -o is only needed when you have not set a default target org, which a later chapter covers.
The required marker
If a flag is marked (required), the command fails without it. This is the quickest way to learn why a command rejects your input. Some flags are required only in combination. For example, a command may need either --query or --file but not both. The help text and the DESCRIPTION section usually say so. When they do not, the CLI's error message names the missing or conflicting flags.
Finding a command you cannot name
Sometimes you know the task but not the command. There are three ways to search.
Browse with the topic listing
Walking down from sf --help works well when you can guess the topic. Task names usually map to topics: data questions go to data, deployments go to project, and org management goes to org.
List every command
The commands command prints all commands the installed CLI and plugins provide.
sf commandsThe list is long, so filter it with your shell. This finds every command mentioning deploy:
sf commands | grep deployOn Windows PowerShell, use Select-String in place of grep:
sf commands | Select-String deployBecause sf commands lists installed plugin commands too, it shows what your installation can do, not what the CLI could do in general. If a command from the documentation is missing, a plugin may not be installed, or your CLI may be out of date. A later chapter covers updates and plugins.
Search by keyword
The search command offers an interactive search over command names and descriptions.
sf searchYou type a keyword, pick from the matches, and the CLI prints that command's help. Use it when grep finds too many hits or none.
Tab completion
Tab completion saves typing and doubles as discovery. The autocomplete command prints setup instructions for your shell.
sf autocompleteFollow the instructions it prints, then restart your shell. After that, press Tab after sf org to see matching commands, or after a command name and a hyphen to see its flags. The exact shells supported depend on your CLI version.
Using the online reference
Terminal help is the fastest source, and it always matches your installed version. The online Salesforce CLI Command Reference has advantages when you need the following:
- Every command on one searchable site.
- Commands from plugins you have not installed.
- Version-to-version differences.
Use the terminal first. Move to the web reference when you are comparing versions or planning a script for another machine. If the two disagree, trust the terminal output for what your machine can run.
Common mistakes
- Putting
--helpin the wrong place.sf --help orgis not the same assf org --help. Put the flag after the words you want help on. - Stopping at a topic.
sf org loginis a subtopic, not a runnable command. Without a final word, it shows help instead of logging you in. - Guessing flag names. Flag names differ between commands. One command uses
--source-dir, while another uses--manifest. Check each command'sFLAGSsection instead of reusing flags from memory. - Ignoring examples. The
EXAMPLESsection often answers the question faster than the description does. - Mixing old and new syntax. Older tutorials use
sfdxwith colon-separated commands such asforce:org:list. Currentsfcommands use spaces. When an old example does not work, search for the current command withsf commands | grep.
A practical lookup routine
Use this sequence when you need to run something you have not used before.
- Run
sf <topic> --helpfor the likely topic. - If nothing fits, run
sf commands | grep <keyword>, or trysf search. - Run the full command with
--help. - Read the summary, then
EXAMPLES, thenFLAGS. - Copy an example, adapt it, and run it.
- Open
FLAG DESCRIPTIONSif a flag's behavior is unclear.
Exercise
You want to run a SOQL query and save the results as a CSV file, but you do not remember the command or the flag that selects the output format. Using only the terminal, list the steps you would take to find both, and say what you would look for in the help text.
Show answer
Run sf --help and find the data topic, or run sf commands | grep query to find data query. Run sf data query --help. In the FLAGS section, look for the result-format flag, -r, --result-format, whose listed options are human|csv|json. Then run, for example, sf data query --query "SELECT Id, Name FROM Account" --result-format csv > accounts.csv. The shell redirect > writes the printed output to a file. Check the EXAMPLES section if you need to confirm the syntax.
Exercise
The usage line of a command shows -o <value> without brackets and [--json] with brackets. What does each notation mean, and what does <options: human|csv|json> tell you about a flag?
Show answer
A flag without brackets is required, and a flag in brackets is optional. So -o must be supplied (unless a default is configured) and --json can be left out. The <options: ...> line means the flag accepts only the listed values. You must type exactly one of human, csv, or json.