[folks] inspect: Split out validation of subcommands from a few commands
- From: Philip Withnall <pwithnall src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [folks] inspect: Split out validation of subcommands from a few commands
- Date: Wed, 25 Feb 2015 15:01:41 +0000 (UTC)
commit fa4f3ea1d7b1799caf7fd02b198cd9217992cc4d
Author: Philip Withnall <philip withnall collabora co uk>
Date: Wed Feb 25 14:58:53 2015 +0000
inspect: Split out validation of subcommands from a few commands
And expand the help output to list the expected subcommands.
tools/inspect/command-linking.vala | 20 ++++++++++----------
tools/inspect/command-set.vala | 16 ++++++++--------
tools/inspect/command-signals.vala | 13 ++++++++-----
tools/inspect/utils.vala | 21 +++++++++++++++++++++
4 files changed, 47 insertions(+), 23 deletions(-)
---
diff --git a/tools/inspect/command-linking.vala b/tools/inspect/command-linking.vala
index 366f13b..aa04fc9 100644
--- a/tools/inspect/command-linking.vala
+++ b/tools/inspect/command-linking.vala
@@ -24,6 +24,13 @@ using GLib;
private class Folks.Inspect.Commands.Linking : Folks.Inspect.Command
{
+ private const string[] _valid_subcommands =
+ {
+ "link-personas",
+ "link-individuals",
+ "unlink-individual",
+ };
+
public override string name
{
get { return "linking"; }
@@ -68,14 +75,9 @@ private class Folks.Inspect.Commands.Linking : Folks.Inspect.Command
parts = command_string.split (" ");
}
- if (parts.length < 1 ||
- (parts[0] != "link-personas" && parts[0] != "link-individuals" &&
- parts[0] != "unlink-individual"))
- {
- Utils.print_line ("Unrecognised 'linking' command '%s'.",
- command_string);
+ if (!Utils.validate_subcommand (this.name, command_string, parts[0],
+ Linking._valid_subcommands))
return;
- }
if (parts[0] == "link-personas" || parts[0] == "link-individuals")
{
@@ -301,9 +303,7 @@ private class Folks.Inspect.Commands.Linking : Folks.Inspect.Command
}
else
{
- subcommand_completions =
- { "link-personas", "link-individuals",
- "unlink-individual", null };
+ subcommand_completions = Linking._valid_subcommands;
prefix = "";
}
diff --git a/tools/inspect/command-set.vala b/tools/inspect/command-set.vala
index 670161c..dd2f83a 100644
--- a/tools/inspect/command-set.vala
+++ b/tools/inspect/command-set.vala
@@ -24,6 +24,11 @@ using GLib;
private class Folks.Inspect.Commands.Set : Folks.Inspect.Command
{
+ private const string[] _valid_subcommands =
+ {
+ "alias",
+ };
+
public override string name
{
get { return "set"; }
@@ -61,13 +66,9 @@ private class Folks.Inspect.Commands.Set : Folks.Inspect.Command
parts = command_string.split (" ");
}
- if (parts.length < 1 ||
- (parts[0] != "alias"))
- {
- Utils.print_line ("Unrecognised 'set' command '%s'.",
- command_string);
+ if (!Utils.validate_subcommand (this.name, command_string, parts[0],
+ Set._valid_subcommands))
return;
- }
if (parts[0] == "alias")
{
@@ -158,8 +159,7 @@ private class Folks.Inspect.Commands.Set : Folks.Inspect.Command
}
else
{
- subcommand_completions =
- { "alias", null };
+ subcommand_completions = Set._valid_subcommands;
prefix = "";
}
diff --git a/tools/inspect/command-signals.vala b/tools/inspect/command-signals.vala
index afff682..ead4584 100644
--- a/tools/inspect/command-signals.vala
+++ b/tools/inspect/command-signals.vala
@@ -44,6 +44,12 @@ using GLib;
private class Folks.Inspect.Commands.Signals : Folks.Inspect.Command
{
+ private const string[] _valid_subcommands =
+ {
+ "connect",
+ "disconnect",
+ };
+
public override string name
{
get { return "signals"; }
@@ -106,12 +112,9 @@ private class Folks.Inspect.Commands.Signals : Folks.Inspect.Command
/* Parse subcommands */
string[] parts = command_string.split (" ", 2);
- if (parts.length < 1)
- {
- Utils.print_line ("Unrecognised 'signals' command '%s'.",
- command_string);
+ if (!Utils.validate_subcommand (this.name, command_string, parts[0],
+ Signals._valid_subcommands))
return;
- }
Type class_type;
Object class_instance;
diff --git a/tools/inspect/utils.vala b/tools/inspect/utils.vala
index 5bbacb6..98879b3 100644
--- a/tools/inspect/utils.vala
+++ b/tools/inspect/utils.vala
@@ -621,4 +621,25 @@ private class Folks.Inspect.Utils
Utils.backend_name_iter = null;
return null;
}
+
+ /* Command validation code for commands which take a well-known set of
+ * subcommands. */
+ public static bool validate_subcommand (string command,
+ string? command_string, string? subcommand, string[] valid_subcommands)
+ {
+ if (subcommand != null && subcommand in valid_subcommands)
+ return true;
+
+ /* Print an error. */
+ Utils.print_line ("Unrecognised '%s' command '%s'.", command,
+ (command_string != null) ? command_string : "");
+
+ Utils.print_line ("Valid commands:");
+ Utils.indent ();
+ foreach (var c in valid_subcommands)
+ Utils.print_line ("%s", c);
+ Utils.unindent ();
+
+ return false;
+ }
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]