From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,HTML_MESSAGE,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 7867 invoked from network); 31 Jul 2021 14:21:43 -0000 Received: from minnie.tuhs.org (45.79.103.53) by inbox.vuxu.org with ESMTPUTF8; 31 Jul 2021 14:21:43 -0000 Received: by minnie.tuhs.org (Postfix, from userid 112) id DA2789CA3F; Sun, 1 Aug 2021 00:21:40 +1000 (AEST) Received: from minnie.tuhs.org (localhost [127.0.0.1]) by minnie.tuhs.org (Postfix) with ESMTP id A589F9C9B2; Sun, 1 Aug 2021 00:21:15 +1000 (AEST) Authentication-Results: minnie.tuhs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="bp++WbBh"; dkim-atps=neutral Received: by minnie.tuhs.org (Postfix, from userid 112) id 2BE4A9C9B2; Sun, 1 Aug 2021 00:21:14 +1000 (AEST) Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) by minnie.tuhs.org (Postfix) with ESMTPS id B570E9C9AF for ; Sun, 1 Aug 2021 00:21:13 +1000 (AEST) Received: by mail-pl1-f179.google.com with SMTP id k1so14477000plt.12 for ; Sat, 31 Jul 2021 07:21:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=Jvo+UacfrdbtqwjrTeiwXYi0lP9o773+mnHPOZWmZW8=; b=bp++WbBhbg8LLDJD1LPcQfQgW9yz+OUJ5kcS5VUy6DWnC3WRyNLhTGYeAs7lhjqGS6 YlJU5b2+EOxcBes+o48YWk3QX34hKnM0QLDyOJkxREcMkoKproHd/rCUoyVCF9XiLlia TzdyjHpvDsyhlwcPnocf0dLvCv8nsYTrgbGvP67/XB0Q8c258Z+zAvMlmVHilrEAee1E MN8MAi8bNusYAMvRCygHiiqLy4B9BeEw9GcI5gVyD6bqqI5rZvaO9seZ0DugOm0ohkug jgLWk912r3mxvmXhbAa+7ioX01V9JgiD+5QjhOYU9/iAXqmexkoqGKZCIm1ORcd1YTmk i56Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=Jvo+UacfrdbtqwjrTeiwXYi0lP9o773+mnHPOZWmZW8=; b=YxXg98pCyhWGBdRYGtisG/a+FY/9BJODtHtWJPxp0fQGN8ZjbQEfh9ULwLuGbxR9x5 MNXTdHQzteS11zn307udGrLkoUbvxMXf3TNUL3W7Y/zyhit9KtxZKpcmv4q2PjoV1hw9 zWly0K3riQCdgssU36DvsccicH0HTTgu1CL2VUj3WND41yl5mHhCztcsHZgZTvHmnWw0 GqpScP91KCdqaON2CCWVFYiJE4T1Dsc/cKM/agNgRLxCua1/4PXZrQJLgi2MrpxLiE15 EIbzI61Wiw7h9M1t3kiIUfLWmwLzAEr5oNR8l0mX/VTY12D42pjqkGWMi2iC7idsTqC2 GMmA== X-Gm-Message-State: AOAM531b8HNi4EbevI8tQguuvZfrZFc0SqAN3ECVSKAhpyo0/NgaIJsO YdATkuwj/7+ssF4Gfp932qM72bk9VKBS40Qn00FHOJpI3Yg= X-Google-Smtp-Source: ABdhPJynY8+/sEYeD6xu4ElNCtWzdLaRxFWZWCS5Jrux2iNu74Rbz6dm/UP04auuO6XL+3j+22M5k+xPuKb1s+N//gk= X-Received: by 2002:a05:6a00:1712:b029:327:75dd:c8da with SMTP id h18-20020a056a001712b029032775ddc8damr7815844pfc.34.1627741272676; Sat, 31 Jul 2021 07:21:12 -0700 (PDT) MIME-Version: 1.0 References: <20210731142533.69caf929@moon> In-Reply-To: <20210731142533.69caf929@moon> From: Adam Thornton Date: Sat, 31 Jul 2021 07:21:01 -0700 Message-ID: To: TUHS main list Content-Type: multipart/alternative; boundary="000000000000581dc605c86c0cfe" Subject: Re: [TUHS] Systematic approach to command-line interfaces X-BeenThere: tuhs@minnie.tuhs.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: The Unix Heritage Society mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: tuhs-bounces@minnie.tuhs.org Sender: "TUHS" --000000000000581dc605c86c0cfe Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Jul 31, 2021, at 5:25 AM, Michael Siegel wrote: While doing that, I learned that there is a better way to approach this problem =E2=80=93 beyond using getopt(s) (which never really made sens= e to me) and having to write case statements in loops every time: Define a grammar, let a pre-built parser do the work, and have the parser provide the results to the program. I see that Dan Halbert beat me to mentioning "click." The trick with shell is that unless you write the parser in shell, which is going to be miserable, you=E2=80=99re doing it in a command in a subshell, = and therefore your return values have to be a structured stream of bytes on stdout, which the parent shell is going to have to interpret. An eval-able shell fragment, where you have a convention of what the variables you get from the option parser will be, is probably the easiest way, since from the parent that would look like: $(parse_my_opts $*) # Magic variables spring to life if [ =E2=80=9C$OPT_SUBCOMMAND_0=E2=80=9D =3D=3D =E2=80=9Cburninate=E2=80=9D= ]; then =E2=80=A6. Adam --000000000000581dc605c86c0cfe Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On Jul 31, 2021, at 5:25 AM, Michael Siegel <msi@malbolge.net> wrote:

While doing that, I learned that there is a better way to= approach
this problem =E2=80=93 beyond using getopt(s) (which never rea= lly made sense to
me) and having to write case statements in loops every= time: Define a
grammar, let a pre-built parser do the work, and have th= e parser
provide the results to the program.

I see that Dan Halbert beat me to mentioning "click.&q= uot;

The trick with shell is that unless you w= rite the parser in shell, which is going to be miserable, you=E2=80=99re do= ing it in a command in a subshell, and therefore your return values have to= be a structured stream of bytes on stdout, which the parent shell is going= to have to interpret.=C2=A0 An eval-able shell fragment, where you have a = convention of what the variables you get from the option parser will be, is= probably the easiest way, since from the parent that would look like:

$(parse_my_opts $*)
# Magic variables spring= to life
if [ =E2=80=9C$OPT_SUBCOMMAND_0=E2=80=9D =3D=3D =E2=80= =9Cburninate=E2=80=9D ]; then =E2=80=A6.

Adam
--000000000000581dc605c86c0cfe--