bugGNU Octave - Bugs: bug #60291, Function parameters not supported...

 
 

bug #60291: Function parameters not supported when calling from the shell

Submitter:  Lars Kindermann <larskindermann>
Submitted:  Fri 26 Mar 2021 01:00:43 AM UTC
   
 
Category:  Documentation Severity:  1 - Wish
Priority:  3 - Low Item Group:  Feature Request
Status:  Fixed Assigned to:  None
Originator Name:  Open/Closed:  * Closed
Release:  * dev Operating System:  * GNU/Linux
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Thu 08 Apr 2021 04:50:37 PM UTC, comment #5: 

I added a new node and documentation for this functionality in this cset: http://hg.savannah.gnu.org/hgweb/octave/rev/7e987b69ebde.

Marking as fixed and closing report.

Rik <rik5>
Group administrator
Fri 02 Apr 2021 01:56:53 AM UTC, comment #4: 

@Rik, thanks, that's of course the obvious solution for writing shell- and Octave-callable functions. I suggest to add your hint to chapter "2.6 Executable Octave Programs" of the doc, as it may be helpful for other people like me who will not figure this out by themselves. E.g:


To write m-files that can act as executable programs when called from the shell and also as normal functions within Octave, default input arguments set by the argv() function can be used.
When a function is called from the shell, Octave will not pass anything to the function inputs, so the default would be used instead:

#!/usr/local/bin/octave -qf
function retval = mysin (x = str2double (argv(){end}))
  retval = sin (x)
endfunction

This can be called from the shell with

    mysin.m 5

or from Octave with

    mysin (5)

Lars Kindermann <larskindermann>
Thu 01 Apr 2021 09:15:05 PM UTC, comment #3: 

This sounds confusing, and I don't think I'd want to do it as a general part of Octave.  But if you really want to for your own code, couldn't you just use default input arguments?  When called from the command line there would be no function input so the default would be used.  When called from Octave with an argument the argument would be overriden.

For example, the function mysin.m which calculates the sine of a value.


#!/usr/local/bin/octave -qf
function retval = mysin (x = str2double (argv(){end}))
  retval = sin (x)
endfunction


This can be called from the command line with


mysin.m 5


or from Octave with


mysin (5)


Rik <rik5>
Group administrator
Fri 26 Mar 2021 06:53:02 AM UTC, comment #2: 

I understand the current method of creating shell-executable scripts, using the argv() function to access parameters.

But these won't work from the Octave command line as argv() here returns the parameters from the initial Octave invocation.

That's all well documented.

I was suggesting to also allow Octave functions/commands to be called from the shell, so behaviour is identical to the octave prompt and they can be used both ways.


Lars Kindermann <larskindermann>
Fri 26 Mar 2021 01:51:40 AM UTC, comment #1: 

The argv function does return the command line arguments passed to the executable script.  But Octave is expecting to execute a script file, not call a function.  Your script might define some functions that are used later, but it shouldn't begin with the "function" keyword.

John W. Eaton <jwe>
Group administrator
Fri 26 Mar 2021 01:00:43 AM UTC, original submission:  

Calling an executable function "otest" from the shell


#!/usr/bin/octave -qf
function out=otest(in)
nargin
argv
in


results in


$ ./otest foo
ans = 0
ans =
{
  [1,1] = foo
}

error: 'in' undefined near line 5, column 5
error: called from
    otest at line 5 column 1


Would it be difficult to pass the command line arguments to the function arguments? This would greatly enhance the usability of Octave scripts for shell programming.

Lars Kindermann <larskindermann>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by larskindermann (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 5 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2021-04-08 rik5 CategoryInterpreter Documentation
        StatusNone Fixed
        Open/ClosedOpen Closed
    2021-04-01 rik5 Severity3 - Normal 1 - Wish
        Priority5 - Normal 3 - Low

    Back to the top

    Powered by Savane 3.13-758e.
    Corresponding source code