support filesize arguments in random binary/chars (#14068)

Closes #13920

# User-Facing Changes

`random binary` and `random chars` now support filesize arguments:

```nushell
random binary 1kb
random chars --length 1kb
```
This commit is contained in:
Solomon
2024-10-12 06:49:05 +00:00
committed by GitHub
parent e32e55938b
commit d83781ddec
5 changed files with 55 additions and 7 deletions

View File

@ -14,7 +14,11 @@ impl Command for SubCommand {
Signature::build("random binary")
.input_output_types(vec![(Type::Nothing, Type::Binary)])
.allow_variants_without_examples(true)
.required("length", SyntaxShape::Int, "Length of the output binary.")
.required(
"length",
SyntaxShape::OneOf(vec![SyntaxShape::Int, SyntaxShape::Filesize]),
"Length of the output binary.",
)
.category(Category::Random)
}
@ -43,11 +47,18 @@ impl Command for SubCommand {
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Generate 16 random bytes",
example: "random binary 16",
result: None,
}]
vec![
Example {
description: "Generate 16 random bytes",
example: "random binary 16",
result: None,
},
Example {
description: "Generate 1 random kilobyte",
example: "random binary 1kb",
result: None,
},
]
}
}

View File

@ -21,7 +21,7 @@ impl Command for SubCommand {
.allow_variants_without_examples(true)
.named(
"length",
SyntaxShape::Int,
SyntaxShape::OneOf(vec![SyntaxShape::Int, SyntaxShape::Filesize]),
"Number of chars (default 25)",
Some('l'),
)
@ -58,6 +58,11 @@ impl Command for SubCommand {
example: "random chars --length 20",
result: None,
},
Example {
description: "Generate one kilobyte of random chars",
example: "random chars --length 1kb",
result: None,
},
]
}
}