简体   繁体   English

论据]问题Objective-C

[英]arguments] Problem Objective-C

I am having trouble with NSProcessInfo's arguments property.我在使用NSProcessInfo's arguments属性时遇到问题。 I am creating a command line tool that needs to decode base64 code that it has been passed from the internet using a PHP script, along with some other arguments.我正在创建一个命令行工具,该工具需要使用 PHP 脚本以及其他一些 arguments 对从 Internet 传递的 base64 代码进行解码。 The data is passed fine, but for some reason.数据传递得很好,但出于某种原因。 [[NSProcessInfo processInfo] arguments] returns 21 arguments, even though I pass just one base64 string. [[NSProcessInfo processInfo] arguments]返回 21 arguments,即使我只传递了一个 base64 字符串。

Here's the objective-c side of it:这是它的 objective-c 方面:

NSArray *arguments = [[NSProcessInfo processInfo] arguments];

if ([[arguments objectAtIndex:1] isEqualToString:@"-s"])
{
    if ([arguments objectAtIndex:2] == nil)
    {
        printf("Error: No data\n");
        [pool drain];
        return 0;
    }

    NSString*data = [arguments objectAtIndex:2];

    if ([data length] == 0)
    {
        printf("Error: No data\n");
        [pool drain];
        return 0;
    }

    NSString*password = @"";

    if ([[arguments objectAtIndex:3] isEqualToString:@"-p"])
    {
        if ([arguments objectAtIndex:4] == nil)
        {
            printf("Error: No password\n");
            [pool drain];
            return 0;
        }
        else
        {
            password = [NSString stringWithString:[arguments lastObject]];
        }
    }

NSLog(@"Args: %i\n\n",[arguments count]); //returns 21? I expect 3.

The base64 code is a bit long, so I've put it here . base64 代码有点长,所以放在这里 Does anyone know why this code returns this many arguments?有谁知道为什么这段代码会返回这么多 arguments? It's supposed to be just one string?应该只有一串吧? Edit: I am stripping whitespaces in my PHP script.编辑:我在 PHP 脚本中删除空格。 See here:看这里:

<?php

$url = $_GET['data'];

$query = "/Library/WebServer/email/emailsender -s";
$password = "-p somePassword";

$commandStr = trim("$query $url $password");

$commandStr = removeNewLines($commandStr);

echo $commandStr;

$output = shell_exec($commandStr);

echo "<pre>Output: $output</pre>";

function removeNewLines($string) {

    $string = str_replace( "\t", ' ', $string );
    $string = str_replace( "\n", ' ', $string );
    $string = str_replace( "\r", ' ', $string );
    $string = str_replace( "\0", ' ', $string );
    $string = str_replace( "\x0B", ' ', $string );

    return $string;

}

?>

When I display the Base64 string on your pastie page as "raw" I see a lot of spaces in it.当我在您的粘贴页面上将 Base64 字符串显示为“原始”时,我看到其中有很多空格。 So most likely the arguments is correct and your PHP script is calling the Objective-C program the wrong way.所以很可能arguments是正确的,而您的 PHP 脚本以错误的方式调用 Objective-C 程序。 An easy fix might be to just strip out any whitespace before passing the string, or properly escape it.一个简单的解决方法可能是在传递字符串之前去掉任何空格,或者正确地转义它。

When you send arguments to a program through the command-line, each argument is separated by a whitespace character.当您通过命令行将 arguments 发送到程序时,每个参数都由空格字符分隔。 This means that if you post a string that contains spaces, your program will interpret it as many arguments.这意味着如果您发布一个包含空格的字符串,您的程序会将其解释为许多 arguments。 To prevent this behavior, you need to quote your strings.为了防止这种行为,您需要引用您的字符串。

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

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