]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
- improved error messages for wrong command line options and arguments (bsc#1150156) 550/head
authorArvin Schnell <aschnell@suse.de>
Mon, 31 Aug 2020 10:45:39 +0000 (12:45 +0200)
committerArvin Schnell <aschnell@suse.de>
Mon, 31 Aug 2020 10:45:39 +0000 (12:45 +0200)
client/GlobalOptions.cc
client/cmd-create.cc
client/cmd-delete.cc
client/cmd-modify.cc
package/snapper.changes

index 5b170c29d1b3ac0b844717e2db90b10fb1c02cba..703b5be3a00f079cde722c32ce4b81c8ba6e84cd 100644 (file)
@@ -211,7 +211,7 @@ namespace snapper
        Ambit ambit;
        if (!toValue(str, ambit, false))
        {
-           string error = sformat(_("Invalid ambit %s."), str.c_str()) + '\n' +
+           string error = sformat(_("Invalid ambit '%s'."), str.c_str()) + '\n' +
                sformat(_("Use %s, %s or %s."), toString(Ambit::AUTO).c_str(),
                        toString(Ambit::CLASSIC).c_str(), toString(Ambit::TRANSACTIONAL).c_str());
 
index 2dd8b6ee90745dc92a38a53ffc77f4a3e995c23b..2ca1413129e97e1d10e56ad93952ed1797967641 100644 (file)
@@ -23,6 +23,8 @@
 
 #include <iostream>
 
+#include <snapper/AppUtil.h>
+
 #include "utils/text.h"
 #include "GlobalOptions.h"
 #include "proxy.h"
@@ -73,11 +75,6 @@ namespace snapper
        };
 
        ParsedOpts opts = get_opts.parse("create", options);
-       if (get_opts.has_args())
-       {
-           cerr << _("Command 'create' does not take arguments.") << endl;
-           exit(EXIT_FAILURE);
-       }
 
        enum class CreateType { SINGLE, PRE, POST, PRE_POST };
 
@@ -105,8 +102,10 @@ namespace snapper
                type = CreateType::PRE_POST;
            else
            {
-               cerr << _("Unknown type of snapshot.") << endl;
-               exit(EXIT_FAILURE);
+               string error = sformat(_("Unknown type '%s'."), opt->second.c_str()) + '\n' +
+                   sformat(_("Use %s, %s, %s or %s."), "single", "pre", "post", "pre-post");
+
+               SN_THROW(OptionsException(error));
            }
        }
 
@@ -142,26 +141,27 @@ namespace snapper
 
        if (type == CreateType::POST && snapshot1 == snapshots.end())
        {
-           cerr << _("Missing or invalid pre-number.") << endl;
-           exit(EXIT_FAILURE);
+           SN_THROW(OptionsException(_("Missing or invalid pre-number.")));
        }
 
        if (type == CreateType::PRE_POST && command.empty())
        {
-           cerr << _("Missing command argument.") << endl;
-           exit(EXIT_FAILURE);
+           SN_THROW(OptionsException(_("Missing command option.")));
        }
 
        if (type != CreateType::SINGLE && !scd.read_only)
        {
-           cerr << _("Option --read-write only supported for snapshots of type single.") << endl;
-           exit(EXIT_FAILURE);
+           SN_THROW(OptionsException(_("Option --read-write only supported for snapshots of type single.")));
        }
 
        if (type != CreateType::SINGLE && parent != snapshots.getCurrent())
        {
-           cerr << _("Option --from only supported for snapshots of type single.") << endl;
-           exit(EXIT_FAILURE);
+           SN_THROW(OptionsException(_("Option --from only supported for snapshots of type single.")));
+       }
+
+       if (get_opts.has_args())
+       {
+           SN_THROW(OptionsException(_("Command 'create' does not take arguments.")));
        }
 
        switch (type)
index d120fc9d5e8e655c2b98810963cf3a5411c8403d..ffac05eeafdcffe19dae886c206e048db3a7cfac 100644 (file)
@@ -87,11 +87,6 @@ namespace snapper
        };
 
        ParsedOpts opts = get_opts.parse("delete", options);
-       if (!get_opts.has_args())
-       {
-           cerr << _("Command 'delete' needs at least one argument.") << endl;
-           exit(EXIT_FAILURE);
-       }
 
        bool sync = false;
 
@@ -100,6 +95,11 @@ namespace snapper
        if ((opt = opts.find("sync")) != opts.end())
            sync = true;
 
+       if (!get_opts.has_args())
+       {
+           SN_THROW(OptionsException(_("Command 'delete' needs at least one argument.")));
+       }
+
        ProxySnapshots& snapshots = snapper->getSnapshots();
 
        vector<ProxySnapshots::iterator> nums;
index d15d4c9f113c8ebaba5fa1e0a085ce0ead520b65..c3155630b447f7b0eebfb762fa988eea74df768e 100644 (file)
@@ -59,10 +59,10 @@ namespace snapper
        };
 
        ParsedOpts opts = get_opts.parse("modify", options);
+
        if (!get_opts.has_args())
        {
-           cerr << _("Command 'modify' needs at least one argument.") << endl;
-           exit(EXIT_FAILURE);
+           SN_THROW(OptionsException(_("Command 'modify' needs at least one argument.")));
        }
 
        ProxySnapshots& snapshots = snapper->getSnapshots();
index 5b30f076efe146e54c059397e0f2ca84d2356b9f..0e1e56f2c4110ba05cbd88cde6ce1103bc4f6a97 100644 (file)
@@ -1,7 +1,8 @@
 -------------------------------------------------------------------
 Fri Aug 28 11:06:23 CEST 2020 - aschnell@suse.com
 
-- improved error reporting in getopt wrapper (part of bsc#1150156)
+- improved error messages for wrong command line options and
+  arguments (bsc#1150156)
 
 -------------------------------------------------------------------
 Thu Aug 27 12:04:44 CEST 2020 - aschnell@suse.com