views:

317

answers:

5

I have a C# command-line application that I need to run in windows and under mono in unix. At some point I want to launch a subprocess given a set of arbitrary paramaters passed in via the command line. For instance:

Usage: mycommandline [-args] -- [arbitrary program]

Unfortunately, System.Diagnostics.ProcessStartInfo only takes a string for args. This is a problem for commands such as:

./my_commandline myarg1 myarg2 -- grep "a b c" foo.txt

In this case argv looks like :

argv = {"my_commandline", "myarg1", "myarg2", "--", "grep", "a b c", "foo.txt"}

Note that the quotes around "a b c" are stripped by the shell so if I simply concatenate the arguments in order to create the arg string for ProcessStartInfo I get:

args = "my_commandline myarg1 myarg2 -- grep a b c foo.txt"

Which is not what I want.

Is there a simple way to either pass an argv to subprocess launch under C# OR to convert an arbitrary argv into a string which is legal for windows and linux shell?

Any help would be greatly appreciated.

A: 

You will need to run a new subprocess using grep and all arguments that grep will be needing.

void runProcess(string processName, string args)
{
    using (Process p = new Process())
    {
        ProcessStartInfo info = new ProcessStartInfo(processName);
        info.Arguments = args;
        info.RedirectStandardInput = true;
        info.RedirectStandardOutput = true;
        info.UseShellExecute = false;
        p.StartInfo = info;
        p.Start();
        string output = p.StandardOutput.ReadToEnd();
        // process output
    }
}

then make a call to runProcess("grep", "a", "b", "c", "foo.txt");

Edit: Updated args handling.

Jake
This won't work if any arguments in the `args` array contain whitespace. Also, from your calling syntax, did you mean to declare `args` as a `params` array? Because the example as you have it can't be used the way you show.
Daniel Pryden
@Daniel Pryden You're correct about the params, and I suppose it would be up to the developer to modify this script to the point of using it in his own code. This post should be seen more as a general guide for how to launch a subprocess than a fully-functioning program to be integrated verbatim into your current system.
Jake
A: 

MSDN has a description of how the MS Visual C Runtime parses the string returned by GetCommandLine() into an argv array.

You might also be interested in the list2cmdline() function from the Python standard library that is used by Python's subprocess module to emulate the Unix argv behavior in a Win32 environment.

Daniel Pryden
A: 

Just use a Regex to check if a string has spaces of any kind, and replace the original string with a new one with quotes:

using System.Text.RegularExpressions;
// ...
for(int i=0; i<argv.Length; i++) {
    if (Regex.IsMatch(i, "(\s|\")+")) {
        argv[i] = "\"" + argv[i] + "\"";
        argv[i].Replace("\"", "\\\"");
    }
}
supercheetah
This won't work if the string has quotes in it: ie "a \" b" produces the arg, a " b, which if surrounded by quotes is "a " b " which is both wrong and an illegal string.
lucas
Well, then you just need to expand the regex, and add a string.replace.
supercheetah
+1  A: 

In windowsland, it's simple really...add extra quotation marks in the string you pass to the System.Diagnostics.ProcessStartInfo object.

e.g. "./my_commandline" "myarg1 myarg2 -- grep \"a b c\" foo.txt"

David Heise
Should work fine in all environments; it's what I use in cygwin. Make sure your string is actually using escaped backslashes - so, literally in your code, you would have @"myarg1 myarg2 -- grep \""a b c\"" foo.txt" or "myarg1 myarg2 -- grep \\\"a b c \\\" foo.txt".
mattdekrey
A: 

Thanks to all for the suggestions. I ended up using the algorithm from shquote (http://www.daemon-systems.org/man/shquote.3.html).

/**
 * Let's assume 'command' contains a collection of strings each of which is an
 * argument to our subprocess (it does not include arg0).
 */
string args = "";
string curArg;
foreach (String s in command) {
    curArg = s.Replace("'", "'\\''"); // 1.) Replace ' with '\''
    curArg = "'"+curArg+"'";          // 2.) Surround with 's
    // 3.) Is removal of unnecessary ' pairs. This is non-trivial and unecessary
    args += " " + curArg;
}

I've only tested this on linux. For windows you can just concatenate the args.

lucas