-
Notifications
You must be signed in to change notification settings - Fork 349
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli: Explicitly add a Help command to accept the early args after it
The default clap's help command doesn't have the ability to accept flags (e.g --no-pager). The recommended way[1] to solve this is to manually implement it. [1]: clap-rs/clap#5332 Fixes: #4501
- Loading branch information
Showing
11 changed files
with
983 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2024 The Jujutsu Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use tracing::instrument; | ||
|
||
use crate::cli_util::CommandHelper; | ||
use crate::command_error; | ||
use crate::command_error::CommandError; | ||
use crate::ui::Ui; | ||
|
||
/// Print this message or the help of the given subcommand(s) | ||
#[derive(clap::Args, Clone, Debug)] | ||
pub(crate) struct HelpArgs { | ||
/// Print help for the subcommand(s) | ||
pub(crate) command: Vec<String>, | ||
} | ||
|
||
#[instrument(skip_all)] | ||
pub(crate) fn cmd_help( | ||
_ui: &mut Ui, | ||
command: &CommandHelper, | ||
args: &HelpArgs, | ||
) -> Result<(), CommandError> { | ||
let mut cmd_names = vec![]; | ||
let mut curr_command = command.app(); | ||
|
||
for cmd in &args.command { | ||
curr_command = | ||
curr_command | ||
.find_subcommand(cmd.clone()) | ||
.ok_or(command.app().clone().error( | ||
clap::error::ErrorKind::InvalidValue, | ||
format!("No subcomand with name {}", cmd), | ||
))?; | ||
|
||
cmd_names.push(curr_command.get_name()); | ||
} | ||
|
||
let mut args_to_show_help = vec![command.app().get_name()]; | ||
args_to_show_help.extend(cmd_names); | ||
args_to_show_help.push("--help"); | ||
|
||
let help_err = command | ||
.app() | ||
.clone() | ||
.subcommand_required(true) | ||
.try_get_matches_from(args_to_show_help) | ||
.expect_err("Clap library should return a DisplayHelp error in this context"); | ||
|
||
Err(command_error::cli_error(help_err)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.