Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing new lines in command line argument?

I loaded up a small console app that simply reads and prints the first command line argument passed to the program to the console.

I want to pass a newline to the argument I tried this:

prog.exe \n  --> outputs \n
prog.exe "sfs \n sfff" --> outputs sfs \n sfff
prog.exe "ff \\n ff" --> outputs ff \\n ff
prog .exe "ff \\\\n ff" --> outputs ff \\\\n ff

Is there some other escaping I'm suppose to use? Or is there some function I have to call on args[0] to process escaped characters before outputting it to the console?

To clarify, I'm trying to pass a string to my program that has newlines in it. The outputting to console was being used as a test. I could have just as easily inserted a debug break line and inspect the contents of the variable.

like image 966
user17753 Avatar asked Jul 11 '12 13:07

user17753


People also ask

How do I add a new line in CMD?

ENTER commandThe ENTER (or CR) command adds a line break to the email, directly in the command prompt. This technique allows to have multiple paragraphs separated by a new line character, in the expression that is written in a single line.

Can you pass command line arguments?

Command-line arguments are given after the name of the program in command-line shell of Operating Systems. To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments.

How do I pass a command line argument in command prompt?

For example, entering C:\abc.exe /W /F on a command line would run a program called abc.exe and pass two command line arguments to it: /W and /F.

How do you separate command line arguments?

Each argument on the command line is separated by one or more spaces, and the operating system places each argument directly into its own null-terminated string. The second parameter passed to main() is an array of pointers to the character strings containing each argument (char *argv[]).


1 Answers

The problem you're having is that the strings you get from the command line are not compiled. When you have string literals in your code they are compiled and this is when escape characters are processed and converted into newlines etc. You will probably have to do something like replacing all instances of "\n" in your input string with a newline character.

Edit:

From here (with minor modifications), this will compile your string using the CSharp compiler thus replacing the escape sequences with the appropriate characters:

    public static string ParseString(string input)
    {
        var provider = new Microsoft.CSharp.CSharpCodeProvider();
        var parameters = new System.CodeDom.Compiler.CompilerParameters()
        {
            GenerateExecutable = false,
            GenerateInMemory = true,
        };

        var code = @"
        public class TmpClass
        {
            public static string GetValue()
            {
                return """ + input + @""";
            }
        }";

        var compileResult = provider.CompileAssemblyFromSource(parameters, code);

        if (compileResult.Errors.HasErrors)
        {
            throw new ArgumentException(compileResult.Errors.Cast<System.CodeDom.Compiler.CompilerError>().First(e => !e.IsWarning).ErrorText);
        }

        var asmb = compileResult.CompiledAssembly;
        var method = asmb.GetType("TmpClass").GetMethod("GetValue");

        return method.Invoke(null, null) as string;
    }
like image 66
N_A Avatar answered Nov 14 '22 12:11

N_A