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

Add .with_args feature to rust library #148

Open
ActuallyHappening opened this issue Jul 2, 2024 · 3 comments
Open

Add .with_args feature to rust library #148

ActuallyHappening opened this issue Jul 2, 2024 · 3 comments

Comments

@ActuallyHappening
Copy link

This features exists in bossy::Command here: https://docs.rs/bossy/latest/bossy/struct.Command.html#method.with_args

It is quite useful, allowing you to write this:

let kill_cmd = session.command("/root/.cargo/bin/nu").withargs([
  "-c",
  r##"ps | filter {|ps| $ps.name == "surreal"} | get pid | each {|pid| kill $pid }"##,
]);

... instead of this:

let mut kill_cmd = session.command("/root/.cargo/bin/nu");
kill_cmd.args([
  "-c",
  r##"ps | filter {|ps| $ps.name == "surreal"} | get pid | each {|pid| kill $pid }"##,
]);

This saves using the mut keyword and a new line, and can even be implemented trivially using an extension trait

@NobodyXu
Copy link
Member

NobodyXu commented Jul 2, 2024

That does sound nice to have!

We can also have a more general

fn with_command(self, f: impl FnOnce(&mut Self)) -> Self;

I would accept a PR for this.

@mingmamma
Copy link

mingmamma commented Jul 31, 2024

@NobodyXu
I am checking out the codebase and have some basic understanding. The suggestion seems like a interesting addition of API. If were to implement the function signature you suggested, based on my understanding that would be an addition of something like

// in src/session.rs, I presumed?
// presuming Self == openssh::Session?
pub fn with_command(self, f: impl FnOnce(&mut Self)) -> Self { todo!() }

Could you give a concrete example of what would be an instance of function/closure as the argument of the type: f: impl FnOnce(&mut Self) to call the new API with?

I just started playing with the codebase and don't see what such instance would be like (presumably a block of code involving use of some of the existing APIs to do some business, but I just don't visualise how that might be the case)

@NobodyXu
Copy link
Member

@mingmamma the function will be added to openssh::OwningCommand, which has many functions taking a mutable reference, such as arg, stdin, stdout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants