简体   繁体   English

重定向用于引导的命令行参数

[英]Redirecting Command-line Arguments for Bootstrapping

I am trying to rewrite the following program in C instead of C# (which is less portable).我正在尝试用 C 而不是 C#(可移植性较差)重写以下程序。 It is obvious that "int system ( const char * command )" will be necessary to complete the program.很明显,完成程序需要“int system (const char * command)”。 Starting it with "int main ( int argc, char * argv[] )" will allow getting the command-line arguments, but there is still a problem that is difficult to understand.以“int main (int argc, char * argv[] )”开头将允许获取命令行参数,但仍然存在一个难以理解的问题。 How do you successfully escape arguments with spaces in them?你如何成功地转义带有空格的参数? In the program below, arguments with spaces in them (example: screensaver.scr "this is a test") will be passed to the script as separate arguments (example: screensaver.scr this is a test) and could easily cause problems.在下面的程序中,其中包含空格的参数(例如:screensaver.scr“这是一个测试”)将作为单独的参数传递给脚本(例如:screensaver.scr 这是一个测试)并且很容易导致问题。

namespace Boids_Screensaver
{
    static class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            System.Diagnostics.Process python = new System.Diagnostics.Process();
            python.EnableRaisingEvents = false;
            python.StartInfo.FileName = "C:\\Python31\\pythonw.exe";
            python.StartInfo.Arguments = "boids.pyw";
            foreach (string arg in args)
            {
                python.StartInfo.Arguments += " " + arg;
            }
            python.Start();
        }
    }
}

Windows is all messed up.窗户全乱了。 Every program has its own rules.每个程序都有自己的规则。

The correct way to do this under windows is to use _spawnv在windows下正确的做法是使用_spawnv

Its equivalent under unix like OSes is fork() followed by execv .它在类似操作系统的 Unix 下的等价物是fork()后跟execv

screensaver.scr "spaced argument" nonspaced_argument

argc = 2
argv[0] = "screensaver.scr"
argv[1] = "spaced argument"
argv[2] = "nonspaced_argument"

Sorry my English :).对不起我的英语:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM