Recent

Author Topic: [SOLVED] Information needed on how to use program parameters  (Read 2577 times)

furious programming

  • Hero Member
  • *****
  • Posts: 853
[SOLVED] Information needed on how to use program parameters
« on: March 11, 2019, 04:43:01 pm »
Sometimes I meet with the extended structure of the program header with a defined list of untyped parameters. I have never used such a structure and I still do not do it, because I have no idea how it is used. For example:

Code: Pascal  [Select][+][-]
  1. program Test(Foo, Bar, Input, Output);
  2. begin
  3.  
  4. end.

What are the parameters of Foo and Bar and other? What type are they? How to use them?

I did not find any information in the documentation on this subject — the 16.1 Programs page does not contain any description, although the diagram shows a variation of the header with parameters. However, in the description is this fragment:

Quote
The program header is provided for backwards compatibility, and is ignored by the compiler.

If the program header is supported due to backward compatibility, then it should be possible to use these parameters, since the declaration of such a header does not cause a compilation error. But how?

I found an explanation of the purpose of these parameters in the manual from IrieToolsWhat are program parameters? — however, the examples given do not work correctly in the FPC (with {$OBJFPC} mode), although they compile without errors.

Does anyone know anything about this?
« Last Edit: March 11, 2019, 08:35:06 pm by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Information needed on how to use program parameters
« Reply #1 on: March 11, 2019, 04:53:39 pm »
Not sure what you are asking exactly.

But if you want to use command line parameter you need to use ParamCount and ParamStr.
See http://wiki.freepascal.org/Command_line_parameters_and_environment_variables

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode objfpc}{$H+}
  3. var
  4.   I: Integer;
  5. begin
  6.   WriteLn('Program: ', ParamStr(0));
  7.   for I := 1 to ParamCount do
  8.     WriteLn('Param ', I, ': ', ParamStr(I));
  9. end.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Information needed on how to use program parameters
« Reply #2 on: March 11, 2019, 04:59:13 pm »
When the FPC Reference says:

Quote from: Free Pascal Reference Guide
The program header is provided for backwards compatibility, and is ignored by the compiler.

It means exactly that: the header is parsed and accepted but nothing  is done with it. It's allowed, but ignored.

Note that the same is true of Turbo Pascal and Delphi (IIRC), and other Pascal compilers: they allow you to specify the full header but do nothing with it.

For those that do something about it, it's the equivalent of doing (if they are file types):
Code: [Select]
Assign(first, ParamStr[1]);
Assign(second, ParamStr[2]);
{...etc...}

ETA
This what the standard has to say about it:
Quote from: ISO/IEC 7185 :1990(E)
The identifier of the program-heading shall be the program name . It shall have no significance within the program . The identifiers contained by the program-parameter-list shall be distinct and shall be designated program-parameters . Each program-parameter shall have a defining-point as a variable-identifier for the region that is the program-block . The binding of the variables denoted by the program-parameters to entities external to the program shall be implementation-dependent, except if the variable possesses a file-type in which case the binding shall be implementation-defined.
« Last Edit: March 11, 2019, 05:18:02 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

furious programming

  • Hero Member
  • *****
  • Posts: 853
Re: Information needed on how to use program parameters
« Reply #3 on: March 11, 2019, 05:14:19 pm »
Not sure what you are asking exactly.

I'm asking exactly how to use the parameters defined in the program header and whether it is possible at all. I use ParamCount and ParamStr functions on a daily basis, but I'm curious if those headers can be used as well. Just curious.

It means exactly that: the header is parsed and accepted but nothing  is done with it. It's allowed, but ignored.

Since the program will compile, but it will not work as it is assumed, it's strange backwards compatibility… :D
« Last Edit: May 07, 2019, 04:33:59 am by furious programming »
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Information needed on how to use program parameters
« Reply #4 on: March 11, 2019, 05:22:27 pm »
Since the program will compile, but it will not work as it is assumed, it's strange backwards compatibility… :D

Maybe it works only in some modes? ISO? To be honest,  I don't really know. But yes it's a strange kind of "compatibility" :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Kays

  • Hero Member
  • *****
  • Posts: 569
  • Whasup!?
    • KaiBurghardt.de
Re: Information needed on how to use program parameters
« Reply #5 on: March 11, 2019, 05:25:25 pm »
[…] If the program header is supported due to backward compatibility, then it should be possible to use these parameters, since the declaration of such a header does not cause a compilation error. But how? […]
It is/will be usable in any ISO mode, e.g. {$mode ISO} or {$mode extendedPascal}, or if {$modeSwitch ISOprogramParams+} is set. You will then have to, in order to use your test program, for instance in a bash, specify all files like that:
Code: Bash  [Select][+][-]
  1. ./test 0</tmp/myFooFile /tmp/myGreatBar>1 2</dev/stdin /dev/stdout>3
Then you can use get and put as in a classical Pascal program, or read and write just as usual, the only difference to regular text files being, that you do not have to (and are not allowed to) assign/reset/rewrite/close the file (since this is done by the surrounding environment).

I do not know the current state of affairs, what's the development state in trunk, but this scheme is not supported in 3.0.4 I can tell.
« Last Edit: March 11, 2019, 05:36:08 pm by Kays »
Yours Sincerely
Kai Burghardt

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Information needed on how to use program parameters
« Reply #6 on: March 11, 2019, 05:41:39 pm »
I do not know the current state of affairs, what's the development state in trunk, but this scheme is not supported in 3.0.4 I can tell.

And I attest to that: Did a quick check in all modes and it didn't work in any of them. It always stops with an "Error 102" on the first Reset()
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

440bx

  • Hero Member
  • *****
  • Posts: 3944
Re: Information needed on how to use program parameters
« Reply #7 on: March 11, 2019, 07:05:21 pm »
I found an explanation of the purpose of these parameters in the manual from IrieToolsWhat are program parameters? — however, the examples given do not work correctly in the FPC (with {$OBJFPC} mode), although they compile without errors.

Does anyone know anything about this?
The concept of program parameters is a legacy concept.  Program abc(input, output) simply gave the program handles (named input and output) to perform console I/O.    In a GUI program input can come from various sources (instead of just one "input") and the same for the output, therefore having unique input and output handles doesn't apply anymore except when writing console applications.

In FPC, you get the input/output automatically when specifying {$APPTYPE CONSOLE}.

HTH.



(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

furious programming

  • Hero Member
  • *****
  • Posts: 853
Re: Information needed on how to use program parameters
« Reply #8 on: March 11, 2019, 08:34:35 pm »
Ok, now everything is clear. Thank you all for your answers.
Lazarus 3.2 with FPC 3.2.2, Windows 10 — all 64-bit

Working solo on an acrade, action/adventure game in retro style (pixelart), programming the engine and shell from scratch, using Free Pascal and SDL. Release planned in 2026.

 

TinyPortal © 2005-2018