On Sat, Mar 30, 2013 at 10:02 AM, Vincent van Ravesteijn <v...@lyx.org> wrote:
> Op 30-3-2013 6:08, Scott Kostyshak schreef:
>
>> The following command now throws an error:
>>
>>    lyx -dbg doesNotExist
>>
>> NOTE: THIS PATCH IS INCOMPLETE. I think I have the right condition
>> for detecting the error, but I don't know what to do with it.
>>
>> I see two ways of throwing an error:
>> (1) create a new enum value for Type, THROWERROR, which
>> LyX.cpp::parse_dbg() would test for on return and, if true,
>> it would handle the error message and exit
>>
>> (2) from LyX.cpp::parse_dbg(), test lowercase(arg) to make sure it is not
>> NONE (or 0).
>> Then, if that is true and if debug.cpp::value(arg) returns NONE, throw
>> the error.
>>
>> None of the two seems elegant to me so I'm guessing there is a better
>> way. Any ideas?
>>
>
> You can make a function
>
> bool Debug::isKnownValue(string const & val)
>
> Then in LyX.cpp::parse_dbg:
>
> if (Debug::isKnownValue(arg)) {
>     ..
> } else {
>     docstring const error_message =
>
>         bformat(_("The following debug flag does not exist:\n %1$s\n"
>             "Please see 'lyx -dbg' for a list of valid debug flags."),
>         from_utf8(tmp));
>     lyxerr << to_utf8(error_message) << endl;
>     exit(0);
> }
>
> Vincent

I think this is something I need to get used to: I would not have
thought that this duplication of code (e.g. reparsing the string in
the same way) was a good solution but I do see this a lot in C++ so
it's something I need learn.

What do you think of the attached patch?

Is the validation of the integer case acceptable?

+       if (isStrInt(val))
+               return val.substr(0, 1) != "-";

Thanks,

Scott
From d67b3ce436083b6384aaec4715e3a0aaf8a033e4 Mon Sep 17 00:00:00 2001
From: Scott Kostyshak <skost...@lyx.org>
Date: Sat, 30 Mar 2013 00:54:50 -0400
Subject: [PATCH] Nonexistent debug flag now gives error and exits

The following command now gives an error and exits with
code 1:

  lyx -dbg doesNotExist
---
 src/LyX.cpp           |   19 +++++++++++++++++++
 src/support/debug.cpp |   12 ++++++++++++
 src/support/debug.h   |    3 +++
 3 files changed, 34 insertions(+)

diff --git a/src/LyX.cpp b/src/LyX.cpp
index 55fb071..4c179f2 100644
--- a/src/LyX.cpp
+++ b/src/LyX.cpp
@@ -1012,6 +1012,25 @@ int parse_dbg(string const & arg, string const &, string 
&)
        }
        lyxerr << to_utf8(bformat(_("Setting debug level to %1$s"), 
from_utf8(arg))) << endl;
 
+       string v = arg;
+       while (!v.empty()) {
+               size_t const st = v.find(',');
+               string const tmp = ascii_lowercase(v.substr(0, st));
+               if (tmp.empty())
+                       break;
+               if (!Debug::isKnownValue(tmp)) {
+                               docstring const error_message =
+                                       bformat(_("The following debug flag is 
not valid:\n  %1$s\n"
+                                               "Please run 'lyx -dbg' for a 
list of valid debug flags."),
+                                       from_utf8(tmp));
+                               lyxerr << to_utf8(error_message) << endl;
+                               exit(1);
+               }
+               if (st == string::npos)
+                       break;
+               v.erase(0, st + 1);
+       }
+
        lyxerr.setLevel(Debug::value(arg));
        Debug::showLevel(lyxerr, lyxerr.level());
        return 1;
diff --git a/src/support/debug.cpp b/src/support/debug.cpp
index 3a6f901..e6d9688 100644
--- a/src/support/debug.cpp
+++ b/src/support/debug.cpp
@@ -115,6 +115,18 @@ string const Debug::name(Debug::Type val)
 }
 
 
+bool Debug::isKnownValue(string const & val)
+{
+       if (isStrInt(val))
+               return val.substr(0, 1) != "-";
+       else
+               for (int i = 0; i < numErrorTags; ++i)
+                       if (val == errorTags[i].name)
+                               return true;
+       return false;
+}
+
+
 Debug::Type Debug::value(string const & val)
 {
        Type l = Debug::NONE;
diff --git a/src/support/debug.h b/src/support/debug.h
index a883e67..960b589 100644
--- a/src/support/debug.h
+++ b/src/support/debug.h
@@ -105,6 +105,9 @@ namespace Debug {
 
        /// Return number of levels
        int levelCount();
+
+       /// Determine if a debug flag is valid
+       bool isKnownValue(std::string const & val);
  
        /// A function to convert debug level string names numerical values
        Type value(std::string const & val);
-- 
1.7.9.5

Reply via email to