Skip to content

Commit

Permalink
cli: Explicitly add a Help command to accept the early args after it
Browse files Browse the repository at this point in the history
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
Grillo-0 committed Sep 23, 2024
1 parent 1f34478 commit ab50a08
Show file tree
Hide file tree
Showing 6 changed files with 335 additions and 2 deletions.
62 changes: 62 additions & 0 deletions cli/src/commands/help.rs
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))
}
66 changes: 64 additions & 2 deletions cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod evolog;
mod file;
mod fix;
mod git;
mod help;
mod init;
mod interdiff;
mod log;
Expand Down Expand Up @@ -109,6 +110,7 @@ enum Command {
Fix(fix::FixArgs),
#[command(subcommand)]
Git(git::GitCommand),
Help(help::HelpArgs),
Init(init::InitArgs),
Interdiff(interdiff::InterdiffArgs),
Log(log::LogArgs),
Expand Down Expand Up @@ -172,13 +174,72 @@ struct DummyCommandArgs {
_args: Vec<String>,
}

fn add_help_cmd_to_subcommands(command: clap::Command, help_cmd: &clap::Command) -> clap::Command {
let mut ret = command.clone();

for subcmd in command.get_subcommands() {
ret = ret.mut_subcommand(subcmd.get_name(), |mut subcmd| {
if subcmd.has_subcommands() {
subcmd = add_help_cmd_to_subcommands(subcmd.clone(), help_cmd);
subcmd.disable_help_subcommand(true).subcommand(help_cmd)
} else {
subcmd
}
});
}

ret
}

pub fn default_app() -> clap::Command {
Command::augment_subcommands(Args::command())
let app = Command::augment_subcommands(Args::command()).disable_help_subcommand(true);
add_help_cmd_to_subcommands(
app.clone(),
app.find_subcommand("help")
.expect("JJ has the help command"),
)
}

fn get_help_arg_cmd(matches: &clap::ArgMatches) -> Option<Vec<String>> {
let mut curr_matches = matches;

let mut ret = vec![];
let mut has_help = false;

while let Some((subcmd_name, matches)) = curr_matches.subcommand() {
if subcmd_name == "help" {
has_help = true;

let cmd = Command::from_arg_matches(curr_matches).unwrap();
if let Command::Help(args) = cmd {
ret.extend(args.command);
} else {
unreachable!()
}

break;
} else {
ret.push(subcmd_name.to_string());
}

curr_matches = matches;
}

if has_help {
Some(ret)
} else {
None
}
}

#[instrument(skip_all)]
pub fn run_command(ui: &mut Ui, command_helper: &CommandHelper) -> Result<(), CommandError> {
let subcommand = Command::from_arg_matches(command_helper.matches()).unwrap();
let subcommand = if let Some(command) = get_help_arg_cmd(command_helper.matches()) {
Command::Help(help::HelpArgs { command })
} else {
Command::from_arg_matches(command_helper.matches()).unwrap()
};

match &subcommand {
Command::Abandon(args) => abandon::cmd_abandon(ui, command_helper, args),
Command::Backout(args) => backout::cmd_backout(ui, command_helper, args),
Expand Down Expand Up @@ -213,6 +274,7 @@ pub fn run_command(ui: &mut Ui, command_helper: &CommandHelper) -> Result<(), Co
}
Command::Fix(args) => fix::cmd_fix(ui, command_helper, args),
Command::Git(args) => git::cmd_git(ui, command_helper, args),
Command::Help(args) => help::cmd_help(ui, command_helper, args),
Command::Init(args) => init::cmd_init(ui, command_helper, args),
Command::Interdiff(args) => interdiff::cmd_interdiff(ui, command_helper, args),
Command::Log(args) => log::cmd_log(ui, command_helper, args),
Expand Down
Loading

0 comments on commit ab50a08

Please sign in to comment.