diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-12-21 17:59:34 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-12-21 17:59:34 +0100 |
commit | dd68f2d20c4340bf31db960df558a7d9f603bb3a (patch) | |
tree | e1328a24539ac5b983129bbf4f76be837a18950a /sinksh | |
parent | 18e6abad3669c2c0ef55922543b4033b031893ff (diff) | |
download | sink-dd68f2d20c4340bf31db960df558a7d9f603bb3a.tar.gz sink-dd68f2d20c4340bf31db960df558a7d9f603bb3a.zip |
sinksh list commandline arguments
Diffstat (limited to 'sinksh')
-rw-r--r-- | sinksh/syntax_modules/sink_list.cpp | 56 | ||||
-rw-r--r-- | sinksh/syntaxtree.cpp | 27 | ||||
-rw-r--r-- | sinksh/syntaxtree.h | 6 |
3 files changed, 60 insertions, 29 deletions
diff --git a/sinksh/syntax_modules/sink_list.cpp b/sinksh/syntax_modules/sink_list.cpp index 8c59064..1e15dd3 100644 --- a/sinksh/syntax_modules/sink_list.cpp +++ b/sinksh/syntax_modules/sink_list.cpp | |||
@@ -37,6 +37,8 @@ | |||
37 | #include "state.h" | 37 | #include "state.h" |
38 | #include "syntaxtree.h" | 38 | #include "syntaxtree.h" |
39 | 39 | ||
40 | SINK_DEBUG_AREA("sinksh_list") | ||
41 | |||
40 | namespace SinkList | 42 | namespace SinkList |
41 | { | 43 | { |
42 | 44 | ||
@@ -53,50 +55,46 @@ static QByteArray compressId(bool compress, const QByteArray &id) | |||
53 | return compactId.first(); | 55 | return compactId.first(); |
54 | } | 56 | } |
55 | 57 | ||
58 | QByteArray baIfAvailable(const QStringList &list) | ||
59 | { | ||
60 | if (list.isEmpty()) { | ||
61 | return QByteArray{}; | ||
62 | } | ||
63 | return list.first().toUtf8(); | ||
64 | } | ||
65 | |||
56 | bool list(const QStringList &args_, State &state) | 66 | bool list(const QStringList &args_, State &state) |
57 | { | 67 | { |
58 | if (args_.isEmpty()) { | 68 | if (args_.isEmpty()) { |
59 | state.printError(QObject::tr("Please provide at least one type to list (e.g. resource, ..")); | 69 | state.printError(QObject::tr("Options: $type [--resource $resource] [--compact] [--filter $property=$value] [--showall|--show $property]")); |
60 | return false; | 70 | return false; |
61 | } | 71 | } |
62 | 72 | ||
63 | auto args = args_; | 73 | auto options = SyntaxTree::parseOptions(args_); |
64 | 74 | ||
65 | auto type = args.takeFirst(); | 75 | auto type = options.positionalArguments.isEmpty() ? QString{} : options.positionalArguments.first(); |
66 | 76 | ||
67 | if (!type.isEmpty() && !SinkshUtils::isValidStoreType(type)) { | 77 | if (type.isEmpty() || !SinkshUtils::isValidStoreType(type)) { |
68 | state.printError(QObject::tr("Unknown type: %1").arg(type)); | 78 | state.printError(QObject::tr("Unknown type: %1").arg(type)); |
69 | return false; | 79 | return false; |
70 | } | 80 | } |
71 | 81 | ||
72 | Sink::Query query; | 82 | Sink::Query query; |
73 | 83 | ||
74 | auto filterIndex = args.indexOf("--filter"); | 84 | if (options.options.contains("resource")) { |
75 | if (filterIndex >= 0) { | 85 | query.resourceFilter(baIfAvailable(options.options.value("resource"))); |
76 | args.removeAt(filterIndex); | ||
77 | for (int i = 1; i < filterIndex; i++) { | ||
78 | query.resourceFilter(args.at(i).toLatin1()); | ||
79 | } | ||
80 | for (int i = filterIndex + 1; i < args.size(); i++) { | ||
81 | auto filter = args.at(i).split("="); | ||
82 | query.filter(filter.at(0).toLatin1(), QVariant::fromValue(filter.at(1))); | ||
83 | } | ||
84 | |||
85 | } | 86 | } |
86 | 87 | if (options.options.contains("filter")) { | |
87 | bool compact = false; | 88 | for (const auto &f : options.options.value("filter")) { |
88 | auto compactIndex = args.indexOf("--compact"); | 89 | auto filter = f.split("="); |
89 | if (compactIndex >= 0) { | 90 | query.filter(filter.at(0).toLatin1(), QVariant::fromValue(Sink::ApplicationDomain::Reference{filter.at(1).toLatin1()})); |
90 | args.removeAt(compactIndex); | 91 | } |
91 | compact = true; | ||
92 | } | 92 | } |
93 | 93 | auto compact = options.options.contains("compact"); | |
94 | auto allIndex = args.indexOf("--all"); | 94 | if (!options.options.contains("showall")) { |
95 | if (allIndex >= 0) { | 95 | if (options.options.contains("show")) { |
96 | args.removeAt(allIndex); | 96 | auto list = options.options.value("show"); |
97 | } else { | 97 | std::transform(list.constBegin(), list.constEnd(), std::back_inserter(query.requestedProperties), [] (const QString &s) { return s.toLatin1(); }); |
98 | if (!args.isEmpty()) { | ||
99 | std::transform(args.constBegin(), args.constEnd(), std::back_inserter(query.requestedProperties), [] (const QString &s) { return s.toLatin1(); }); | ||
100 | } else { | 98 | } else { |
101 | query.requestedProperties = SinkshUtils::requestedProperties(type); | 99 | query.requestedProperties = SinkshUtils::requestedProperties(type); |
102 | } | 100 | } |
@@ -151,7 +149,7 @@ bool list(const QStringList &args_, State &state) | |||
151 | 149 | ||
152 | Syntax::List syntax() | 150 | Syntax::List syntax() |
153 | { | 151 | { |
154 | Syntax list("list", QObject::tr("List all resources, or the contents of one or more resources"), &SinkList::list, Syntax::EventDriven); | 152 | Syntax list("list", QObject::tr("List all resources, or the contents of one or more resources."), &SinkList::list, Syntax::EventDriven); |
155 | list.completer = &SinkshUtils::resourceOrTypeCompleter; | 153 | list.completer = &SinkshUtils::resourceOrTypeCompleter; |
156 | return Syntax::List() << list; | 154 | return Syntax::List() << list; |
157 | } | 155 | } |
diff --git a/sinksh/syntaxtree.cpp b/sinksh/syntaxtree.cpp index 8a8684c..ee9d6f8 100644 --- a/sinksh/syntaxtree.cpp +++ b/sinksh/syntaxtree.cpp | |||
@@ -223,3 +223,30 @@ QStringList SyntaxTree::tokenize(const QString &text) | |||
223 | 223 | ||
224 | return tokens; | 224 | return tokens; |
225 | } | 225 | } |
226 | |||
227 | SyntaxTree::Options SyntaxTree::parseOptions(const QStringList &args) | ||
228 | { | ||
229 | Options result; | ||
230 | auto it = args.constBegin(); | ||
231 | for (;it != args.constEnd(); it++) { | ||
232 | if (it->startsWith("--")) { | ||
233 | QString option = it->mid(2); | ||
234 | QStringList list; | ||
235 | it++; | ||
236 | for (;it != args.constEnd(); it++) { | ||
237 | if (it->startsWith("--")) { | ||
238 | it--; | ||
239 | break; | ||
240 | } | ||
241 | list << *it; | ||
242 | } | ||
243 | result.options.insert(option, list); | ||
244 | if (it == args.constEnd()) { | ||
245 | break; | ||
246 | } | ||
247 | } else { | ||
248 | result.positionalArguments << *it; | ||
249 | } | ||
250 | } | ||
251 | return result; | ||
252 | } | ||
diff --git a/sinksh/syntaxtree.h b/sinksh/syntaxtree.h index be56067..6624388 100644 --- a/sinksh/syntaxtree.h +++ b/sinksh/syntaxtree.h | |||
@@ -67,6 +67,12 @@ public: | |||
67 | 67 | ||
68 | static QStringList tokenize(const QString &text); | 68 | static QStringList tokenize(const QString &text); |
69 | 69 | ||
70 | struct Options { | ||
71 | QStringList positionalArguments; | ||
72 | QMap<QString, QStringList> options; | ||
73 | }; | ||
74 | static Options parseOptions(const QStringList &text); | ||
75 | |||
70 | private: | 76 | private: |
71 | SyntaxTree(); | 77 | SyntaxTree(); |
72 | 78 | ||