A couple questions about a simple project

John Colvin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Aug 17 08:17:38 PDT 2015


On Monday, 17 August 2015 at 15:05:56 UTC, Andre Polykanine wrote:
> Hi everyone,
> I'm  new to D (I'm learning it by reading the great online book 
> by Ali
> Çehreli  -  thank  you  very  much for it, sir!), and, more 
> than that,
> programming  is  my hobby, so please bear with me if I'm asking 
> stupid
> questions.
> I've  made  a  toy  project  which  is  a small command-line 
> XML files
> validator:
> https://github.com/Oire/dxv/
> and I have a couple questions about it:
> 1.  I'm  using std.getopt but don't know how to make it display 
> a help
> message if no options are present at all:
> D:\repos\git\dxv\dxv.exe
> (nothing   happens   but   I   would  like it to show the help 
> as with
> --help switch)

Check if args has changed length after calling getopt, if not 
then it didn't find any options.

> 2. As you can see, I check whether the file to validate can be 
> read. I
> tried both `try...catch` and `enforce` (current version:
> `string  s  =  enforce(cast(string)std.file.read(f),  "Unable  
> to read
> file");`
> ),  but  this  very  exception  for  some reason can be caught 
> only in
> `main()`.
> What am I missing?
> Thanks!

string s;
try
{
     s = cast(string)std.file.read(f);
}
catch (FileException e)
{
     // do whatever you want.
}



More information about the Digitalmars-d-learn mailing list