Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix check option #79

Closed
wants to merge 10 commits into from
Closed

Conversation

0x61nas
Copy link
Contributor

@0x61nas 0x61nas commented Jul 12, 2023

Description

Add the missing code to override the check args in the config instance with the --check CLI option

Motivation and Context

How Has This Been Tested?

Types of Changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation (no code change)
  • Refactor (refactoring production code)
  • Other

Checklist:

  • My code follows the code style of this project.
  • I have updated the documentation accordingly.
  • I have formatted the code with rustfmt.
  • I checked the lints with clippy.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@0x61nas
Copy link
Contributor Author

0x61nas commented Jul 12, 2023

I'm confused about how the args.check_args should split.

  • if the user passed --check="-h -v --help" we'll convert it into vec[vec["-h", "-v", "--help"]] which seems reasonable. but the check_args function well returns when any of the arguments are worked which is what we want in the config case because we know that every array should have related items (to test the same thing), but in this case, we don't so we should continue to test the other arguments in the same array even if one of them worked
  • if the user passed --check="-v" --check="-h,--help" we'll convert it into vec[vec["-v"], vec["-h", "--help"]] which seems reasonable also.but we'll have the same issue in the second array if the -h is succeeded, but In this case that what's the user most likely wants
  • and the last issue, if the user passed --check="-v" --check="-h,--help" --check="-V --version" we'll convert it into vec[vec["-v"], vec["-h", "--help"], vec["-V", "--version"]], and the third array it'll ignored by
for arg_variants in [
          (config.check_version).then(|| &args[0]),
          (config.check_help && args.len() >= 2).then(|| &args[1]),
      ]

so we have to make a special case if the user used the --check option?

@orhun
Copy link
Owner

orhun commented Jul 12, 2023

Hmm, it seems like a quite a bit of a thought experiment. I don't think we will allow things like --check="-h -v --help" and only accept a singular argument as the --check parameter (such as --check "-h" --check "-v" --check "--help") so we can simply use vec[vec["-h"], vec["-v"], vec["help"]]?

so we have to make a special case if the user used the --check option?

Yup, if necessary.

@0x61nas
Copy link
Contributor Author

0x61nas commented Jul 12, 2023

I don't think we will allow things like --check="-h -v --help" and only accept a singular argument as the --check parameter (such as --check "-h" --check "-v" --check "--help") so we can simply use vec[vec["-h"], vec["-v"], vec["help"]]?

Then we'll still have the ignoring behavior issue + this would be a breaking change

Yup, if necessary.

I don't see any other solution at this moment.

in this case, I have two possible ways to solve this:

  • we can add a hidden boolean variable in the config struct and set it to true if the user uses the --check option and in the get_args_help we check on this and test all the arrays elements if it's true
  • we can simply set the first element in the config.check_args array to an empty array if the user passed the --check option and use this as our condition instead of the additional variable in the first solution.

I prefer the second way 'cause it simpler

@0x61nas
Copy link
Contributor Author

0x61nas commented Jul 15, 2023

@orhun I can't figure out what makes test_check_help_args test fails. if you can provide some help for me I'll be grateful

