From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id B21436752A for ; Wed, 26 Aug 2020 21:27:50 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 35A12117D0 for ; Wed, 26 Aug 2020 21:27:20 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id A655B117C5 for ; Wed, 26 Aug 2020 21:27:17 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 4AA1B448C0 for ; Wed, 26 Aug 2020 21:27:17 +0200 (CEST) From: Thomas Lamprecht To: pve-devel@lists.proxmox.com Date: Wed, 26 Aug 2020 21:27:10 +0200 Message-Id: <20200826192710.2131502-1-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.094 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [jsonschema.pm] Subject: [pve-devel] [PATCH common] get_options: allow optional arguments "arg_params" if no ambiguity X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 26 Aug 2020 19:27:52 -0000 If we run out of passed arguments from the user but still had defined "arg_params" (those params which went after the command in fixed order without option -- dashes) we always errored out with "not enough arguments". But, there are situations where the remaining arg_params are all marked as optional in the schema, so we do not need to error out in that case. Signed-off-by: Thomas Lamprecht --- A prime (future) use case is "pvesm prune-backups". Currently the usage is: > pvesm prune-backups storeid --prune-backups keep-last=1,keep-... Because the "prune-backups" keep retention property is optional as it can fallback to the one defined in the storage configuration. With this patch we can make it an argument and allow the following two usages: 1. As above, but avoiding the extra ugly --prune-backups > pvesm prune-backups storeid keep-last=1,keep-... 2. Fallback to storage config: > pvesm prune-backups storeid src/PVE/JSONSchema.pm | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/PVE/JSONSchema.pm b/src/PVE/JSONSchema.pm index c5002d8..2ceb1bd 100644 --- a/src/PVE/JSONSchema.pm +++ b/src/PVE/JSONSchema.pm @@ -1613,7 +1613,8 @@ sub get_options { $opts->{$list_param} = $args; $args = []; } elsif (ref($arg_param)) { - foreach my $arg_name (@$arg_param) { + for (my $i = 0; $i < scalar(@$arg_param); $i++) { + my $arg_name = $arg_param->[$i]; if ($opts->{'extra-args'}) { raise("internal error: extra-args must be the last argument\n", code => HTTP_BAD_REQUEST); } @@ -1622,7 +1623,15 @@ sub get_options { $args = []; next; } - raise("not enough arguments\n", code => HTTP_BAD_REQUEST) if !@$args; + if (!@$args) { + # check if all left-over arg_param are optional, else we + # must die as the mapping is then ambigious + for (my $j = $i; $j < scalar(@$arg_param); $j++) { + my $prop = $arg_param->[$j]; + raise("not enough arguments\n", code => HTTP_BAD_REQUEST) + if !$schema->{properties}->{$prop}->{optional}; + } + } $opts->{$arg_name} = shift @$args; } raise("too many arguments\n", code => HTTP_BAD_REQUEST) if @$args; -- 2.20.1