Tyrrrz / CliWrap

Library for running command-line processes
MIT License
4.32k stars 264 forks source link

Add `WithArguments` overload (or new method) that allows appending arguments to an existing command #209

Closed MindSwipe closed 1 year ago

MindSwipe commented 1 year ago

Details

Currently calling WithArguments returns a new command where any potential arguments already added are cleared, for example when doing:

var command = Cli.Wrap("ping").WithArguments(new [] { "-t" });

if (_options.UseIPV6)
    command = command.WithArguments(new [] { "-6" });

Results in the command

ping -6

The current workaround is to do something like this:

var arguments = new List<string> { "-t" };

if (_options.UseIPV6)
    arguments.Add("-6");

var command = Cli.Wrap("ping").WithArguments(arguments);

which is a little cumbersome.

Tyrrrz commented 1 year ago

Why not do this?

var command = Cli.Wrap("ping").WithArguments(args =>
{
    args.Add("-t");
    if (_options.UseIPV6)
        args.Add("-6");
});

The builder overload was made specifically for these type of scenarios.

Tyrrrz commented 1 year ago

Going to close this issue, since the above suggestion should satisfy the highlighted use case. Let me know if there's another scenario where you'd need to extend arguments of an existing command.