Skip to content

Commit 35cf275

Browse files
committed
Own OptType for --help & --version
Before the OptType::Flag was used, but that supports an optional boolean value. A '--help=false' is nonsense, so the new OptType::Help doesn't support a value.
1 parent 2660483 commit 35cf275

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

src/main.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ fn shell_init_code(
174174
OptType::Counter(_) => {
175175
init_code.push(CodeChunk::AssignVar(name.clone(), VarValue::IntValue(0)));
176176
}
177+
OptType::Help(_) => {}
177178
}
178179
handled_vars.push(name.clone());
179180
} else if let OptType::Counter(_) = &opt_cfg.opt_type {
@@ -357,6 +358,12 @@ fn parse_shell_options(
357358

358359
prev_counter = Some((target, oc.count_value.get()));
359360
}
361+
OptType::Help(target) => {
362+
if opt_value.is_some() {
363+
Err(format!("{}: No value supported.", oc.options_string()))?;
364+
}
365+
shell_code.push(assign_target(target, VarValue::None));
366+
}
360367
}
361368

362369
if oc.singleton {
@@ -535,7 +542,7 @@ fn parseargs(cmd_line_args: CmdLineArgs) -> ! {
535542
opt_cfg_list.push(OptConfig {
536543
opt_chars: "".to_string(),
537544
opt_strings: vec!["help".to_string()],
538-
opt_type: OptType::Flag(OptTarget::Function("show_help".to_string())),
545+
opt_type: OptType::Help(OptTarget::Function("show_help".to_string())),
539546
required: false,
540547
singleton: true,
541548
assigned: Cell::new(false),
@@ -548,7 +555,7 @@ fn parseargs(cmd_line_args: CmdLineArgs) -> ! {
548555
opt_cfg_list.push(OptConfig {
549556
opt_chars: "".to_string(),
550557
opt_strings: vec!["version".to_string()],
551-
opt_type: OptType::Flag(OptTarget::Function("show_version".to_string())),
558+
opt_type: OptType::Help(OptTarget::Function("show_version".to_string())),
552559
required: false,
553560
singleton: true,
554561
assigned: Cell::new(false),

src/opt_def.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ pub enum OptType {
3535
Assignment(OptTarget),
3636
/// Counting occuences on the command line. Like -v, -vvv, -v -vvv,...
3737
Counter(OptTarget),
38+
/// Help related option used for --help and --version. Cannot be created
39+
/// by the user directly, only by requesting help/version support.
40+
Help(OptTarget),
3841
}
3942

4043
/// Describes a supported option.
@@ -96,7 +99,9 @@ impl OptConfig {
9699
| OptType::Counter(OptTarget::Function(name))
97100
| OptType::Counter(OptTarget::Variable(name))
98101
| OptType::ModeSwitch(OptTarget::Function(name), _)
99-
| OptType::ModeSwitch(OptTarget::Variable(name), _) => name.clone(),
102+
| OptType::ModeSwitch(OptTarget::Variable(name), _)
103+
| OptType::Help(OptTarget::Variable(name))
104+
| OptType::Help(OptTarget::Function(name)) => name.clone(),
100105
}
101106
}
102107

@@ -106,7 +111,8 @@ impl OptConfig {
106111
OptType::Flag(ot)
107112
| OptType::Assignment(ot)
108113
| OptType::Counter(ot)
109-
| OptType::ModeSwitch(ot, _) => ot,
114+
| OptType::ModeSwitch(ot, _)
115+
| OptType::Help(ot) => ot,
110116
}
111117
}
112118

0 commit comments

Comments
 (0)