Recent

Author Topic: Console app, parsing command line.  (Read 2360 times)

Prime

  • Jr. Member
  • **
  • Posts: 62
Console app, parsing command line.
« on: March 22, 2018, 05:28:41 pm »
Hi all,

Running Laz 1.8.2 on 64 bit windows 7.

I'm having a few problems with handling command line parameters, specifically the long options.

So my program can accept a whole bunch of parameters, which I have defined as constants for ease of configuration. I have also combined these into a string suitable for feeding into GetNonOptions.

Code: [Select]
    {Short form options}
    OptSFCount  = 'c';          // Over-ride catalog filecount
    OptSDFSName = 'd';          // Specify DFS filename to read / write
    OptSExec    = 'e';          // Specify Execution address when writing
    OptSFile    = 'f';          // Specify host file to read / write
    OptSHelp    = 'h';          // Get some help
    OptSLoad    = 'l';          // Specify load address when writing
    OptSLabel   = 'L';          // Set disk label when creating
    OptSOpt     = 'o';          // Set disk opt when creating
    OptSQual    = 'q';          // Specify qualifier, defaults to '$'
    OptSTracks  = 't';          // Specify max tracks when writing (40 or 80).

    {Long form options}
    OptLFCount  = 'count';
    OptLDFSName = 'dfs';
    OptLExec    = 'exec';
    OptLFile    = 'file';
    OptLHelp    = 'help';
    OptLLoad    = 'load';
    OptLLabel   = 'label';
    OptLOpt     = 'opt';
    OptLQual    = 'qual';
    OptLTracks  = 'tracks';

    {Short and long opts as strings for use in CheckOpts}
    ShortOpts   : string = OptSFCount+  ':'+
                           OptSDFSName+ ':'+
                           OptSExec+    ':'+
                           OptSFile+    ':'+
                           OptSHelp+
                           OptSLoad+    ':'+
                           OptSLabel+   ':'+
                           OptSOpt+     ':'+
                           OptSQual+    ':'+
                           OptSTracks+  ':';

    LongOpts    : string = OptLFCount+  ' '+
                           OptLDFSName+ ' '+
                           OptLExec+    ' '+
                           OptLFile+    ' '+
                           OptLHelp+    ' '+
                           OptLLoad+    ' '+
                           OptLLabel+   ' '+
                           OptLOpt+     ' '+
                           OptLQual+    ' '+
                           OptLTracks+  ' ';

    {Valid operations}
    CmdCreate   = 'create';                     // Create a new disk image
    CmdRead     = 'read';                       // Transfer DFS file to host
    CmdWrite    = 'write';                      // Transfer host file to DFS
    CmdCat      = 'cat';                        // Catalog the disk
    CmdDump     = 'dump';                       // Dump the file in hex

    NoCmds      = 5;


My program is designed to be invoked with two manditory parameters, a command and a filename, however some of the commands can take optional parameters that I am specifying. So to get the list of *NON* options so that I can extract and validate the command and filename I use

Code: [Select]
  // quick check parameters
  ErrorMsg:=CheckOptions(ShortOpts, LongOpts);

  // parse parameters
  if HasOption(OptSHelp, OptLHelp) then
  begin;
    DoHelp;
//    Terminate;
    Exit;
  end;

  { add your program here }
  { Get parameters that are not options}
  GetNonOptions(ShortOpts, LongOpts, CmdParams);

  {Get command and DFS disk name}
  IF (CmdParams.Count > 1) THEN
  BEGIN;
    OpStr:=CmdParams.Strings[0];
    DFSImageName:=CmdParams.Strings[1];
  END
  ELSE
    ErrorMsg:=ErrorMsg+'Error must specify at least one operation and DFSImageFile';


In my DoRun procedure, however this exits with a parameter error when called :
Code: [Select]
F:\Phill\Dropbox\AtomDOS\DFSdisk>dfsdisk create test.ssd --tracks=80
Exception at 000000010002A4FA: EListError:
Invalid option at position 3: "tracks".

Anyone have any idea what is going on here?

Using the short option works without problems.

Cheers.

Phill.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Console app, parsing command line.
« Reply #1 on: March 22, 2018, 05:50:31 pm »
Please provide the code that we can compile, run and test. If you're not willing to publicize the code, you can write a demo that showing that issue.

If you want to debug it yourself, the culprit should be on where you're handling the TList or TString. You can try to enable all the checkings in the compiler options to help you do the debugging:
Lazarus main menu > Project > Project Options > Debugging

Read more about EListError:
https://www.freepascal.org/docs-html/rtl/classes/elisterror.html

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Console app, parsing command line.
« Reply #2 on: March 22, 2018, 06:09:50 pm »
Anyone have any idea what is going on here?
There may be an error in the procedure code, or maybe in this:
Code: Pascal  [Select][+][-]
  1. ...
  2.     {Short and long opts as strings for use in CheckOpts}
  3.     ShortOpts   : string = OptSFCount+  ':'+
  4.                            OptSDFSName+ ':'+
  5.                            OptSExec+    ':'+
  6.                            OptSFile+    ':'+
  7.                            OptSHelp+         // where is ':'  ?
  8.                            OptSLoad+    ':'+
  9. ...
  10.  


Prime

  • Jr. Member
  • **
  • Posts: 62
Re: Console app, parsing command line.
« Reply #3 on: March 22, 2018, 06:51:18 pm »
Bingo!

I've worked out what the problem is, there is a variant of CheckOptions that takes a string with all the long options in, however there *ISNT* one of GetNonOptions, both variants take an array of string, needless to say feeding it an array of string (rather than a string) allows it to work. I may just switch to using the array of string variant of CheckOptions, as it will avoid future confusion.

Cheers.

Phill.

 

TinyPortal © 2005-2018