Skip to content

Commit

Permalink
cli: Move flags if help command is used
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). So move them if the help command is used.

Fixes: #4501
  • Loading branch information
Grillo-0 committed Sep 25, 2024
1 parent fb4614d commit eec69d9
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 2 deletions.
45 changes: 43 additions & 2 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3007,6 +3007,46 @@ fn resolve_aliases(
}
}

fn move_global_args_if_help_cmd(
app: &Command,
args: Vec<String>,
) -> Result<Vec<String>, CommandError> {
let has_help_cmd = args[..args.len() - 1].iter().any(|str| str == "help");

if !has_help_cmd {
return Ok(args);
}

let args_without_help = args.iter().filter(|&str| str != "help");

let mut curr_matches = &app
.clone()
.allow_external_subcommands(true)
.ignore_errors(true)
.try_get_matches_from(args_without_help)?;

let mut cmds = vec!["help"];

while let Some((subcmd_name, matches)) = curr_matches.subcommand() {
cmds.push(subcmd_name);

curr_matches = matches;
}

let (ret, rest) = args.split_at(1);
let mut ret = ret.to_vec();

let (commands, args): (Vec<_>, Vec<_>) = rest
.iter()
.cloned()
.partition(|str| cmds.contains(&str.as_str()));

ret.extend(args);
ret.extend(commands);

Ok(ret)
}

/// Parse args that must be interpreted early, e.g. before printing help.
fn handle_early_args(
ui: &mut Ui,
Expand Down Expand Up @@ -3055,8 +3095,9 @@ pub fn expand_args(
}
}

let string_args = resolve_default_command(ui, config, app, string_args)?;
resolve_aliases(ui, config, app, string_args)
let mut string_args = resolve_default_command(ui, config, app, string_args)?;
string_args = resolve_aliases(ui, config, app, string_args)?;
move_global_args_if_help_cmd(app, string_args)
}

pub fn parse_args(
Expand Down
1 change: 1 addition & 0 deletions cli/tests/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ mod test_git_remotes;
mod test_git_submodule;
mod test_gitignores;
mod test_global_opts;
mod test_help_command;
mod test_immutable_commits;
mod test_init_command;
mod test_interdiff_command;
Expand Down
4 changes: 4 additions & 0 deletions cli/tests/test_global_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,10 @@ fn test_early_args() {
let stdout = test_env.jj_cmd_success(test_env.env_root(), &["--color=always", "help"]);
insta::assert_snapshot!(stdout.lines().find(|l| l.contains("Commands:")).unwrap(), @"Commands:");

// Check that early args are accepted after the help command
let stdout = test_env.jj_cmd_success(test_env.env_root(), &["help", "--color=always"]);
insta::assert_snapshot!(stdout.lines().find(|l| l.contains("Commands:")).unwrap(), @"Commands:");

// Early args are parsed with clap's ignore_errors(), but there is a known
// bug that causes defaults to be unpopulated. Test that the early args are
// tolerant of this bug and don't cause a crash.
Expand Down
46 changes: 46 additions & 0 deletions cli/tests/test_help_command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 crate::common::TestEnvironment;

#[test]
fn test_help() {
let test_env = TestEnvironment::default();

let help_cmd_stdout = test_env.jj_cmd_success(test_env.env_root(), &["help"]);
// The help command output should be equal to the long --help flag
let help_flag_stdout = test_env.jj_cmd_success(test_env.env_root(), &["--help"]);
assert_eq!(help_cmd_stdout, help_flag_stdout);

// Help command should work with commands
let help_cmd_stdout = test_env.jj_cmd_success(test_env.env_root(), &["help", "log"]);
let help_flag_stdout = test_env.jj_cmd_success(test_env.env_root(), &["log", "--help"]);
assert_eq!(help_cmd_stdout, help_flag_stdout);

// Help command should work with subcommands
let help_cmd_stdout =
test_env.jj_cmd_success(test_env.env_root(), &["help", "workspace", "add"]);
let help_flag_stdout =
test_env.jj_cmd_success(test_env.env_root(), &["workspace", "add", "--help"]);
assert_eq!(help_cmd_stdout, help_flag_stdout);

// Help command should work recursively
let help_cmd_stdout =
test_env.jj_cmd_success(test_env.env_root(), &["workspace", "help", "add"]);
let help_flag_stdout =
test_env.jj_cmd_success(test_env.env_root(), &["workspace", "add", "--help"]);
assert_eq!(help_cmd_stdout, help_flag_stdout);

let _ = test_env.jj_cmd_failure(test_env.env_root(), &["workspace", "add", "help"]);
}

0 comments on commit eec69d9

Please sign in to comment.