Comment on lines +99 to +106
for arg_variants in if args[0].is_empty() {
[(args.len() > 1).then(|| &args[1..]), None]
} else {
[
(config.check_version).then(|| &args[..1]),
(config.check_help && args.len() >= 2).then(|| &args[1..]),
]
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test_check_help_args test fail because this statement returns [None, None] which means no argument is checked.

(config.check_help && args.len() >= 2).then(|| &args[1]),
]
for arg_variants in if args[0].is_empty() {
[(args.len() > 1).then(|| &args[1..]), None]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this can be simplified. It is very hard to understand what this is doing at first glance 💫

args: ArgsIter,
arg: &str,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the design choice behind removing the iterator and using a str here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'cause I moved the iteration loop to the get_args_help so I can have more control over when to stop and when to continue

Comment on lines +21 to +23
/// Error that might if the argument is not found.
#[error("Argument not found.")]
ArgumentNotFoundError,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We didn't need this type of error so far, why do we have it now?

Comment on lines +77 to +78
// Sets the first element to an empty vector to indicate that the all arguments should be checked.
args.push(Vec::new());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, is there a better way to handle this?

if args[0].is_empty() {
// If the user uses the `--check` option then we check all the arguments.
arg_variants.iter().flatten().for_each(|arg| {
let _ = check_arg(cmd, arg, verbose, output);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the result is ignored?

@orhun
Copy link
Owner

orhun commented Jul 15, 2023

Btw, I'm still not entirely sure why do we need all of these changes. Isn't this the only thing that we need:

diff --git a/src/cli.rs b/src/cli.rs
index cd1ae29..d5a37c6 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -70,6 +70,9 @@ impl CliArgs {
     pub fn update_config(&self, config: &mut Config) {
         config.check_help = !self.no_help;
         config.check_version = !self.no_version;
+        if let Some(check_args) = &self.check_args {
+            config.check_args = Some(check_args.iter().map(|v| vec![v.to_string()]).collect());
+        }
         if let Some(CliCommands::Plz {
             ref man_cmd,
             ref cheat_sh_url,

Examples in README.md work just fine with this:

$ cargo run -- --check "\--silent" zps
(°ロ°)  checking 'zps --silent'
\(^ヮ^)/ success '--silent' argument found!
$ cargo run -- --check "help" --check "test" menyoki
(°ロ°)  checking 'menyoki help'
\(^ヮ^)/ success 'help' argument found!
---
---
(°ロ°)  checking 'menyoki test'
(×﹏×)      fail 'test' argument not found.

@0x61nas
Copy link
Contributor Author

0x61nas commented Jul 15, 2023

Examples in README.md work just fine with this:

hmmmmm, I guess I unintentionally overcomplicated the problem.

diff --git a/src/cli.rs b/src/cli.rs
index cd1ae29..d5a37c6 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -70,6 +70,9 @@ impl CliArgs {
     pub fn update_config(&self, config: &mut Config) {
         config.check_help = !self.no_help;
         config.check_version = !self.no_version;
+        if let Some(check_args) = &self.check_args {
+            config.check_args = Some(check_args.iter().map(|v| vec![v.to_string()]).collect());
+        }
         if let Some(CliCommands::Plz {
             ref man_cmd,
             ref cheat_sh_url,

if this simply works then let's close this PR and open another PR or if u want to commit it on the main branch

@orhun
Copy link
Owner

orhun commented Jul 15, 2023

I think you can submit another PR 🐻 I haven't tested that approach thoroughly so feel free to add some tests if needed.

@0x61nas
Copy link
Contributor Author

0x61nas commented Jul 15, 2023

ok, i'll do that

@0x61nas 0x61nas closed this Jul 15, 2023
@0x61nas
Copy link
Contributor Author

0x61nas commented Jul 15, 2023

if this simply works then let's close this PR and open another PR or if u want to commit it on the main branch

it didn't work with this example:

 $ cargo r -- --check='-H,help,--help' halp
    Finished dev [unoptimized + debuginfo] target(s) in 0.13s
     Running `/home/anas/.cargo-target/debug/halp --check=-H,help,--help halp`
(°ロ°)  checking 'halp -H,help,--help'
(×﹏×)      fail '-H,help,--help' argument not found.

@0x61nas 0x61nas reopened this Jul 15, 2023
@0x61nas 0x61nas closed this Jul 15, 2023
@orhun
Copy link
Owner

orhun commented Jul 15, 2023

--check='-H,help,--help' halp

That's now how it is supposed to be used though. It should be individual arguments such as --check '-H', --check 'help', etc.

@0x61nas
Copy link
Contributor Author

0x61nas commented Jul 16, 2023

but it works with the current version anyway and if we change that it might be a breaking change for someone
image

@orhun
Copy link
Owner

orhun commented Jul 16, 2023

I don't think it is working.

$ halp --check "\-H,help,\-\-help" ls
(°ロ°)  checking 'ls -H,help,--help'
(×﹏×)      fail '-H,help,--help' argument not found.

Can you remove the default config file and try again?

@0x61nas
Copy link
Contributor Author

0x61nas commented Jul 16, 2023

Oh, I completely forget about the config file issue and I didn't notice that halp ignores my arguments

@0x61nas 0x61nas mentioned this pull request Jul 16, 2023
12 tasks
bors bot added a commit that referenced this pull request Jul 17, 2023
81: fix(cli): fix the check option  r=orhun a=0x61nas

<!--- Thank you for contributing to halp! 🐙 -->

## Description
Add the missing code to override the check args in the config instance with the `--check` CLI option

## Motivation and Context
- Fix the issue that's introduced in #62 

## How Has This Been Tested?
- I trayed to pass `--check="-h"` and it worked as expected
- I trayed to pass ` --check='-h' --check='-V'` and it worked as expected also

## Types of Changes
- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
- [ ] Documentation (no code change)
- [ ] Refactor (refactoring production code)
- [ ] Other <!--- (provide information) -->

## Checklist:
- [x] My code follows the code style of this project.
- [x] I have updated the documentation accordingly.
- [x] I have formatted the code with [rustfmt](https://github.com/rust-lang/rustfmt).
- [x] I checked the lints with [clippy](https://github.com/rust-lang/rust-clippy).
- [x] I have added tests to cover my changes.
- [x] All new and existing tests passed.

> This's an alternative to #79 

Co-authored-by: Anas Elgarhy <anas.elgarhy.dev@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants