* * *

Author Topic: ShowException in CLI application  (Read 430 times)

francisco1844

  • New member
  • *
  • Posts: 15
ShowException in CLI application
« on: February 26, 2017, 06:29:19 am »
I am doing a CLI appliation and used  Lazarus option for CLI application which adds some code.
This is the part I am trying to figure out:
Code: Pascal  [Select]
  1.   ErrorMsg:=CheckOptions('h', 'help');
  2.   if ErrorMsg<>'' then begin
  3.     ShowException(Exception.Create(ErrorMsg));
  4.     Terminate;
  5.     Exit;
  6.   end;  

What I am trying to find out is how to show an error when the user sends the wrong parameter. Looked at the documentation for ShowException, http://www.freepascal.org/docs-html/rtl/sysutils/showexception.html, but I am a beginner and that page really doesn't tell me much of how to use it.

Any advantage of using ShowException vs something like
Code: Pascal  [Select]
  1.   if ErrorMsg<>'' then begin
  2.      writeln('Invalid parameter sent');
  3.      WriteHelp;
  4.     Terminate;
  5.     Exit;
  6.   end;

WriteHelp was a procedure already created as part of the template I got and there I plan to put basic parameter info for the program.

Any pointers greatly appreciated.

molly

  • Hero Member
  • *****
  • Posts: 1395
Re: ShowException in CLI application
« Reply #1 on: February 26, 2017, 12:13:47 pm »
Hi francisco1844,

I've read your question a couple of times now, and it completely eludes me what the actual question is. (i'm probably not awake enough)

From your writings i do understand that you probably have difficulty with fully understanding how this command-line checking parameters works ?

Quote
What I am trying to find out is how to show an error when the user sends the wrong parameter.
The checkOptions function does that for you.

You 'give' that function a list of short and long options/parameters and the function checks that against the actual provided command-line parameters. You should provide that function with a full complete list of all your parameters that you ever wish to support in your program (which for me personally is usually a limitation because the presence of one option often depends for me on whether or not another option is present).

The checkoptions function does describe how you can supply a list of your options, but can be a bit intimidating.

Because any given answer probably depends on what you want exactly, could you perhaps elaborate on what it is you have difficulty with understanding ?

In case you wish to show both the exception and the help to the user then just do so before terminating your application ?

« Last Edit: February 26, 2017, 12:15:37 pm by molly »

francisco1844

  • New member
  • *
  • Posts: 15
Re: ShowException in CLI application
« Reply #2 on: February 26, 2017, 05:06:21 pm »
I think I figured it out..

I was trying:
MyProgram BadParm

Instead of
MyProgram --BadParm
or
MyProgram -b

Since this the base CLI template, initially, only h and help are allowed so the above should indeed give errors.

Trying with --BadParm I get something that makes sense:
Exception at 0000000000000000: Exception:
Invalid option at position 1: "badparm".


Is there a way to customize that error? Ideally would like to show just:
Invalid option at position 1: "badparm"

I think I will end up with
Code: Pascal  [Select]
  1.   ErrorMsg:=CheckOptions('h', 'help');
  2.   if ErrorMsg<>'' then begin
  3.     ShowException(Exception.Create(ErrorMsg));
  4.      WriteHelp
  5.     Terminate;
  6.     Exit;
  7.   end;
  8.  

Quote
it completely eludes me what the actual question is. (i'm probably not awake enough)

Much more likely my question was just confusing.
Hopefully I will get better at both Pascal/Lazarus and asking questions.  8)

Doing an open source utility on my spare time which I will present to my boss to see if we use it at work.

Although several people, specially on reddit, were asking why on earth I would want to start learning Pascal.. trying Lazarus reminds me of how much I enjoyed working with Delphi many years back. Although I am no longer a developer, DBA now, I still find it very enjoyable to work on Pascal/Lazarus. It has been 10+ years since I used Delphi and even then only did it for about a year, so it is almost as if I am starting from scratch.

SkyKhan

  • Jr. Member
  • **
  • Posts: 97
Re: ShowException in CLI application
« Reply #3 on: February 26, 2017, 06:22:33 pm »
ShowException formats the error message and error location and then ( for console applications ) print these by writeln anyway.
You dont have to use it.

try this,
Code: Pascal  [Select]
  1. if ErrorMsg<>'' then begin
  2.     Writeln(ErrorMsg);
  3.     WriteHelp;
  4.     Terminate;
  5.     Exit;
  6. end;
  7.  

francisco1844

  • New member
  • *
  • Posts: 15
Re: ShowException in CLI application
« Reply #4 on: February 26, 2017, 10:31:40 pm »
Code: Pascal  [Select]
  1. then ( for console applications ) print these by writeln anyway.
  2. ...
  3. try this,

Thanks  SkyKhan that is exactly what I wanted.

So I would use ShowException if it was a GUI application?

SkyKhan

  • Jr. Member
  • **
  • Posts: 97
Re: ShowException in CLI application
« Reply #5 on: February 26, 2017, 10:56:55 pm »
Thanks  SkyKhan that is exactly what I wanted.

So I would use ShowException if it was a GUI application?

You're welcome. Yes, GUI apps normally does not have a console window to write so using writeln would cause more errors.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus