4

I have an Octave (v. 4.0.0 on CentOS 6.6) code that I normally run like octave-cli mycode someargument. I'd like to debug this code from the Octave command line interpreter. Here is a minimal working example:

$ 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");

This code runs like:

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

I'd like to be able to debug this code from the command line, and pass somearg such that argv() catches it e.g.

$ 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> 

But I cannot figure out how to get argv() to read the command line argument. Do I need to put in a some ugly switch? Something like:

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

In Python I wouldn't have this problem, e.g. python -m pdb ./somefunc.py somearg and in C (using Gdb) I'd pass the arguments when starting gdb or would be passed to the command run.

Question : How do I pass command line arguments to a program when running it interactively from the octave command line?

irritable_phd_syndrome
  • 4,631
  • 3
  • 32
  • 60

1 Answers1

3

The best way would be using two separated files:

% 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

Then, from your OS command line:

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

and from Octave command line:

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

Notice that in your example, as the function name matches the source file name, you will be calling the function inside the source and the last part of the code (i.e. "this is the end") would never be reached .

dariox
  • 303
  • 2
  • 11