www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - A couple questions about a simple project

reply Andre Polykanine via Digitalmars-d-learn writes:
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)
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!
  

-- 
With best regards from Ukraine,
Andre
Skype: Francophile
Twitter:  m_elensule; Facebook: menelion
My blog: http://menelion.oire.org/
Aug 17 2015
next sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
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. }
Aug 17 2015
parent reply Andre Polykanine via Digitalmars-d-learn writes:
Hello John,

Yes, but this doesn't work, either.
Now I have this (that was my first variant, btw):

                                string s;
                                try {
                                        s = cast(string)std.file.read(f);
                                        try {
                                                check(s);
                                                if (this.verbose) {
                                                        output(format("%s:
validation passed!", f));
                                                }
                                        } catch(CheckException e) {
// This is output when a file is invalid, that works
                                                output(format("Failed to
validate %s: %s", f, e.toString()));
                                        }
                                } catch(Exception exc) {
//  And  that  isn't  output,  I  see  just  the  stacktrace as if the
exception was not caught at all
                                        output(format("Error reading %s: %s",
f, exc.msg));
                                }
                        }

To  catch  the  exception,  I  need  to  wrap  the  validate() call in
`try...catch` in `main()`. Why so?
Thanks!
-- 
With best regards from Ukraine,
Andre
Skype: Francophile
Twitter:  m_elensule; Facebook: menelion
My blog: http://menelion.oire.org/


------------ Original message ------------
From: John Colvin via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
To: digitalmars-d-learn puremagic.com
Date created: , 6:17:38 PM
Subject: A couple questions about a simple project


      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. }
Aug 17 2015
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 08/17/2015 10:08 AM, Andre Polykanine via Digitalmars-d-learn wrote:

                                  string s;
                                  try {
                                          s = 
cast(string)std.file.read(f);
                                          try {
                                                  check(s);
                                                  if (this.verbose) {
 
output(format("%s: validation passed!", f));
                                                  }
                                          } catch(CheckException e) {
 // This is output when a file is invalid, that works
 
output(format("Failed to validate %s: %s", f, e.toString()));
                                          }
                                  } catch(Exception exc) {
 //  And  that  isn't  output,  I  see  just  the  stacktrace as if the
 exception was not caught at all
                                          output(format("Error reading 
%s: %s", f, exc.msg));
                                  }
                          }
Regardless of whether it is the best solution here, you can have multiple catch blocks for a single try: try { // ... } catch(CheckException e) { // ... } catch(Exception exc) { // ... }
 To  catch  the  exception,  I  need  to  wrap  the  validate() call in
 `try...catch` in `main()`. Why so?
The reason is you don't try to make use of the provided file name until validate() starts executing. If it's important that the program notify the user right away, then you can call std.file.exists() to make sure that the file is there. Still, it does not try to open so it can't know whether the file will be readable later. The funny thing is, even though the upfront check sees a file as being present, it may disappear when it is being used or it may become unreadable. A better solution may be to open the files first and then pass File objects to validate().
 Thanks!
And thank you very much for the kind words! :) Ali
Aug 17 2015
prev sibling parent "FreeSlave" <freeslave93 gmail.com> writes:
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)
 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!
1. getopt modifies args array leaving not processed arguments in it, i.e. name of the program and positional arguments (those without leading - or --). If there're no command line arguments given, args array will contain the only one element - the name of executable. Therefore you can check for args.length == 1 after processing by getopt. 2. catch can handle only exception of type specified in its argument and derived. std.file.read throws FileException on fail, while you catch only CheckException. To cover all cases you can catch any exception by using Exception type (it's the base class for all exception classes), or write two catch-statements in a row for both FileException and CheckException. You don't need enforce here, unless you want to check if std.file.read returns null slice.
Aug 17 2015