From eb4c557efa38673eba773bda6b71a286d0c3c3b1 Mon Sep 17 00:00:00 2001 From: Minijackson Date: Thu, 2 Aug 2018 10:40:35 +0200 Subject: Add subcommand/parameter/option/flag and automatic help --- sinksh/syntax_modules/sink_modify.cpp | 50 +++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 23 deletions(-) (limited to 'sinksh/syntax_modules/sink_modify.cpp') diff --git a/sinksh/syntax_modules/sink_modify.cpp b/sinksh/syntax_modules/sink_modify.cpp index 2579550..cd29d56 100644 --- a/sinksh/syntax_modules/sink_modify.cpp +++ b/sinksh/syntax_modules/sink_modify.cpp @@ -38,20 +38,36 @@ namespace SinkModify { -bool modify(const QStringList &args, State &state) +bool modify(const QStringList &args, State &state); +bool resource(const QStringList &args, State &state); + +Syntax::List syntax() { - if (args.isEmpty()) { - state.printError(QObject::tr("A type is required"), "sink_modify/02"); - return false; - } + Syntax modify("modify", QObject::tr("Modify items in a resource"), &SinkModify::modify); + modify.addPositionalArgument({ .name = "type", .help = "The type of entity to modify (mail, event, etc.)" }); + modify.addPositionalArgument({ .name = "resourceId", .help = "The ID of the resource containing the entity" }); + modify.addPositionalArgument({ .name = "objectId", .help = "The ID of the entity" }); + modify.addPositionalArgument( + { .name = "key value", .help = "Attributes and values to modify", .required = false, .variadic = true }); - if (args.count() < 2) { - state.printError(QObject::tr("A resource ID is required to remove items"), "sink_modify/03"); - return false; - } + Syntax resource("resource", QObject::tr("Modify a resource"), &SinkModify::resource);//, Syntax::EventDriven); + + resource.addPositionalArgument({ .name = "id", .help = "The ID of the resource" }); + resource.addPositionalArgument( + { .name = "key value", .help = "Attributes and values to modify", .required = false, .variadic = true }); + + resource.completer = &SinkshUtils::resourceOrTypeCompleter; + modify.children << resource; + + return Syntax::List() << modify; +} +REGISTER_SYNTAX(SinkModify) + +bool modify(const QStringList &args, State &state) +{ if (args.count() < 3) { - state.printError(QObject::tr("An object ID is required to remove items"), "sink_modify/03"); + state.printError(syntax()[0].usage()); return false; } @@ -81,6 +97,7 @@ bool modify(const QStringList &args, State &state) bool resource(const QStringList &args, State &state) { if (args.isEmpty()) { + // TODO: pass the syntax as parameter state.printError(QObject::tr("A resource can not be modified without an id"), "sink_modify/01"); } @@ -105,17 +122,4 @@ bool resource(const QStringList &args, State &state) return true; } - -Syntax::List syntax() -{ - Syntax modify("modify", QObject::tr("Modify items in a resource"), &SinkModify::modify); - Syntax resource("resource", QObject::tr("Modify a resource"), &SinkModify::resource);//, Syntax::EventDriven); - resource.completer = &SinkshUtils::resourceOrTypeCompleter; - modify.children << resource; - - return Syntax::List() << modify; -} - -REGISTER_SYNTAX(SinkModify) - } -- cgit v1.2.3