A couple questions about a simple project
FreeSlave via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Aug 17 08:22:53 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)
> 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.
More information about the Digitalmars-d-learn
mailing list