Skip to content

Commit 90e0d30

Browse files
authored
test: move help strings to markdown file (#4816)
1 parent e166d90 commit 90e0d30

File tree

2 files changed

+95
-79
lines changed

2 files changed

+95
-79
lines changed

src/uu/test/src/test.rs

Lines changed: 16 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -20,82 +20,27 @@ use std::fs;
2020
use std::os::unix::fs::MetadataExt;
2121
use uucore::display::Quotable;
2222
use uucore::error::{UResult, USimpleError};
23-
use uucore::format_usage;
23+
use uucore::{format_usage, help_about, help_section};
2424

25+
const ABOUT: &str = help_about!("test.md");
26+
27+
// The help_usage method replaces util name (the first word) with {}.
28+
// And, The format_usage method replaces {} with execution_phrase ( e.g. test or [ ).
29+
// However, This test command has two util names.
30+
// So, we use test or [ instead of {} so that the usage string is correct.
2531
const USAGE: &str = "\
26-
{} EXPRESSION
27-
{}
28-
[ EXPRESSION ]
29-
[ ]
30-
[ OPTION";
32+
test EXPRESSION
33+
test
34+
[ EXPRESSION ]
35+
[ ]
36+
[ OPTION";
3137

3238
// We use after_help so that this comes after the usage string (it would come before if we used about)
33-
const AFTER_HELP: &str = "
34-
Exit with the status determined by EXPRESSION.
35-
36-
An omitted EXPRESSION defaults to false. Otherwise,
37-
EXPRESSION is true or false and sets exit status. It is one of:
38-
39-
( EXPRESSION ) EXPRESSION is true
40-
! EXPRESSION EXPRESSION is false
41-
EXPRESSION1 -a EXPRESSION2 both EXPRESSION1 and EXPRESSION2 are true
42-
EXPRESSION1 -o EXPRESSION2 either EXPRESSION1 or EXPRESSION2 is true
43-
44-
-n STRING the length of STRING is nonzero
45-
STRING equivalent to -n STRING
46-
-z STRING the length of STRING is zero
47-
STRING1 = STRING2 the strings are equal
48-
STRING1 != STRING2 the strings are not equal
49-
50-
INTEGER1 -eq INTEGER2 INTEGER1 is equal to INTEGER2
51-
INTEGER1 -ge INTEGER2 INTEGER1 is greater than or equal to INTEGER2
52-
INTEGER1 -gt INTEGER2 INTEGER1 is greater than INTEGER2
53-
INTEGER1 -le INTEGER2 INTEGER1 is less than or equal to INTEGER2
54-
INTEGER1 -lt INTEGER2 INTEGER1 is less than INTEGER2
55-
INTEGER1 -ne INTEGER2 INTEGER1 is not equal to INTEGER2
56-
57-
FILE1 -ef FILE2 FILE1 and FILE2 have the same device and inode numbers
58-
FILE1 -nt FILE2 FILE1 is newer (modification date) than FILE2
59-
FILE1 -ot FILE2 FILE1 is older than FILE2
60-
61-
-b FILE FILE exists and is block special
62-
-c FILE FILE exists and is character special
63-
-d FILE FILE exists and is a directory
64-
-e FILE FILE exists
65-
-f FILE FILE exists and is a regular file
66-
-g FILE FILE exists and is set-group-ID
67-
-G FILE FILE exists and is owned by the effective group ID
68-
-h FILE FILE exists and is a symbolic link (same as -L)
69-
-k FILE FILE exists and has its sticky bit set
70-
-L FILE FILE exists and is a symbolic link (same as -h)
71-
-N FILE FILE exists and has been modified since it was last read
72-
-O FILE FILE exists and is owned by the effective user ID
73-
-p FILE FILE exists and is a named pipe
74-
-r FILE FILE exists and read permission is granted
75-
-s FILE FILE exists and has a size greater than zero
76-
-S FILE FILE exists and is a socket
77-
-t FD file descriptor FD is opened on a terminal
78-
-u FILE FILE exists and its set-user-ID bit is set
79-
-w FILE FILE exists and write permission is granted
80-
-x FILE FILE exists and execute (or search) permission is granted
81-
82-
Except for -h and -L, all FILE-related tests dereference symbolic links.
83-
Beware that parentheses need to be escaped (e.g., by backslashes) for shells.
84-
INTEGER may also be -l STRING, which evaluates to the length of STRING.
85-
86-
NOTE: Binary -a and -o are inherently ambiguous. Use 'test EXPR1 && test
87-
EXPR2' or 'test EXPR1 || test EXPR2' instead.
88-
89-
NOTE: [ honors the --help and --version options, but test does not.
90-
test treats each of those as it treats any other nonempty STRING.
91-
92-
NOTE: your shell may have its own version of test and/or [, which usually supersedes
93-
the version described here. Please refer to your shell's documentation
94-
for details about the options it supports.";
95-
96-
const ABOUT: &str = "Check file types and compare values.";
39+
const AFTER_HELP: &str = help_section!("after help", "test.md");
9740

9841
pub fn uu_app() -> Command {
42+
// Disable printing of -h and -v as valid alternatives for --help and --version,
43+
// since we don't recognize -h and -v as help/version flags.
9944
Command::new(uucore::util_name())
10045
.version(crate_version!())
10146
.about(ABOUT)
@@ -112,15 +57,7 @@ pub fn uumain(mut args: impl uucore::Args) -> UResult<()> {
11257
if binary_name.ends_with('[') {
11358
// If invoked as [ we should recognize --help and --version (but not -h or -v)
11459
if args.len() == 1 && (args[0] == "--help" || args[0] == "--version") {
115-
// Let clap pretty-print help and version
116-
Command::new(binary_name)
117-
.version(crate_version!())
118-
.about(ABOUT)
119-
.override_usage(format_usage(USAGE))
120-
.after_help(AFTER_HELP)
121-
// Disable printing of -h and -v as valid alternatives for --help and --version,
122-
// since we don't recognize -h and -v as help/version flags.
123-
.get_matches_from(std::iter::once(program).chain(args.into_iter()));
60+
uu_app().get_matches_from(std::iter::once(program).chain(args.into_iter()));
12461
return Ok(());
12562
}
12663
// If invoked via name '[', matching ']' must be in the last arg

src/uu/test/test.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# test
2+
3+
```
4+
test EXPRESSION
5+
test
6+
[ EXPRESSION ]
7+
[ ]
8+
[ OPTION
9+
```
10+
11+
Check file types and compare values.
12+
13+
## After Help
14+
15+
Exit with the status determined by `EXPRESSION`.
16+
17+
An omitted `EXPRESSION` defaults to false.
18+
Otherwise, `EXPRESSION` is true or false and sets exit status.
19+
20+
It is one of:
21+
22+
* ( EXPRESSION ) `EXPRESSION` is true
23+
* ! EXPRESSION `EXPRESSION` is false
24+
* EXPRESSION1 -a EXPRESSION2 both `EXPRESSION1` and `EXPRESSION2` are true
25+
* EXPRESSION1 -o EXPRESSION2 either `EXPRESSION1` or `EXPRESSION2` is true
26+
27+
String operations:
28+
* -n STRING the length of `STRING` is nonzero
29+
* STRING equivalent to -n `STRING`
30+
* -z STRING the length of `STRING` is zero
31+
* STRING1 = STRING2 the strings are equal
32+
* STRING1 != STRING2 the strings are not equal
33+
34+
Integer comparisons:
35+
* INTEGER1 -eq INTEGER2 `INTEGER1` is equal to `INTEGER2`
36+
* INTEGER1 -ge INTEGER2 `INTEGER1` is greater than or equal to `INTEGER2`
37+
* INTEGER1 -gt INTEGER2 `INTEGER1` is greater than `INTEGER2`
38+
* INTEGER1 -le INTEGER2 `INTEGER1` is less than or equal to `INTEGER2`
39+
* INTEGER1 -lt INTEGER2 `INTEGER1` is less than `INTEGER2`
40+
* INTEGER1 -ne INTEGER2 `INTEGER1` is not equal to `INTEGER2`
41+
42+
File operations:
43+
* FILE1 -ef FILE2 `FILE1` and `FILE2` have the same device and inode numbers
44+
* FILE1 -nt FILE2 `FILE1` is newer (modification date) than `FILE2`
45+
* FILE1 -ot FILE2 `FILE1` is older than `FILE2`
46+
47+
* -b FILE `FILE` exists and is block special
48+
* -c FILE `FILE` exists and is character special
49+
* -d FILE `FILE` exists and is a directory
50+
* -e FILE `FILE` exists
51+
* -f FILE `FILE` exists and is a regular file
52+
* -g FILE `FILE` exists and is set-group-ID
53+
* -G FILE `FILE` exists and is owned by the effective group ID
54+
* -h FILE `FILE` exists and is a symbolic link (same as -L)
55+
* -k FILE `FILE` exists and has its sticky bit set
56+
* -L FILE `FILE` exists and is a symbolic link (same as -h)
57+
* -N FILE `FILE` exists and has been modified since it was last read
58+
* -O FILE `FILE` exists and is owned by the effective user ID
59+
* -p FILE `FILE` exists and is a named pipe
60+
* -r FILE `FILE` exists and read permission is granted
61+
* -s FILE `FILE` exists and has a size greater than zero
62+
* -S FILE `FILE` exists and is a socket
63+
* -t FD `file` descriptor `FD` is opened on a terminal
64+
* -u FILE `FILE` exists and its set-user-ID bit is set
65+
* -w FILE `FILE` exists and write permission is granted
66+
* -x FILE `FILE` exists and execute (or search) permission is granted
67+
68+
Except for `-h` and `-L`, all FILE-related tests dereference (follow) symbolic links.
69+
Beware that parentheses need to be escaped (e.g., by backslashes) for shells.
70+
`INTEGER` may also be -l `STRING`, which evaluates to the length of `STRING`.
71+
72+
NOTE: Binary `-a` and `-o` are inherently ambiguous.
73+
Use `test EXPR1 && test EXPR2` or `test EXPR1 || test EXPR2` instead.
74+
75+
NOTE: `[` honors the `--help` and `--version` options, but test does not.
76+
test treats each of those as it treats any other nonempty `STRING`.
77+
78+
NOTE: your shell may have its own version of `test` and/or `[`, which usually supersedes the version described here.
79+
Please refer to your shell's documentation for details about the options it supports.

0 commit comments

Comments
 (0)