# Opt\_arguments/3 positional args differ between saved state and non-saved state

**URL:** <https://swi-prolog.discourse.group/t/opt-arguments-3-positional-args-differ-between-saved-state-and-non-saved-state/427>\
**Category:** General\
**Tags:** bug\
**Created:** [March 21, 2019, 10:14pm UTC](https://swi-prolog.discourse.group/t/opt-arguments-3-positional-args-differ-between-saved-state-and-non-saved-state/427 "2019-03-21T22:14:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [March 21, 2019, 10:14pm UTC](https://swi-prolog.discourse.group/t/opt-arguments-3-positional-args-differ-between-saved-state-and-non-saved-state/427/1 "2019-03-21T22:14:02Z")

</div>

If I create a saved state by

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

```

and run it:

```prolog
foo --myoption=somevalue positional-param

```

and the code uses [opt\_arguments/3](http://www.swi-prolog.org/pldoc/man?predicate=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:

```prolog
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` …)

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [March 22, 2019, 7:48am UTC](https://swi-prolog.discourse.group/t/opt-arguments-3-positional-args-differ-between-saved-state-and-non-saved-state/427/2 "2019-03-22T07:48:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [March 22, 2019, 3:04pm UTC](https://swi-prolog.discourse.group/t/opt-arguments-3-positional-args-differ-between-saved-state-and-non-saved-state/427/3 "2019-03-22T15:04:31Z")

</div>

```auto
$ 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]

```

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [March 22, 2019, 3:21pm UTC](https://swi-prolog.discourse.group/t/opt-arguments-3-positional-args-differ-between-saved-state-and-non-saved-state/427/4 "2019-03-22T15:21:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [March 22, 2019, 3:59pm UTC](https://swi-prolog.discourse.group/t/opt-arguments-3-positional-args-differ-between-saved-state-and-non-saved-state/427/5 "2019-03-22T15:59:20Z")

</div>

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):

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

```

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [March 22, 2019, 4:25pm UTC](https://swi-prolog.discourse.group/t/opt-arguments-3-positional-args-differ-between-saved-state-and-non-saved-state/427/6 "2019-03-22T16:25:06Z")

</div>

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`)

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [March 22, 2019, 4:34pm UTC](https://swi-prolog.discourse.group/t/opt-arguments-3-positional-args-differ-between-saved-state-and-non-saved-state/427/7 "2019-03-22T16:34:56Z")

</div>

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 🙂
