doc: document test command
PATCH 1/1 doc: document test command
Add documentation for the test command, including the newly added =~ operator and some gotchas wrt. the numeric comparisons.
---
This should be considered on top of
https://lore.kernel.org/u-boot/[email protected]/
, but everything other than the regex-related stuff could stand on its own.
My rst-fu is quite weak.
doc/usage/cmd/setexpr.rst | 5 ++- doc/usage/cmd/test.rst | 95 +++++++++++++++++++++++++++++++++++++++ doc/usage/index.rst | 1 + 3 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 doc/usage/cmd/test.rst
doc/usage/cmd/setexpr.rst +3 -2
@@ -144,8 +144,9 @@ Configuration * The *setexpr* command is only available if CMD_SETEXPR=y. * The *setexpr fmt* sub-command is only available if CMD_SETEXPR_FMT=y. -* The *setexpr gsub* and *setexpr sub* sub-commands are only available if - CONFIG_REGEX=y. +* The *setexpr gsub* and *setexpr sub* sub-commands are only available + if CONFIG_REGEX=y. For an overview of the supported regex syntax, + see :doc:`test`. Return value ------------
doc/usage/cmd/test.rst new file +95 -0
95 lines not on this page · raw message
doc/usage/index.rst +1 -0
@@ -123,6 +123,7 @@ Shell commands cmd/source cmd/tcpm cmd/temperature + cmd/test cmd/tftpput cmd/trace cmd/true
Signature · 1 line
2.49.0
Quoted 12 lines · Rasmus Villemoes
Add documentation for the test command, including the newly added =~ operator and some gotchas wrt. the numeric comparisons.
Signed-off-by Rasmus Villemoes <[email protected]>---
This should be considered on top of
https://lore.kernel.org/u-boot/[email protected]/
, but everything other than the regex-related stuff could stand on its own.
Quoted 8 lines
My rst-fu is quite weak.
doc/usage/cmd/setexpr.rst | 5 ++- doc/usage/cmd/test.rst | 95 +++++++++++++++++++++++++++++++++++++++ doc/usage/index.rst | 1 + 3 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 doc/usage/cmd/test.rst
Hi Rasmus,
Quoted 72 lines · Rasmus Villemoes
Add documentation for the test command, including the newly added =~ operator and some gotchas wrt. the numeric comparisons.
Signed-off-by Rasmus Villemoes <[email protected]>---
This should be considered on top of
https://lore.kernel.org/u-boot/[email protected]/
, but everything other than the regex-related stuff could stand on its own.My rst-fu is quite weak.
doc/usage/cmd/setexpr.rst | 5 ++- doc/usage/cmd/test.rst | 95 +++++++++++++++++++++++++++++++++++++++ doc/usage/index.rst | 1 + 3 files changed, 99 insertions(+), 2 deletions(-)create mode 100644
doc/usage/cmd/test.rstdoc/usage/cmd/setexpr.rst +3
-2@@ -144,8 +144,9 @@ Configuration * The *setexpr* command is only available if CMD_SETEXPR=y. * The *setexpr fmt* sub-command is only available if CMD_SETEXPR_FMT=y. -* The *setexpr gsub* and *setexpr sub* sub-commands are only available if - CONFIG_REGEX=y. +* The *setexpr gsub* and *setexpr sub* sub-commands are only available + if CONFIG_REGEX=y. For an overview of the supported regex syntax, + see :doc:`test`. Return value ------------doc/usage/cmd/test.rst new file +31
-0@@ -0,0 +1,95 @@ +.. SPDX-License-Identifier: GPL-2.0-or-later + +.. index:: + single: test (command) + +test command +============ + +Synopsis +-------- + +:: + + test <str-op> <s> + test <s1> <str-cmp> <s2> + test <n1> <num-cmp> <n2> + test ! <expr> + test <expr1> -o <expr2> + test <expr1> -a <expr2> + test -e <interface> <dev[:part]> <path> + test <s> =~ <re> + +Description +----------- + +The ``test`` command is similar to the ordinary shell built-in by the +same name. Unlike in ordinary shells, it cannot be spelled ``[``. + +Strings +~~~~~~~ +
doc/usage/cmd/test.rst line 41
Quoted 10 lines
doc/usage/cmd/test.rst new file +10
-0+The string tests ``-n`` and ``-z``, and string comparison operators +``=``, ``!=``, ``<`` and ``>``, work exactly as in ordinary shells. + +Numbers +~~~~~~~ + +The number comparison operators ``-lt``, ``-le``, ``-gt``, ``-gt``, +``-eq`` and ``-ne`` work as in ordinary shells. + +*Note*, however, that the numbers are parsed with ``simple_strtol(,
Maybe use an admonition here?
Something like
.. note:: Numbers are parsed with ``simple_strtol(,0)`` ...
so it's (hopefully) properly highlighted?
doc/usage/cmd/test.rst line 42
Quoted 1 line
doc/usage/cmd/test.rst new file +1
-0+0)``, meaning that they are treated as decimal unless there is a 0x
``0x`` ?
doc/usage/cmd/test.rst line 47
Quoted 5 lines
doc/usage/cmd/test.rst new file +5
-0+prefix, any errors in parsing are ignored, and parsing stops as soon +as a non-digit (for the selected base) is encountered. And most U-Boot +commands that generate "numeric" environment variables store them as +hexadecimal *without* a 0x prefix. +
``0x`` ?
doc/usage/cmd/test.rst line 50
Quoted 3 lines
doc/usage/cmd/test.rst new file +3
-0+For example, this is not a correct way of testing whether a given file +has a size less then 4KiB:: +
s/then/than/
doc/usage/cmd/test.rst line 58
Quoted 8 lines
doc/usage/cmd/test.rst new file +8
-0+ # Assuming readme.txt exists, sets 'filesize' environment variable + $ size mmc 0:1 readme.txt + $ if test "$filesize" -lt 4096 ; then ... + +If the file size is actually 8000 (decimal), its hexadecimal +representation, and thus the value of ``$filesize``, is ``1f40``, so +the comparison that is done ends up being "1 < 4096". +
All nicely explained! Can you provide an example on how to do this properly though?
I assume something like:
if test "0x$filesize" -lt 4096 ; then ...
maybe?
doc/usage/cmd/test.rst line 63
Quoted 5 lines
doc/usage/cmd/test.rst new file +5
-0+Logic +~~~~~ + +The ``!`` operator negates the sense of the test of the expression +``<expr>``. The ``-o`` and ``-a`` operators perform logical or and
logical OR and logical AND (and maybe even highlight them with backticks like ``OR``?) to make them stand out of the sentence since those are typical English words it may make it harder to parse the sentence if left looking like all other words?
doc/usage/cmd/test.rst line 65
Quoted 2 lines
doc/usage/cmd/test.rst new file +2
-0+logical and, respectively, of the two expressions. +
I would have the second sentence on a newline (with a blank link between the first and second sentence so that they make separate paragraphs in rST).
doc/usage/cmd/test.rst line 73
Quoted 8 lines
doc/usage/cmd/test.rst new file +8
-0+File existence +~~~~~~~~~~~~~~ + +Like ordinary shells, the ``-e`` operator can be used to test for +existence of a file. However, the U-Boot version takes three +arguments: The interface and device number (possibly including a +partition specification), in addition to the usual path. +
This could be a list, e.g.:
arguments:
- the interface (e.g. mmc)
- the device number (possibly including a partition specification)
- the typical path to the file
Quoted 12 lines
doc/usage/cmd/test.rst new file +12
-0+Regular expressions +~~~~~~~~~~~~~~~~~~~ + +When ``CONFIG_REGEX`` is enabled, an additional operator ``=~`` is +available. This is similar to the same operator available with bash's +extended test command ``[[ ]]``. The left operand is a string which is +matched against the regular expression described by the right operand. + +The regular expression engine supports these features: + +- Anchoring ``^`` and ``$``, matching at the beginning/end of the + string.
doc/usage/cmd/test.rst line 95
Quoted 10 lines
doc/usage/cmd/test.rst new file +10
-0+- Matching any single character (including whitespace) using ``.``. +- Character classes ``[ ]``, including ranges ``[0-9]`` and negation + ``[^ /.]``. +- Grouping ``( )``. +- Alternation ``|``. +- Postfix qualifiers ``*``, ``+`` and ``?`` and their non-greedy + variants ``*?``, ``+?`` and ``??`` + +For extracting the parts matching a capture group and/or performing +substitutions, including back references, see :doc:`setexpr`.
Question: Should we provide an example use case for each test subcommand?
Looking good otherwise!
Cheers,
Quentin
Quoted 2 lines · Quentin Schulz
Hi Rasmus,
Hi Quentin
Thanks for the review.
Quoted 12 lines · Rasmus Villemoes
doc/usage/cmd/test.rst line 41
Quoted 8 lines
doc/usage/cmd/test.rst new file +8
-0+ +Numbers +~~~~~~~ + +The number comparison operators ``-lt``, ``-le``, ``-gt``, ``-gt``, +``-eq`` and ``-ne`` work as in ordinary shells. + +*Note*, however, that the numbers are parsed with ``simple_strtol(,Maybe use an admonition here?
Quoted 5 lines
Something like
.. note:: Numbers are parsed with ``simple_strtol(,0)`` ...
so it's (hopefully) properly highlighted?
Good idea, as I said I'm not really an rst expert so I mostly just copy-paste.
Quoted 4 lines
Quoted 1 line
+0)``, meaning that they are treated as decimal unless there is a 0x
``0x`` ?
Quoted 8 lines
doc/usage/cmd/test.rst line 47
Quoted 5 lines
doc/usage/cmd/test.rst new file +5
-0+prefix, any errors in parsing are ignored, and parsing stops as soon +as a non-digit (for the selected base) is encountered. And most U-Boot +commands that generate "numeric" environment variables store them as +hexadecimal *without* a 0x prefix. +``0x`` ?
Hm, perhaps, I'm not sure if "code" highlighting is the best for those 0x, but I agree that some highlighting is in order.
Quoted 5 lines
doc/usage/cmd/test.rst line 50
Quoted 3 lines
doc/usage/cmd/test.rst new file +3
-0+For example, this is not a correct way of testing whether a given file +has a size less then 4KiB:: +s/then/than/
Ack.
Quoted 12 lines
doc/usage/cmd/test.rst line 58
Quoted 8 lines
doc/usage/cmd/test.rst new file +8
-0+ # Assuming readme.txt exists, sets 'filesize' environment variable + $ size mmc 0:1 readme.txt + $ if test "$filesize" -lt 4096 ; then ... + +If the file size is actually 8000 (decimal), its hexadecimal +representation, and thus the value of ``$filesize``, is ``1f40``, so +the comparison that is done ends up being "1 < 4096". +All nicely explained! Can you provide an example on how to do this properly though?
Quoted 5 lines
I assume something like:
if test "0x$filesize" -lt 4096 ; then ...
maybe?
Yes, I think that would be the proper way - but it's really messy, because if you ever have some variable that happens to contain a hex number with a 0x prefix, adding another 0x in front would end up yielding 0 when parsed. So I don't want to give the impression that one should always blindly prefix 0x (also, sometimes one really does have a decimal value). So I'm actually leaning on leaving a "positive" example out, to force a developer to think.
Quoted 10 lines
doc/usage/cmd/test.rst line 63
Quoted 5 lines
doc/usage/cmd/test.rst new file +5
-0+Logic +~~~~~ + +The ``!`` operator negates the sense of the test of the expression +``<expr>``. The ``-o`` and ``-a`` operators perform logical or andlogical OR and logical AND (and maybe even highlight them with backticks like ``OR``?) to make them stand out of the sentence since those are typical English words it may make it harder to parse the sentence if left looking like all other words?
Ah yes, ack, I did think about the "logical or and ..." reading quite oddly, but didn't think of a good way of making them stand out. Capitalizing is probably a good fix.
Quoted 6 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+logical and, respectively, of the two expressions. +I would have the second sentence on a newline (with a blank link between the first and second sentence so that they make separate paragraphs in rST).
Ack.
Quoted 12 lines
doc/usage/cmd/test.rst line 73
Quoted 9 lines
doc/usage/cmd/test.rst new file +8
-0+File existence +~~~~~~~~~~~~~~ + +Like ordinary shells, the ``-e`` operator can be used to test for +existence of a file. However, the U-Boot version takes three +arguments: The interface and device number (possibly including a +partition specification), in addition to the usual path. +This could be a list, e.g.:
Quoted 6 lines
arguments:
- the interface (e.g.
mmc)
- the device number (possibly including a partition specification)
- the typical path to the file
Ack.
Quoted 16 lines
Quoted 16 lines
doc/usage/cmd/test.rst new file +16
-0+Regular expressions +~~~~~~~~~~~~~~~~~~~ + +When ``CONFIG_REGEX`` is enabled, an additional operator ``=~`` is +available. This is similar to the same operator available with bash's +extended test command ``[[ ]]``. The left operand is a string which is +matched against the regular expression described by the right operand. + +The regular expression engine supports these features: + +- Anchoring ``^`` and ``$``, matching at the beginning/end of the + string. +- Matching any single character (including whitespace) using ``.``. +- Character classes ``[ ]``, including ranges ``[0-9]`` and negation + ``[^ /.]``. +- Grouping ``( )``.
Quoted 8 lines
doc/usage/cmd/test.rst line 95
Quoted 6 lines
+- Alternation ``|``. +- Postfix qualifiers ``*``, ``+`` and ``?`` and their non-greedydoc/usage/cmd/test.rst new file +4
-0+ variants ``*?``, ``+?`` and ``??`` + +For extracting the parts matching a capture group and/or performing +substitutions, including back references, see :doc:`setexpr`.Question: Should we provide an example use case for each test subcommand?
I don't think so. I'd assume any U-Boot developer to be reasonably familiar with the ordinary shell test, so I think it's better to just emphasize the differences and gotchas than providing "shell 101" material.
I'll fold in your suggestions, then resend the whole regex series with this one properly included as patch 12.
Thanks,
Rasmus
Hi Rasmus,
Quoted 27 lines · Rasmus Villemoes
Quoted 2 lines · Quentin Schulz
Hi Rasmus,
Hi Quentin
Thanks for the review.
Quoted 17 lines · Rasmus Villemoes
doc/usage/cmd/test.rst line 41
Quoted 8 lines
doc/usage/cmd/test.rst new file +8
-0+ +Numbers +~~~~~~~ + +The number comparison operators ``-lt``, ``-le``, ``-gt``, ``-gt``, +``-eq`` and ``-ne`` work as in ordinary shells. + +*Note*, however, that the numbers are parsed with ``simple_strtol(,Maybe use an admonition here?
Something like
.. note:: Numbers are parsed with ``simple_strtol(,0)`` ...
so it's (hopefully) properly highlighted?
And re-reading myself, we probably want:
.. note::
Numbers are parsed with ``simple_strtol(,0)`` ...
here :)
[...]
Quoted 9 lines
Quoted 8 lines
doc/usage/cmd/test.rst line 47
Quoted 5 lines
doc/usage/cmd/test.rst new file +5
-0+prefix, any errors in parsing are ignored, and parsing stops as soon +as a non-digit (for the selected base) is encountered. And most U-Boot +commands that generate "numeric" environment variables store them as +hexadecimal *without* a 0x prefix. +``0x`` ?
Quoted 3 lines
Hm, perhaps, I'm not sure if "code" highlighting is the best for those 0x, but I agree that some highlighting is in order.
Fair, could be also 0x if you prefer how this renders, I don't think U-Boot is too picky about those.
[...]
Quoted 18 lines
Quoted 12 lines
doc/usage/cmd/test.rst line 58
Quoted 8 lines
doc/usage/cmd/test.rst new file +8
-0+ # Assuming readme.txt exists, sets 'filesize' environment variable + $ size mmc 0:1 readme.txt + $ if test "$filesize" -lt 4096 ; then ... + +If the file size is actually 8000 (decimal), its hexadecimal +representation, and thus the value of ``$filesize``, is ``1f40``, so +the comparison that is done ends up being "1 < 4096". +All nicely explained! Can you provide an example on how to do this properly though?
Quoted 5 lines
I assume something like:
if test "0x$filesize" -lt 4096 ; then ...
maybe?
Quoted 8 lines
Yes, I think that would be the proper way - but it's really messy, because if you ever have some variable that happens to contain a hex number with a 0x prefix, adding another 0x in front would end up yielding 0 when parsed. So I don't want to give the impression that one should always blindly prefix 0x (also, sometimes one really does have a decimal value). So I'm actually leaning on leaving a "positive" example out, to force a developer to think.
This is terrible :/
We now need to figure out if a command modifies environment variables to contain hex values, returns hex values, or simply decimal values.
Nothing that is coming from your doing in this patch though :)
[...]
Quoted 25 lines
Quoted 16 lines
Quoted 16 lines
doc/usage/cmd/test.rst new file +16
-0+Regular expressions +~~~~~~~~~~~~~~~~~~~ + +When ``CONFIG_REGEX`` is enabled, an additional operator ``=~`` is +available. This is similar to the same operator available with bash's +extended test command ``[[ ]]``. The left operand is a string which is +matched against the regular expression described by the right operand. + +The regular expression engine supports these features: + +- Anchoring ``^`` and ``$``, matching at the beginning/end of the + string. +- Matching any single character (including whitespace) using ``.``. +- Character classes ``[ ]``, including ranges ``[0-9]`` and negation + ``[^ /.]``. +- Grouping ``( )``.Quoted 8 lines
doc/usage/cmd/test.rst line 95
Quoted 6 lines
+- Alternation ``|``. +- Postfix qualifiers ``*``, ``+`` and ``?`` and their non-greedydoc/usage/cmd/test.rst new file +4
-0+ variants ``*?``, ``+?`` and ``??`` + +For extracting the parts matching a capture group and/or performing +substitutions, including back references, see :doc:`setexpr`.Question: Should we provide an example use case for each test subcommand?
Quoted 5 lines
I don't think so. I'd assume any U-Boot developer to be reasonably familiar with the ordinary shell test, so I think it's better to just emphasize the differences and gotchas than providing "shell 101" material.
Fair enough!
Quentin