Recent

Author Topic: Does GetOption and HasOption support single - prefixes on multicharacter options  (Read 6380 times)

vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?
I want to use single '-' prefixes on command line options, but it seems that if you want to use more than one character for an option it has to be prefixed by 2 dashes , i e '--'.

eg. if I want a two character optiion, say 'aD' I have to make it '--aD' instead of '-aD'

Is that right?
Lazarus 3.0/FPC 3.2.2


Zoran

  • Hero Member
  • *****
  • Posts: 1830
    • http://wiki.lazarus.freepascal.org/User:Zoran
http://wiki.freepascal.org/Command_line_parameters_and_environment_variables#Check_for_a_parameter

I believe the original question is actually "If two or more short (one letter) options are given together with one dash (-aD), does Application.HasOption handle them same as if both of them are given separately (-a -D)", right?
If so, this wiki article does not answer it.
So, vfclist, would you please try experimenting it in your application and tell us here about your findings (and it would be nice to update the wiki to give clear answer for this).

vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?
I believe the original question is actually "If two or more short (one letter) options are given together with one dash (-aD), does Application.HasOption handle them same as if both of them are given separately (-a -D)", right?
If so, this wiki article does not answer it.
So, vfclist, would you please try experimenting it in your application and tell us here about your findings (and it would be nice to update the wiki to give clear answer for this).

This is some description  from the wiki.
Quote
With TCustomApplication you can access parameters by name. For example your program should print a help text when the user gave the common help parameter -h. The -h is a short option. The long form is the --help. To test whether the user called the program with -h or --help you can use

My question is whether the short form option can use 2 characters. For instance let's say I want to archive some files with an utility, the long form of the directory being --archive-directory=/somedir, can I use -ad somedir for the short option, rather than --ad  /somedir ? It seems to me that the long option prefixed with -- is meant to be plain unabbreviated descriptions seperated by dashes, so to use --ad for an abbreviated form doesn't seem to make sense. -a and -d may already be in use for other options so to use -ad to represent --archive-directory will be wrong.

In summary the question is whether it is possible to use a single dash prefix to represent an option without the library code expanding it into the separate short options its  characters are made of, ie treat -abc as a separate option abc rather than as -a -b -c.
Lazarus 3.0/FPC 3.2.2

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Is this what you asked?

Code: [Select]
if Application.HasOption('aD') then writeln ('HasOption("aD")');
 if Application.HasOption('a') then writeln ('HasOption("a")');
 if Application.HasOption('D') then writeln ('HasOption("D")');

Code: [Select]
C:\Users\Bart\LazarusProjecten>test -a
HasOption("a")
C:\Users\Bart\LazarusProjecten>test -D
HasOption("D")
C:\Users\Bart\LazarusProjecten>test -aD
HasOption("aD")

Tested with fpc 3.0.0

Bart

vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?
Yes, this is what I am want to check?

Is this what you asked?

Code: [Select]
if Application.HasOption('aD') then writeln ('HasOption("aD")');
 if Application.HasOption('a') then writeln ('HasOption("a")');
 if Application.HasOption('D') then writeln ('HasOption("D")');

Code: [Select]
C:\Users\Bart\LazarusProjecten>test -a
HasOption("a")
C:\Users\Bart\LazarusProjecten>test -D
HasOption("D")
C:\Users\Bart\LazarusProjecten>test -aD
HasOption("aD")

Tested with fpc 3.0.0

Bart
Lazarus 3.0/FPC 3.2.2

ASBzone

  • Hero Member
  • *****
  • Posts: 678
  • Automation leads to relaxation...
    • Free Console Utilities for Windows (and a few for Linux) from BrainWaveCC
As far as I can tell from the docs, multiple characters will not work, as the declaration for ShortOption calls for char, not string.

http://www.freepascal.org/docs-html/fcl/custapp/tcustomapplication.getoptionvalue.html

Current source in v3.0.2 shows the same:

    Function HasOption(Const S : String) : Boolean;
    Function HasOption(Const C : Char; Const S : String) : Boolean;


-ASB
-ASB: https://www.BrainWaveCC.com/

Lazarus v2.2.7-ada7a90186 / FPC v3.2.3-706-gaadb53e72c
(Windows 64-bit install w/Win32 and Linux/Arm cross-compiles via FpcUpDeluxe on both instances)

My Systems: Windows 10/11 Pro x64 (Current)

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
In general and this is documented the difference is:

- indicates a one character option and -- indicates a qualified named option, in other words: more descriptive..
This is a standard convention on UNIX and the likes.
e.g.:
This is a linker option in ld: 
-L DIRECTORY, which is the same as
--library-path DIRECTORY

That's all.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Roland57

  • Sr. Member
  • ****
  • Posts: 423
    • msegui.net
Hello! I am not sure to understand the question, but maybe TCommandLineReader could give you satisfaction.


My projects are on Gitlab and on Codeberg.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11452
  • FPC developer.
Hello! I am not sure to understand the question, but maybe TCommandLineReader could give you satisfaction.

Well, I didn't read further than the license and it was very unsatisfactory :-)

jmm72

  • Jr. Member
  • **
  • Posts: 79
  • Very experienced in being a beginner...
Actually the purpose of short options versus long options is not to have the long options be descriptive while short are mnemonic. Theorethically one has a full set of options, like 100 or 1000 options, in long form, and then have a subset in short form for ease. Since one can put several short options in one set like '-Cas', having short options of two or more characters would make things go awry. I don't remember where I did read this but I guess it was related to the standard getopts GNU package, which in turn I also guess is the standard/model that GetOptions and HasOption in TApplication use.
Lazarus 1.6.4 + FPC 3.0.2 64bits under Windows 7 64bits
Only as a hobby nowadays
Current proyect release: TBA

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
but I guess it was related to the standard getopts GNU package, which in turn I also guess is the standard/model that GetOptions and HasOption in TApplication use.
No! it is much older and stems from even before UNIX 5. It is as I described. No need to guess. (the docs may have you wrong-footed there.)
« Last Edit: March 29, 2017, 08:51:31 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Hello! I am not sure to understand the question, but maybe TCommandLineReader could give you satisfaction.
Why? We have the getopts unit.....
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018