Opt_arguments/3 positional args differ between saved state and non-saved state

If I create a saved state by

swipl `--stand_alone=true --foreign=save -o foo -c foo.pl

and run it:

foo --myoption=somevalue positional-param

and the code uses opt_arguments/3 to parse the command-line arguments, then PositionalArgs contains the executable as the first value (that is, PositionalArgs=[foo, 'positional-param']).

But if I run the program this way:

swipl -q foo..pl -- --myoption=somevalue positional-param

then PositionalArgs doesn’t contain the executable (that is, PositionalArgs=['positional-param']).

Is there a way to make the behavior of opt_arguments/3 the same for both saved state and non-saved state? (I could, of course, add a dummy arg when running under swipl …)

Please include foo.pl so we do not have to do work and guess. There are many ways t run the initial goal and get the arguments.

$ cat tt.pl
:- use_module(library(optparse), [opt_arguments/3]).

:- initialization(tt_main, main).

tt_main :-
    current_prolog_flag(argv, Argv),
    OptsSpec =
       [[opt(version), type(atom), default(''), longflags(['version']),
         help('Must be this version or later')]],
    opt_arguments(OptsSpec, Opts, PositionalArgs),
    format('Argv=~q~nOpts=~q~nPositionalArgs=~q~n',
           [Argv, Opts, PositionalArgs]).


$ swipl --stand_alone=true --foreign=save --undefined=error -o tt -c tt.pl

$ swipl -q ./tt.pl -- --version=v1 some_file
Argv=['--version=v1',some_file]
Opts=[version(v1)]
PositionalArgs=[some_file]

$ ./tt --version=v1 some_file
Argv=['./tt','--version=v1',some_file]
Opts=[version(v1)]
PositionalArgs=['./tt',some_file]

Thanks. The devil is indeed in the detail, in this case in the --stand_alone=true. Pushed 30498cd0a5fac0242926fd0b71d2e61f3b6024ee to fix this.

I tried rebuilding, to test this, and got the following (I haven’t rebuilt for a while, so I suspect this error is related to something else):

$ ninja
ninja: error: '../man/archive', needed by 'man/lib/prologpack.tex', missing and no known rule to make it

The build succeeded after I did mkdir ../man/archive; and the test succeeded. Thank-you!
(PS: I did give you all the information you needed in my first message; it showed the options I used for building the saved state. But in making a reproducible bug, I did some tracing through opt_arguments and noticed that current_prolog_flag(argv, Argv) wasn’t what I expected, so I added that to my test.)
(PPS: is there a “standard” extension for a saved state? cf: .qof)

You should have installed the package (submodule) archive. If new problems like this arise, do a git submodule update -init to get all modules installed. Watch out for conflicts and resolve these before continuing the build. These are commonly caused by old files that prohibit creating the submodule.
Next run the CMake in a clean directory.

Saved states are executables, so the convention depends on the platform. On Windows the system adds .exe. On other systems there is typically no convention. If you intend to load them explicitly into an embedded Prolog or using swipl -x state it might make sense to give them some extension. The system calls them .prc (boot.prc). Forgot why :slight_smile: