From nobody Thu Feb 27 17:51:08 2025 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Z3f5m67P3z59LfK; Thu, 27 Feb 2025 17:51:08 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Z3f5m54yjz493w; Thu, 27 Feb 2025 17:51:08 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1740678668; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ohf5Ljt3FQTKHILIshEKdshRoDG4GqH2bvVVFs1xCI0=; b=UCrmrtg8njMC6+Ih1ds9WJblyHEHU5cZqp42xbc7LzIDcAamoOKC9q79Gk6MbFUPan48lZ L7rBW4d1NzuvSyP5eqVtw04HTD4p96SaWSihp50xfCu0SCfviJoE28ob97H8vk28ZpFzwq XwaLtKaZJjiG1bpAXi147j4KGAymMbulQWy18vqN8jGW8XiKT8mYS/jwaRvjssIQlSj9OH yOldS4JuQDB906LYh/bDo/BAEWkExtxDum+HUNfuXjd6wmm5poEFSQhQ0jEch+jHNcIWtW 9X2P59tgS01v61XpEO9c5epDKB/m9HteqJK6u33fIb0RjgCYBdPVNd+UbzYSog== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1740678668; a=rsa-sha256; cv=none; b=t8jbgf/5PqUKmslQbbT8ldN+GUX6fnfgs2LJB0dGAizrYTQXwQNlBN1/qTqA0VCr3XkYbR y7EAAEkfuvMceze+JHcRhF7HpqNPl6D3mSw5yVKz5tyhlACr3/4K0etAHuGJI4IZvRD5Am SPOYGq/5Q+dixYlBh5jmpO4G468Vh4dytdGCa+TMK5JcoAKFhELkNT5uMhFDoYFEXhbHtO HH077USOWBYaENYzBdg4z2uKZ40xHkZ66dah41HJ51azxKUroqrwmy9LkYGzwyxEgnBjAo 1hz38WOk1uYbbDuLypunJuwq8YPMU24aXc8Vp4/8Qz3HTqHdNflJkj6QW5n00Q== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1740678668; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ohf5Ljt3FQTKHILIshEKdshRoDG4GqH2bvVVFs1xCI0=; b=Knj4drBw7XkzdDCwT42u/IFJSq4V+3TPaaQzIHMzcOoOBXekmjUwOBCbQIFVJMi6K4DQHF IjQuNkVTagvd9Dhy6NH5F1W9LqFAdiRP6NTD7YcdGHTj1HLnjPqucVdkA1UvtUExx23dPf EtfIyK+pN1akVezl6eQODIhas67tqH3tjkaetHktA0Z9E//Iae1YfySBvyGvYa6rlETLLj cJDggaPrJ2zoW7IacEgwf9sP+G6ygKx/2CU566yCVQpYWxaRuBvZf3trvpNeX+/HgFo7n6 N3OuzIJsev07UkmMWH6fo1f7c+0nOh22Q9IqaToReXAltWswMoKUYvwL0c453A== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Z3f5m4ckqzskT; Thu, 27 Feb 2025 17:51:08 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 51RHp8Pd025468; Thu, 27 Feb 2025 17:51:08 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 51RHp8xu025465; Thu, 27 Feb 2025 17:51:08 GMT (envelope-from git) Date: Thu, 27 Feb 2025 17:51:08 GMT Message-Id: <202502271751.51RHp8xu025465@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Piotr Pawel Stefaniak Subject: git: 2980318b2747 - main - sh.1: extend the section about getopts List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: pstef X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 2980318b2747711433c4408e89cb1b208aac9748 Auto-Submitted: auto-generated The branch main has been updated by pstef: URL: https://cgit.FreeBSD.org/src/commit/?id=2980318b2747711433c4408e89cb1b208aac9748 commit 2980318b2747711433c4408e89cb1b208aac9748 Author: Piotr Paweł Stefaniak AuthorDate: 2025-02-22 15:57:05 +0000 Commit: Piotr Paweł Stefaniak CommitDate: 2025-02-27 17:39:29 +0000 sh.1: extend the section about getopts Provide more details about the influence of optargs' first character on the shell's behavior in regard to invalid arguments. Also do some minor word-smithing. Original submission by rea@ Differential Revision: https://reviews.freebsd.org/D49106 --- bin/sh/sh.1 | 60 ++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 14 deletions(-) diff --git a/bin/sh/sh.1 b/bin/sh/sh.1 index 3d3f200a8e17..768193905c49 100644 --- a/bin/sh/sh.1 +++ b/bin/sh/sh.1 @@ -31,7 +31,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd January 10, 2025 +.Dd February 27, 2025 .Dt SH 1 .Os .Sh NAME @@ -2356,29 +2356,61 @@ Move the specified .Ar job or the current job to the foreground. .It Ic getopts Ar optstring var -The POSIX -.Ic getopts -command. -The -.Ic getopts -command deprecates the older -.Xr getopt 1 -command. -The first argument should be a series of letters, each possibly +Parse command-line options and arguments. +The first argument +.Va optstring +should be a series of letters, each possibly followed by a colon which indicates that the option takes an argument. -The specified variable is set to the parsed option. +The specified variable +.Va var +is set to the parsed option. The index of the next argument is placed into the shell variable .Va OPTIND . If an option takes an argument, it is placed into the shell variable .Va OPTARG . -If an invalid option is encountered, +.Pp +If the found character +is not specified by +.Va optstring +or if it is missing a required argument, +the option is considered invalid and: +.Bl -offset indent +.It +If the first character of +.Va optstring +is not a colon then +.Va OPTARG +is unset, .Ar var is set to -.Ql \&? . -It returns a false value (1) when it encounters the end of the options. +.Ql \&? +and a diagnostic message is written to stderr. +.It +If the first character of +.Va optstring +is a colon then +.Va OPTARG +is set to the the option character found, +.Ar var +is set to +.Ql \&: +when a required argument is missing or to +.Ql \&? +when the option was not specified by +.Va optstring , +and no diagnostic message is written to stderr. +.El +.Pp +.Ic getopts +returns a false value (1) when it encounters the end of the options. A new set of arguments may be parsed by assigning .Li OPTIND=1 . +The POSIX +.Ic getopts +command deprecates the older +.Xr getopt 1 +command. .It Ic hash Oo Fl rv Oc Op Ar command ... The shell maintains a hash table which remembers the locations of commands. With no arguments whatsoever, the