summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Estabrook <jestabro@vyos.io>2023-10-06 11:18:33 -0500
committerGitHub <noreply@github.com>2023-10-06 11:18:33 -0500
commit731fa6a931126f963475c962dbc5cedd062a9f5d (patch)
treea71b0798c309f723ed289478b6627ac8c700bc3f
parentbd727ffdd8975d60ba55b448fe194f99ec850008 (diff)
parent4a18740024573d6c6f459053a8902ad00fcfaa60 (diff)
downloadvyos-utils-731fa6a931126f963475c962dbc5cedd062a9f5d.tar.gz
vyos-utils-731fa6a931126f963475c962dbc5cedd062a9f5d.zip
Merge pull request #15 from dmbaturin/T5638-require-range
numeric: T5638: Add support for --require-range
-rw-r--r--src/numeric.ml8
1 files changed, 7 insertions, 1 deletions
diff --git a/src/numeric.ml b/src/numeric.ml
index a3c5d11..0c75f83 100644
--- a/src/numeric.ml
+++ b/src/numeric.ml
@@ -10,6 +10,7 @@ type options = {
not_values: string list;
relative: bool;
allow_range: bool;
+ require_range: bool;
}
let default_opts = {
@@ -21,6 +22,7 @@ let default_opts = {
not_values = [];
relative = false;
allow_range = false;
+ require_range = false;
}
let opts = ref default_opts
@@ -36,6 +38,7 @@ let args = [
("--float", Arg.Unit (fun () -> opts := {!opts with allow_float=true}), "Allow floating-point numbers");
("--relative", Arg.Unit (fun () -> opts := {!opts with relative=true}), "Allow relative increment/decrement (+/-N)");
("--allow-range", Arg.Unit (fun () -> opts := {!opts with allow_range=true}), "Allow the argument to be a range rather than a single number");
+ ("--require-range", Arg.Unit (fun () -> opts := {!opts with require_range=true; allow_range=true}), "Require the argument to be a range rather than a single number");
("--", Arg.Rest (fun s -> number_arg := s), "Interpret next item as an argument");
]
let usage = Printf.sprintf "Usage: %s [OPTIONS] <number>|<range>" Sys.argv.(0)
@@ -146,6 +149,7 @@ let check_not_ranges opts m =
let check_not_values opts m =
let excluded_values = List.map (number_of_string opts) opts.not_values in
+ if excluded_values = [] then () else
match m with
| Range_float _ -> Printf.ksprintf failwith "--not-value cannot be used with ranges"
| Number_float num ->
@@ -158,7 +162,9 @@ let check_not_values opts m =
let check_argument_type opts m =
match m with
- | Number_float _ -> ()
+ | Number_float _ ->
+ if opts.require_range then Printf.ksprintf failwith "Value must be a range, not a number"
+ else ()
| Range_float _ ->
if opts.allow_range then ()
else Printf.ksprintf failwith "Value must be a number, not a range"