]> git.proxmox.com Git - mirror_frr.git/blob - doc/cli.md
cfb4d629f953a3e408417c84fc00c139b30431a4
[mirror_frr.git] / doc / cli.md
1 FRR Command Line Interface
2 ==========================
3
4 Definition Grammar
5 ------------------
6
7 This is a reference for the syntax used when defining new CLI commands. An
8 example definition is:
9
10 ```
11 DEFUN (command_name,
12 command_name_cmd,
13 --> "example <command|line [interface]> DEFINITION...",
14 <..doc strings..>)
15 ```
16
17 The arrowed part is the definition string.
18
19 Explicit syntax rules in Flex and Bison may be found in lib/command_lex.l and
20 lib/command_parse.y, respectively. If you can read BNF and regex those will be
21 more useful than this document.
22
23 If the parser is throwing syntax or other errors and you can't figure out why,
24 it's unlikely to be a bug in the parser. If the error message is not useful,
25 please file a bug for a better error message. If all else fails, read the token
26 definitions in the lexer source and the Bison BNF in the parser source.
27
28 Characters allowed in each token type:
29
30 Tokens
31 ------
32 * `WORD` -- A token that begins with +, -, or a lowercase letter. It is
33 an unchanging part of the command and will only match itself.
34 Example: "show ip bgp", every token is a WORD.
35 * `IPV4` -- 'A.B.C.D', matches an IPv4 address.
36 * `IPV6` -- 'X:X::X:X', matches an IPv6 address.
37 * `IPV4_PREFIX` -- 'A.B.C.D/M', matches an IPv4 prefix in CIDR notation.
38 * `IPV6_PREFIX` -- 'X:X::X:X/M', matches an IPv6 prefix in CIDR notation.
39 * `VARIABLE` -- Begins with a capital letter. Matches any input.
40 * `RANGE` -- Numeric range delimited by parentheses, e.g. (-100 - 100) or
41 (10-20). Will only match numbers in the range.
42
43 Rules
44 -----
45 * `<angle|brackets>` -- Contain sequences of tokens separated by pipes and
46 provide mutual exclusion. Sequences may contain
47 `<mutual|exclusion>` but not as the first token.
48 Disallowed: `"example <<a|b> c|d>"`
49 Allowed: `"example <a c|b c|d>"`
50 * `[square brackets]` -- Contains sequences of tokens that are optional (can be
51 omitted). `[<a|b>]` can be shortened to `[a|b]`.
52 * `{curly|braces}` -- similar to angle brackets, but instead of mutual
53 exclusion, curly braces indicate that one or more of the
54 pipe-separated sequences may be provided in any order.
55 * `VARIADICS...` -- Any token which accepts input (so anything except WORD)
56 and that occurs as the last token of a line may be
57 followed by an ellipsis, which indicates that input
58 matching the token may be repeated an unlimited number
59 of times.
60 * `$name` -- Specify a variable name for the preceding token. See
61 "Variable Names" below.
62
63 Some general notes:
64
65 * Options are allowed at the beginning of the command. The developer is
66 entreated to use these extremely sparingly. They are most useful for
67 implementing the 'no' form of configuration commands. Please think carefully
68 before using them for anything else. There is usually a better solution, even
69 if it is just separating out the command definition into separate ones.
70
71 * The developer should judiciously apply separation of concerns when defining
72 CLI. CLI definitions for two unrelated or vaguely related commands or
73 configuration items should be defined in separate commands. Clarity is
74 preferred over LOC (within reason).
75
76 Variable Names
77 --------------
78 The parser tries to fill the "varname" field on each token. This can happen
79 either manually or automatically. Manual specifications work by appending
80 `"$name"` after the input specifier:
81
82 ```
83 foo bar$cmd WORD$name A.B.C.D$ip
84 ```
85
86 Note that you can also assign variable names to fixed input tokens, this can
87 be useful if multiple commands share code. You can also use "$name" after a
88 multiple-choice option:
89
90 ```
91 foo bar <A.B.C.D|X:X::X:X>$addr [optionA|optionB]$mode
92 ```
93
94 The variable name is in this case assigned to the last token in each of the
95 branches.
96
97 Automatic assignment of variable names works by applying the following rules:
98
99 - manual names always have priority
100 - a "[no]" at the beginning receives "no" as varname on the "no" token
101 - VARIABLE tokens whose text is not "WORD" or "NAME" receive a cleaned lowercase
102 version of the token text as varname, e.g. "ROUTE-MAP" becomes "route_map".
103 - other variable tokens (i.e. everything except "fixed") receive the text of
104 the preceding fixed token as varname, if one can be found. E.g.:
105 "ip route A.B.C.D/M INTERFACE" assigns "route" to the "A.B.C.D/M" token.
106
107 These rules should make it possible to avoid manual varname assignment in 90%
108 of the cases.
109
110 DEFPY
111 -----
112
113 `DEFPY(...)` is an enhanced version of `DEFUN()` which is preprocessed by
114 ` python/clidef.py`. The python script parses the command definition string,
115 extracts variable names and types, and generates a C wrapper function that
116 parses the variables and passes them on. This means that in the CLI function
117 body, you will receive additional parameters with appropriate types.
118
119 This is best explained by an example:
120
121 ```
122 DEFPY(func, func_cmd, "[no] foo bar A.B.C.D (0-99)$num", "...help...")
123
124 =>
125
126 func(self, vty, argc, argv, /* standard CLI arguments */
127
128 const char *no, /* unparsed "no" */
129 struct in_addr bar, /* parsed IP address */
130 const char *bar_str, /* unparsed IP address */
131 long num, /* parsed num */
132 const char *num_str) /* unparsed num */
133 ```
134
135 Note that as documented in the previous section, "bar" is automatically
136 applied as variable name for "A.B.C.D". The python code then detects this
137 is an IP address argument and generates code to parse it into a
138 `struct in_addr`, passing it in `bar`. The raw value is passed in `bar_str`.
139 The range/number argument works in the same way with the explicitly given
140 variable name.
141
142 ### Type rules
143
144 | Token(s) | Type | Value if omitted by user |
145 |--------------------------|-------------|--------------------------|
146 | `A.B.C.D` | `struct in_addr` | 0.0.0.0 |
147 | `X:X::X:X` | `struct in6_addr` | :: |
148 | `A.B.C.D + X:X::X:X` | `const union sockunion *` | NULL |
149 | `A.B.C.D/M` | `const struct prefix_ipv4 *` | NULL |
150 | `X:X::X:X/M` | `const struct prefix_ipv6 *` | NULL |
151 | `A.B.C.D/M + X:X::X:X/M` | `const struct prefix *` | NULL |
152 | `(0-9)` | `long` | 0 |
153 | `VARIABLE` | `const char *` | NULL |
154 | `word` | `const char *` | NULL |
155 | _all other_ | `const char *` | NULL |
156
157 Note the following details:
158
159 * not all parameters are pointers, some are passed as values.
160 * when the type is not `const char *`, there will be an extra `_str` argument
161 with type `const char *`.
162 * you can give a variable name not only to `VARIABLE` tokens but also to
163 `word` tokens (e.g. constant words). This is useful if some parts of a
164 command are optional. The type will be `const char *`.
165 * `[no]` will be passed as `const char *no`.
166 * pointers will be NULL when the argument is optional and the user did not
167 use it.
168 * if a parameter is not a pointer, but is optional and the user didn't use it,
169 the default value will be passed. Check the `_str` argument if you need to
170 determine whether the parameter was omitted.
171 * if the definition contains multiple parameters with the same variable name,
172 they will be collapsed into a single function parameter. The python code
173 will detect if the types are compatible (i.e. IPv4 + IPv6 variantes) and
174 choose a corresponding C type.
175 * the standard DEFUN parameters (self, vty, argc, argv) are still present and
176 can be used. A DEFUN can simply be **edited into a DEFPY without further
177 changes and it will still work**; this allows easy forward migration.
178 * a file may contain both DEFUN and DEFPY statements.
179
180 ### Getting a parameter dump
181
182 The clidef.py script can be called to get a list of DEFUNs/DEFPYs with
183 the parameter name/type list:
184
185 ```
186 lib/clippy python/clidef.py --all-defun --show lib/plist.c > /dev/null
187 ```
188
189 The generated code is printed to stdout, the info dump to stderr. The
190 `--all-defun` argument will make it process DEFUN blocks as well as DEFPYs,
191 which is useful prior to converting some DEFUNs. **The dump does not list
192 the `_str` arguments** to keep the output shorter.
193
194 Note that the clidef.py script cannot be run with python directly, it needs
195 to be run with _clippy_ since the latter makes the CLI parser available.
196
197 ### Include & Makefile requirements
198
199 A source file that uses DEFPY needs to include the `_clippy.c` file **before
200 all DEFPY statements**:
201
202 ```
203 /* GPL header */
204 #include ...
205
206 ...
207
208 #include "filename_clippy.c"
209
210 DEFPY(...)
211 DEFPY(...)
212
213 install_element(...)
214 ```
215
216 This dependency needs to be marked in Makefile.am: (there is no ordering
217 requirement)
218
219 ```
220 include ../common.am
221
222 # ...
223
224 # if linked into a LTLIBRARY (.la/.so):
225 filename.lo: filename_clippy.c
226
227 # if linked into an executable or static library (.a):
228 filename.o: filename_clippy.c
229 ```
230
231 Doc Strings
232 -----------
233 Each token in a command definition should be documented with a brief doc
234 string that informs a user of the meaning and/or purpose of the subsequent
235 command tree. These strings are provided as the last parameter to DEFUN macros,
236 concatenated together and separated by an escaped newline ('\n'). These are
237 best explained by example.
238
239 ```
240 DEFUN (config_terminal,
241 config_terminal_cmd,
242 "configure terminal",
243 "Configuration from vty interface\n"
244 "Configuration terminal\n")
245 ```
246
247 The last parameter is split into two lines for readability. Two newline
248 delimited doc strings are present, one for each token in the command. The
249 second string documents the functionality of the 'terminal' command in the
250 'configure' tree.
251
252 Note that the first string, for 'configure' does not contain documentation for
253 'terminal'. This is because the CLI is best envisioned as a tree, with tokens
254 defining branches. An imaginary 'start' token is the root of every command in a
255 CLI node. Each subsequent written token descends into a subtree, so the
256 documentation for that token ideally summarizes all the functionality contained
257 in the subtree.
258
259 A consequence of this structure is that the developer must be careful to use
260 the same doc strings when defining multiple commands that are part of the same
261 tree. Commands which share prefixes must share the same doc strings for those
262 prefixes. On startup the parser will generate warnings if it notices
263 inconsistent doc strings. Behavior is undefined; the same token may show up
264 twice in completions, with different doc strings, or it may show up once with a
265 random doc string. Parser warnings should be heeded and fixed to avoid
266 confusing users.
267
268 The number of doc strings provided must be equal to the amount of tokens
269 present in the command definition, read left to right, ignoring any special
270 constructs.
271
272 In the examples below, each arrowed token needs a doc string.
273
274 ```
275 "show ip bgp"
276 ^ ^ ^
277
278 "command <foo|bar> [example]"
279 ^ ^ ^ ^
280 ```
281
282 Data Structures
283 ---------------
284 On startup, the CLI parser sequentially parses each command string definition
285 and constructs a directed graph with each token forming a node. This graph is
286 the basis of the entire CLI system. It is used to match user input in order to
287 generate command completions and match commands to functions.
288
289 There is one graph per CLI node (not the same as a graph node in the CLI
290 graph). The CLI node struct keeps a reference to its graph (see lib/command.h).
291
292 While most of the graph maintains the form of a tree, special constructs
293 outlined in the Rules section introduce some quirks. <>, [] and {} form
294 self-contained 'subgraphs'. Each subgraph is a tree except that all of the
295 'leaves' actually share a child node. This helps with minimizing graph size and
296 debugging.
297
298 As an example, the subgraph generated by <foo|bar> looks like this:
299
300 .
301 .
302 |
303 +----+---+
304 +--- -+ FORK +----+
305 | +--------+ |
306 +--v---+ +--v---+
307 | foo | | bar |
308 +--+---+ +--+---+
309 | +------+ |
310 +------> JOIN <-----+
311 +---+--+
312 |
313 .
314 .
315
316 FORK and JOIN nodes are plumbing nodes that don't correspond to user input.
317 They're necessary in order to deduplicate these constructs where applicable.
318
319 Options follow the same form, except that there is an edge from the FORK node
320 to the JOIN node.
321
322 Keywords follow the same form, except that there is an edge from JOIN to FORK.
323 Because of this the CLI graph cannot be called acyclic. There is special logic
324 in the input matching code that keeps a stack of paths already taken through
325 the node in order to disallow following the same path more than once.
326
327 Variadics are a bit special; they have an edge back to themselves, which allows
328 repeating the same input indefinitely.
329
330 The leaves of the graph are nodes that have no out edges. These nodes are
331 special; their data section does not contain a token, as most nodes do, or
332 NULL, as in FORK/JOIN nodes, but instead has a pointer to a cmd_element. All
333 paths through the graph that terminate on a leaf are guaranteed to be defined
334 by that command. When a user enters a complete command, the command matcher
335 tokenizes the input and executes a DFS on the CLI graph. If it is
336 simultaneously able to exhaust all input (one input token per graph node), and
337 then find exactly one leaf connected to the last node it reaches, then the
338 input has matched the corresponding command and the command is executed. If it
339 finds more than one node, then the command is ambiguous (more on this in
340 deduplication). If it cannot exhaust all input, the command is unknown. If it
341 exhausts all input but does not find an edge node, the command is incomplete.
342
343 The parser uses an incremental strategy to build the CLI graph for a node. Each
344 command is parsed into its own graph, and then this graph is merged into the
345 overall graph. During this merge step, the parser makes a best-effort attempt
346 to remove duplicate nodes. If it finds a node in the overall graph that is
347 equal to a node in the corresponding position in the command graph, it will
348 intelligently merge the properties from the node in the command graph into the
349 already-existing node. Subgraphs are also checked for isomorphism and merged
350 where possible. The definition of whether two nodes are 'equal' is based on the
351 equality of some set of token properties; read the parser source for the most
352 up-to-date definition of equality.
353
354 When the parser is unable to deduplicate some complicated constructs, this
355 can result in two identical paths through separate parts of the graph. If
356 this occurs and the user enters input that matches these paths, they will
357 receive an 'ambiguous command' error and will be unable to execute the command.
358 Most of the time the parser can detect and warn about duplicate commands, but
359 it will not always be able to do this. Hence care should be taken before
360 defining a new command to ensure it is not defined elsewhere.
361
362
363 Command handlers
364 ----------------
365 The block that follows a CLI definition is executed when a user enters input
366 that matches the definition. Its function signature looks like this:
367
368 int (*func) (const struct cmd_element *, struct vty *, int, struct cmd_token *[]);
369
370 The first argument is the command definition struct. The last argument is an
371 ordered array of tokens that correspond to the path taken through the graph,
372 and the argument just prior to that is the length of the array.
373
374 The arrangement of the token array has changed from the prior incarnation of
375 the CLI system. In the old system, missing arguments were padded with NULLs so
376 that the same parts of a command would show up at the same indices regardless
377 of what was entered. The new system does not perform such padding and therefore
378 it is generally _incorrect_ to assume consistent indices in this array. As a
379 simple example:
380
381 Command definition:
382 ```
383 command [foo] <bar|baz>
384 ```
385
386 User enters:
387 ```
388 command foo bar
389 ```
390
391 Array:
392 ```
393 [0] -> command
394 [1] -> foo
395 [2] -> bar
396 ```
397
398 User enters:
399 ```
400 command baz
401 ```
402
403 Array:
404 ```
405 [0] -> command
406 [1] -> baz
407 ```
408
409
410
411 Command abbreviation & matching priority
412 ----------------------------------------
413 As in the prior implementation, it is possible for users to elide parts of
414 tokens when the CLI matcher does not need them to make an unambiguous match.
415 This is best explained by example.
416
417 Command definitions:
418 ```
419 command dog cow
420 command dog crow
421 ```
422
423 User input:
424 ```
425 c d c -> ambiguous command
426 c d co -> match "command dog cow"
427 ```
428
429 In the new implementation, this functionality has improved. Where previously
430 the parser would stop at the first ambiguous token, it will now look ahead and
431 attempt to disambiguate based on tokens later on in the input string.
432
433 Command definitions:
434 ```
435 show ip bgp A.B.C.D
436 show ipv6 bgp X:X::X:X
437 ```
438
439 User enters:
440 ```
441 s i b 4.3.2.1 -> match "show ip bgp A.B.C.D"
442 s i b ::e0 -> match "show ipv6 bgp X:X::X:X"
443 ```
444
445 Previously both of these commands would be ambiguous since 'i' does not
446 explicitly select either 'ip' or 'ipv6'. However, since the user later provides
447 a token that matches only one of the commands (an IPv4 or IPv6 address) the
448 parser is able to look ahead and select the appropriate command. This has some
449 implications for parsing the argv*[] that is passed to the command handler.
450
451 Now consider a command definition such as:
452 ```
453 command <foo|VAR>
454 ```
455
456 'foo' only matches the string 'foo', but 'VAR' matches any input, including
457 'foo'. Who wins? In situations like this the matcher will always choose the
458 'better' match, so 'foo' will win.
459
460 Consider also:
461 ```
462 show <ip|ipv6> foo
463 ```
464
465 User input:
466 ```
467 show ip foo
468 ```
469
470 'ip' partially matches 'ipv6' but exactly matches 'ip', so 'ip' will win.
471
472
473 struct cmd_token
474 ----------------
475
476 ```
477 /* Command token struct. */
478 struct cmd_token
479 {
480 enum cmd_token_type type; // token type
481 u_char attr; // token attributes
482 bool allowrepeat; // matcher allowed to match token repetitively?
483
484 char *text; // token text
485 char *desc; // token description
486 long long min, max; // for ranges
487 char *arg; // user input that matches this token
488 char *varname; // variable name
489 };
490 ```
491
492 This struct is used in the CLI graph to match input against. It is also used to
493 pass user input to command handler functions, as it is frequently useful for
494 handlers to have access to that information. When a command is matched, the
495 sequence of cmd_tokens that form the matching path are duplicated and placed in
496 order into argv*[]. Before this happens the ->arg field is set to point at the
497 snippet of user input that matched it.
498
499 For most nontrivial commands the handler function will need to determine which
500 of the possible matching inputs was entered. Previously this was done by
501 looking at the first few characters of input. This is now considered an
502 anti-pattern and should be avoided. Instead, the ->type or ->text fields for
503 this logic. The ->type field can be used when the possible inputs differ in
504 type. When the possible types are the same, use the ->text field. This field
505 has the full text of the corresponding token in the definition string and using
506 it makes for much more readable code. An example is helpful.
507
508 Command definition:
509 ```
510 command <(1-10)|foo|BAR>
511 ```
512
513 In this example, the user may enter any one of:
514 * an integer between 1 and 10
515 * "foo"
516 * anything at all
517
518 If the user enters "command f", then:
519
520 ```
521 argv[1]->type == WORD_TKN
522 argv[1]->arg == "f"
523 argv[1]->text == "foo"
524 ```
525
526 Range tokens have some special treatment; a token with ->type == RANGE_TKN will
527 have the ->min and ->max fields set to the bounding values of the range.
528
529
530 Permutations
531 ------------
532 Finally, it is sometimes useful to check all the possible combinations of input
533 that would match an arbitrary definition string. There is a tool in tools/
534 called 'permutations' that reads CLI definition strings on stdin and prints out
535 all matching input permutations. It also dumps a text representation of the
536 graph, which is more useful for debugging than anything else. It looks like
537 this:
538
539 ```
540 $ ./permutations "show [ip] bgp [<view|vrf> WORD]"
541
542 show ip bgp view WORD
543 show ip bgp vrf WORD
544 show ip bgp
545 show bgp view WORD
546 show bgp vrf WORD
547 show bgp
548 ```
549
550 This functionality is also built into VTY/VTYSH; the 'list permutations'
551 command will list all possible matching input permutations in the current CLI
552 node.