Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2002-07

Re: About GetOpts and configuration file parsing

7 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Jonas Maebe
to Aitor Santamaría Merino: Two questions: (1) Can I reconfigure GetOpts unit somehow in order to use the / character…
Aitor Santamaría Merino wrote
(1) Can I reconfigure GetOpts unit somehow in order to use the / character instead of - to start an option?

I don't think so. Afaik, it's a port from a GNU package and they always use - and never /, even under Dos/Windows.

(2) Do you know of programs for parsing configuration files of the style of WIN.INI? is any of these included within the FPC distributions? (I can't find any nor in units.pdf neither in the 'packages' at http://www.freepascal.org/packages/packages.html).

There's a unit called inifiles in the FCL. It's not documented however and personally, I've never used it.

Jonas

Reply by email

Aitor Santamaría Merino

Hi,

Two questions:
(1) Can I reconfigure GetOpts unit somehow in order to use the / character instead of - to start an option?

(2) Do you know of programs for parsing configuration files of the style of WIN.INI? is any of these included within the FPC distributions? (I can't find any nor in units.pdf neither in the 'packages' at http://www.freepascal.org/packages/packages.html).

Aitor

Reply by email

Yet Another Geek
(*

If your program only requires short (single-character) options,
then you can just add this line at the beginning of your program:

    OptSpecifier:=['/'];

Not sure how you would handle multi-char (string)
options, though.

The problem is that the getopts unit follows the POSIX

standard, and most DOS programs do not .

The getopts unit decides if an option is a string
or a set of single-character options by whether
the option begins with "-" or "--"

For instance, if the options are:

-a : Show the current date.
-b : Show the current time.
-c : Destroy the hard drive.
--abc : Tell a funny joke.

Then obviously, "-abc" and "--abc"
will have completely different results !

So to use slashes with a mixure of single-char (short) options
and multi-char (long) options you would need to
specify your
long options with '//' on the command line. This would work,
but it would look a little bit strange :

*)

program slashopt;
var
  I: integer;
begin
  { convert options from '/' to '-' before parsing ...
}
  for I:=1 to argc -1  do begin
    if ( argv[I]  nil ) and ( argv[I][0] = '/' )
then  begin
      argv[I][0] := '-' ;
      if ( argv[I][1] = '/' ) then  argv[I][1] := '-'
    end;
  end;
  { ... Now your command line is ready for the getopts
unit }
end.

- Jeff

__________________________________________________ Do You Yahoo!?
Sign up for SBC Yahoo! Dial - First Month Free
http://sbc.yahoo.com

Reply by email

Aitor Santamaría
to Yet Another Geek

Hi,

Show quoted text 12 lines
program slashopt; var I: integer; begin { convert options from '/' to '-' before parsing ... } for I:=1 to argc -1 do begin if ( argv[I] nil ) and ( argv[I][0] = '/' ) then begin argv[I][0] := '-' ; if ( argv[I][1] = '/' ) then argv[I][1] := '-'

well, I am not planning to allow -- at all, I don't like it, so I suppose I can skip that row.

Show quoted text 7 lines
end; end; { ... Now your command line is ready for the getopts unit } end.

do you mean that any FPC program has defined argc (I suppose ParamCount returns this value, then), and argv as an array of PChars, that can be modified?

Aitor

Reply by email

Yet Another Geek
well, I am not planning to allow -- at all, I don't

like it,

so I suppose I can skip that row.

You can skip all of the argc and argv stuff, then - All you need is this one line:

> OptSpecifier:=['/'];

This tells getopts to use '/' for all single-char
options.

do you mean that any FPC program has defined argc

- YES.

(I suppose ParamCount returns this value, then)

"ParamCount" does not include the program name in its count,
but "argc" does. Thus "ParamCount" is always one less than "argc"
Both of the indexes are zero-based .
( The program name is index zero )

Also note that Paramstr(0) returns the fully-qualified path to
the executable, where argv[0] returns the actual
command entered
on the command line (it may or may not include the path).

So if you just typed "myprog" at the command prompt, argv[0] would be "myprog",
ParamStr(0) would be "C:\path\to\myprog.exe"

and argv as an array of PChars that can be modified?

- YES.

All of this is at least true for Linux,
I have not tested it with Windows or DOS.
- I will leave that up to you ;-)

__________________________________________________ Do You Yahoo!?
Yahoo! Autos - Get free new car price quotes
http://autos.yahoo.com

Reply by email