【问题标题】:How do I pass a command line argument to an Octave function when calling function from the command line interpreter?从命令行解释器调用函数时,如何将命令行参数传递给 Octave 函数?
【发布时间】:2018-06-09 21:53:54
【问题描述】:

我有一个 Octave(CentOS 6.6 上的 v. 4.0.0)代码,我通常像 octave-cli mycode someargument 一样运行。 我想从 Octave 命令行解释器调试这段代码。这是一个最小的工作示例:

$ cat somefunc.m 
function somefunc
    printf("hello world\n");
    args = argv();
    printf("args = %s\n", args{1});
endfunction
somefunc();
printf("this is the end\n");

这段代码运行如下:

$ octave-cli somefunc.m somearg
hello world
args = somearg
this is the end

我希望能够从命令行调试此代码,并传递 somearg 以便 argv() 捕获它,例如

$ octave-cli
octave:1> dbstop somefunc 4;
octave:2> somefunc somearg
stopped in /some/path/somefunc.m at line 4
4:     printf("args = %s\n", args{1});
hello world
debug> dbcont
error: somefunc: A(I): index out of bounds; value 1 out of bound 0
error: called from
    somefunc at line 4 column 5
octave:2> 

但我不知道如何让argv() 读取命令行参数。我需要安装一个丑陋的开关吗?类似的东西:

if(is_interactive_mode)
    args = "somearg";
else
    args = argv(); 
end

在 Python 中我不会有这个问题,例如python -m pdb ./somefunc.py somearg 和 C(使用 Gdb)我会在 starting gdb 时传递参数,或者将传递给命令 run

问题:从 octave 命令行以交互方式运行程序时,如何将命令行参数传递给程序?

【问题讨论】:

    标签: debugging octave


    【解决方案1】:

    最好的方法是使用两个分开的文件:

    % somescript.m
    args = argv();
    printf("args = %s\n", args{1});
    somefunc(args{1});
    printf("this is the end\n");
    
    %somefunc.m
    function somefunc(func_arg)
        printf("hello world\n");
        printf("func_arg=%s\n",func_arg)
    endfunction
    

    然后,从您的操作系统命令行:

    $ octave-cli somescript.m somearg
    args = somearg
    hello world
    func_arg=somearg
    this is the end
    

    从 Octave 命令行:

    octave:2> somefunc("somearg")
    hello world
    func_arg=somearg
    

    请注意,在您的示例中,由于函数名与源文件名匹配,因此您将在源代码中调用该函数,并且永远不会到达代码的最后一部分(即“这是结束”)。

    【讨论】:

      猜你喜欢
      • 2012-08-13
      • 1970-01-01
      • 2015-04-17
      • 2014-07-22
      • 2013-12-23
      • 1970-01-01
      • 1970-01-01
      • 2020-01-06
      • 2013-03-18
      相关资源
      最近更新 更多